开发者

Paramiko bug: SSHClient.connect() method hangs when the peer is unreachable even if I set the 'timeout'

开发者 https://www.devze.com 2023-04-07 06:21 出处:网络
Here is a python code snippet that uses paramiko: import paramiko sshClient = paramiko.SSHClient() sshClient.set_missing_host_key_policy(paramiko.AutoAddPolicy)

Here is a python code snippet that uses paramiko:

import paramiko

sshClient = paramiko.SSHClient()
sshClient.set_missing_host_key_policy(paramiko.AutoAddPolicy)
sshClient.connect(_peerIp, username=_username, password=_password, timeout=3.0)

As soon as I run the script, I also unplug _peerIp's network cable. And connect() method hangs. Even though the timeout is 3.0, it has been 10 minutes and it still hangs.

(I think the TCP connection was established in a split second and I unplugged the cable during the ssh establishment)

So, do you know any workaround for this? My script will run at a manufacturing factory and it must not hang in such a scenario and handle it properly.

EDIT:

It just gave an exception:

No handlers could be found for logger "paramiko.transport"
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/pymodules/python2.6/paramiko/client.py", line 327, in connect
    self._auth(username, password, pkey, key_filenames, allow_agent, look_for_keys)
  File "/usr/lib/pymodules/python2.6/paramiko/client.py", line 438, in _auth
    self._transport.auth_publickey(username, key)
  File "/usr/lib/pymodules/python2.6/paramiko/transport.py", line 1234, in auth_publickey
    return self.auth_handler.wait_for_response(my_event)
  File "/usr/lib/pymodules/python2.6/paramiko/auth_handler.py", line 163, in wait_for_response
    raise e
socket.error: [Errno 113] No route to host

Ok, at least it eventually raised an exception but I believe this is not the e开发者_如何学Cxpected behaviour. If the timeout is 3.0, connect() method should return something after timeout expires.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号