Paramiko ssh_config parameters
Question: 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.


Answer: 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
    

