Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Paramiko, exec_command get the output stream continuously [duplicate]

I dry on a Python script. I create a python script for a given IP will connect via Paramiko to a server to execute another Python script.

Here is some code:

self._client = paramiko.SSHClient()
self._client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
self._client.connect(self._ip, username=self._server['username'], password=self._server['password'])
channel = self._client.get_transport().open_session()
channel.exec_command('python3 /tmp/scrap.py /tmp/' + self._ip + '.txt 0 1')

The script "scrap.py" returns every X seconds a line in the console of the remote machine, but I can not recover as and when these lines in the script above (at the exit of exec_command (. ..)).

Is this possible, and if so have you any idea how?

Thank you in advance.

like image 967
Gon3s Avatar asked Jul 09 '15 12:07

Gon3s


People also ask

How to get the output of exec_command 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.readlines () (returns a list of lines). This website uses cookies to improve your experience.

What is paramiko and how do I use it?

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. To authenticate an SSH connection, we need to set up a private RSA SSH key (not to be confused with OpenSSH).

How to connect to remote SSH server using paramiko library?

To install paramiko library, run the subsequent command in the command prompt. paramiko needs cryptography as a dependency module. So run both commands in command prompt : After installation is completed, now we’ll hook up with a remote SSH server using paramiko library. Code snippet for an equivalent is given below:

How do I get the output of the command?

You can get the output the command by using stdout.read () (returns a string) or stdout.readlines () (returns a list of lines). This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish.


2 Answers

This should do the trick:

stdin, stdout, stderr = channel.exec_command("python myScript.py")
stdin.close()
for line in iter(lambda: stdout.readline(2048), ""):
    print(line, end="")

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.

like image 191
Lukas N.P. Egger Avatar answered Nov 05 '22 06:11

Lukas N.P. Egger


I'm not sure if my version of paramiko is outdated, but Lukas N.P. Egger solution didn't work for me, as channel.exec_command did not return three things, only one. This worked for me:

print 'connecting'
transport = paramiko.Transport((IP_ADDRESS, 22))
transport.connect(username='USERNAME', password='PASSWORD')
print 'opening ssh channel'
channel = transport.open_session()
channel.set_combine_stderr(1)
channel.exec_command('cd render && python render.py')
while True:
  if channel.exit_status_ready():
    if channel.recv_ready():
      print 'done generating graph'
      break
  sys.stdout.write(channel.recv(1))
print 'closing ssh channel'
channel.close()
like image 36
Daniel F Avatar answered Nov 05 '22 06:11

Daniel F