Timeout in paramiko (python)

61,478

The connection timeout can be set with the timeout parameter (that indicated the number of seconds for the time out as described here) of the connect function.

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(host, username=username, password=password, timeout=10)
sftp = ssh.open_sftp()
sftp.get(remotepath, localpath)
sftp.close()
Share:
61,478

Related videos on Youtube

Kukosk
Author by

Kukosk

Updated on April 04, 2020

Comments

  • Kukosk
    Kukosk about 4 years

    I'm looking for a way to set a timeout for this:

    transport = paramiko.Transport((host, port))
    transport.connect(username = username, password = password)
    sftp = paramiko.SFTPClient.from_transport(transport)
    sftp.get(remotepath, localpath)
    sftp.close()
    transport.close()
    
  • manoj prashant k
    manoj prashant k over 6 years
    @kukosk It would help if you mentioned the unit in which timeout is to be given, I guess its seconds.
  • jxramos
    jxramos almost 6 years
    What's the default timeout, I don't think I found it mentioned in the docs either, although they do mention timeout (float) – an optional timeout (in seconds) for the TCP connect
  • jxramos
    jxramos almost 6 years
    Here's some more background, if timeout is not specified the client falls into blocking mode: docs.python.org/3/library/socket.html#socket-timeouts
  • jxramos
    jxramos almost 6 years
    Lots of default timeout definitions pop up at github.com/jbouse-debian/paramiko/blob/master/paramiko/…, all given in seconds
  • trozen
    trozen about 5 years
    Note that timeout parameter sets TCP timeout. There are also banner_timeout and auth_timeout which you may want to adjust as well.
  • chrisinmtown
    chrisinmtown almost 2 years
    @jxramos are you sure about blocking behavior? When I tested an unreachable address with no timeout argument, the connect timed out after 75 seconds.