Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get output from a Paramiko SSH exec_command continuously

I am executing a long-running python script via ssh on a remote machine using paramiko. Works like a charm, no problems so far.

Unfortunately, the stdout (respectively the stderr) are only displayed after the script has finished! However, due to the execution time, I'd much prefer to output each new line as it is printed, not afterwards.

remote = paramiko.SSHClient() remote.set_missing_host_key_policy(paramiko.AutoAddPolicy()) remote.connect("host", username="uname", password="pwd")   # myScript produces continuous output, that I want to capture as it appears     stdin, stdout, stderr = remote.exec_command("python myScript.py") stdin.close() for line in stdout.read().splitlines():     print(line) 

How can this be achieved? Note: Of course one could pipe the output to a file and 'less' this file via another ssh session, but this is very ugly and I need a cleaner, ideally pythonic solution :)

like image 657
Lukas N.P. Egger Avatar asked Aug 05 '15 14:08

Lukas N.P. Egger


People also ask

How do I get command output in 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 SSHClient ()?

SSH client & key policies class paramiko.client. SSHClient. A high-level representation of a session with an SSH server. This class wraps Transport , Channel , and SFTPClient to take care of most aspects of authenticating and opening channels.

What can you do with Paramiko?

Paramiko helps you automate repetitive system administration tasks on remote servers. More advanced Paramiko programs send the lines of a script one at a time. It does this rather than transacting all of a command, such as df or last , synchronously to completion.

Why is Paramiko used?

Paramiko is a Python library that makes a connection with a remote device through SSh. Paramiko is using SSH2 as a replacement for SSL to make a secure connection between two devices. It also supports the SFTP client and server model.


2 Answers

A minimal and complete working example of how to use this answer (tested in Python 3.6.1)

# run.py from paramiko import SSHClient  ssh = SSHClient() ssh.load_system_host_keys()  ssh.connect('...')  print('started...') stdin, stdout, stderr = ssh.exec_command('python -m example', get_pty=True)  for line in iter(stdout.readline, ""):     print(line, end="") print('finished.') 

and

# example.py, at the server import time  for x in range(10):     print(x)     time.sleep(2) 

run on the local machine with

python -m run 
like image 131
Jorge Leitao Avatar answered Oct 13 '22 01:10

Jorge Leitao


As specified in the read([size]) documentation, if you don't specify a size, it reads until EOF, that makes the script wait until the command ends before returning from read() and printing any output.

Check this answers: How to loop until EOF in Python? and How to do a "While not EOF" for examples on how to exhaust the File-like object.

like image 28
KurzedMetal Avatar answered Oct 13 '22 01:10

KurzedMetal