Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if paramiko ssh connection is still alive

Tags:

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
like image 573
MRocklin Avatar asked Feb 02 '15 23:02

MRocklin


People also ask

Is paramiko safe?

The python package paramiko was scanned for known vulnerabilities and missing license, and no issues were found. Thus the package was deemed as safe to use.

How do I get stdout from paramiko?

Using exec_command() in Paramiko returns a tuple (stdin, stdout, stderr) . Most of the time, you just want to read stdout and ignore stdin and stderr . You can get the output the command by using stdout. read() (returns a string) or stdout.

What is paramiko alternative?

While you wait for paramiko on Python 3, you can use putty.


2 Answers

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

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

like image 43
Joran Beasley Avatar answered Oct 01 '22 22:10

Joran Beasley


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
like image 92
user2488286 Avatar answered Oct 01 '22 23:10

user2488286