2

I'm using python with paramiko (wrapped in pysftp) and there appears to be an issue where it will hang for a long time if it cannot authenticate during ssh. I can't figure out a way to set a timeout for the connection and I'm cycling through many machines, so a single machine that is pingable, but not ssh'able (can't reach via cmdline ssh either) is hanging everything. Using this:

ssh -o ServerAliveInterval=1 -o ServerAliveCountMax=1 <host>

I can at least get it to error out after 1 second without waiting for a long time for the authentication in paramiko to die out and raise an exception. However, I can't figure out how to pass these ssh_config options to paramiko (or better yet to apply a timeout to the connect). I tried using the SSHConfig module and that reads in a config file, but it doesn't seem to apply the data anywhere, seems more used for host aliases.

Any help would be appreciated, been searching around for information/help for many hours.

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
user1420676
  • 21
  • 1
  • 2

1 Answers1

1

Establish the initial connection using SSHClient.connect() with a specified socket timeout, then create a SFTPClient using its transport.

Successful connection

>>> import paramiko
>>> client = paramiko.SSHClient()
>>> client.load_system_host_keys()
>>> client.connect(hostname='localhost', port=22, username='user', password='****', timeout=5.0)
>>> sftp = paramiko.SFTPClient.from_transport(client.get_transport())
>>> dirlist = sftp.listdir('.')

Timed out connection

>>> import paramiko
>>> client = paramiko.SSHClient()
>>> client.load_system_host_keys()
>>> client.connect(hostname='slowhost', username='user', password='****', timeout=1.0)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/site-packages/paramiko/client.py", line 296, in connect
    sock.connect(addr)
  File "/usr/lib64/python2.7/socket.py", line 222, in meth
    return getattr(self._sock,name)(*args)
socket.timeout: timed out
mhawke
  • 84,695
  • 9
  • 117
  • 138
  • hi, thanks for the reply! Yes, that is what I am doing in the second instance with the timed out connection, but it doesn't timeout with this host. I'm doing this: >>> import paramiko >>> client = paramiko.SSHClient() >>> client.load_system_host_keys() >>> client.connect(hostname=node.hostname, timeout=1.0) – user1420676 May 28 '12 at 17:12
  • and it just hangs there for a long time and then: Traceback (most recent call last): File "", line 1, in 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 481, in _auth raise saved_exception paramiko.SSHException: No existing session I'm not sure what it is doing, but I'm pretty sure it is in the wait_for_response in the auth_handler. Right now I'm just using subprocess.Popen – user1420676 May 28 '12 at 17:14
  • Sorry, the problem then is not with the TCP connect, it is with the SSH negotiation after the TCP connection has been established. You might be able to get some more details if you look at the paramiko logging: `paramiko.util.log_to_file('paramiko.log')` – mhawke May 29 '12 at 01:29