Make sure paramiko ssh connection is still alive

Is there a way to check if the paramikoSSH connection supports ?

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 imitate something like this (which does not currently exist)

In [5]: ssh.isalive()
True
+4
source share
3 answers
 ssh.get_transport().is_active()

should do it .... assuming i read the docs correctly

+5
source

I noticed 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
+6
source

, , exec_command() :

self.ssh.exec_command('ls', timeout=5)

, :

def check_connection(self):
    """
    This will check if the connection is still availlable.

    Return (bool) : True if it 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

5 .

+1

All Articles