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.
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.
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).
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:
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.
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.
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With