Check if paramiko ssh connection is still alive
I had observed is_active() returning false positives.
I would recommend using this piece:
# use the code below if is_active() returns True
try:
transport = client.get_transport()
transport.send_ignore()
except EOFError, e:
# connection is closed
For me the above answer doesn't work so I've done it by sending an exec_command() with a timeout :
self.ssh.exec_command('ls', timeout=5)
for exemple the full method will be like :
def check_connection(self):
"""
This will check if the connection is still availlable.
Return (bool) : True if it's still alive, False otherwise.
"""
try:
self.ssh.exec_command('ls', timeout=5)
return True
except Exception as e:
print "Connection lost : %s" %e
return False
and I call it every 5 or so seconds.
if ssh.get_transport() is not None:
ssh.get_transport().is_active()
should do it .... assuming I read the docs right.