Python Paramiko exec_command timeout doesn't work?

懵懂的女人 提交于 2019-12-07 19:51:14

问题


Hey guys I got the following ssh command,

    try:
        print 'trying to restart'
        self.ssh.exec_command(RR_CMD % (self.path_ext, self.rport), timeout=1)
        print 'restarted'
    except:
        self.ssh.close()
        self.ssh = ssh.create_ssh_client(self.ip, self.port, self. username, self.password)
        self.restart()

Basically I'm trying to restart a remote perl script. But sometimes, like let's say 1 out of 2000 - my python program is freezing over the exec_command line for sometimes up to a few minutes!

I would like to use the timeout function, which I set to 1 second, but it doesn't work for some reason...


回答1:


I've had issues with the timeout in exec_command not being honored how I would anticipate. For example, I had the timeout set to 60 and found a hung command running all night. What I was doing before was

response = client.exec_command(command, timeout=60)
returncode = response[0].channel.recv_exit_status()

But with a timeout of None or any value, it was hanging on recv_exit_status, Now, instead, I am just managing the timeout myself, since exec_command is non-blocking, by polling channel.exit_status_ready.

start = time.time()
while time.time() < start + timeout:
    if response[0].channel.exit_status_ready():
        break
    time.sleep(1)
else:
    raise TimeoutError(f'{command} timed out on {hostname}')
returncode = response[0].channel.recv_exit_status()



回答2:


What about your version of paramiko?

The latest version of paramiko supported argument timeout.

Paramiko Changelog v1.10.0:

Add timeout parameter to SSHClient.exec_command for easier setting of the command’s internal channel object’s timeout. Thanks to Cernov Vladimir for the patch.



来源:https://stackoverflow.com/questions/35403031/python-paramiko-exec-command-timeout-doesnt-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!