Search code examples
pythonsshparamiko

Check if paramiko ssh connection is still alive


Is there a way to check if a paramiko SSH connection is still alive?

In [1]: import paramiko

In [2]: ssh = paramiko.SSHClient() 

In [3]: ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())

In [4]: ssh.connect(hostname)

I want to mimic something like this (which doesn't currently exist)

In [5]: ssh.isalive()
True

Solution

  •  if ssh.get_transport() is not None:
         ssh.get_transport().is_active()
    

    should do it .... assuming I read the docs right.