最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Python Paramiko.SHH invoke_shell() set timeout for user in linux and check if session is closed - Stack Overflow

programmeradmin2浏览0评论

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?

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?

Share Improve this question edited Mar 17 at 6:38 Ulrich Eckhardt 17.5k5 gold badges31 silver badges60 bronze badges asked Mar 14 at 11:45 Павло РакетаПавло Ракета 294 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Find out that this command should be split into the 2:

exec bash --noprofile --norc 
export TMOUT=10

And run via send(), (don't fet about /n in the end). And also there will not be

paramiko.SSHException

You will receive

{OSError}OSError('Socket is closed')

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论