[Python] Python Paramiko.SHH invoke_shell() set timeout for user in linux and check if session...

Discussão em 'Python' iniciado por Stack, Março 17, 2025.

  1. Stack

    Stack Membro Participativo

    Just a small question about Paramiko: As I understand, the regular exec_command() is just a sender for sending commands to a device, but doesn't store a state of "terminal". On the other hand, there is invoke_shell() which stores the state of "terminal". And as an example, I have a timeout for all users on my device for active sessions, for example 9999 and this is readonly. But I don't want to wait 9999, so I want to change it to 10 sec, just to check if everything works fine, my rules are applied and etc. So I do noprofile bash and set this variable as 10 sec: exec bash --noprofile --norc -c "export TMOUT=10". If I do this using invoke_shell() and send() method I didn't get terminated my session as after 10 sec I do recv(1024) and see an outcome, but I should have received a paramiko.SSHException. And here is the code:

    command = f'exec bash --noprofile --norc -c "export TMOUT=10;"'
    noprofile_shell = connection.invoke_shell()
    noprofile_shell.send(command)

    time.sleep(10)

    with pytest.raise(paramiko.SSHException):
    noprofile_shell.send("pwd")
    noprofile_shell.recv(1024)


    Did I do something wrong? Or is there any other ways to check it?

    Continue reading...

Compartilhe esta Página