I use Paramiko to run some ssh commands to the remote Linux server. The commands will have continuous output in the console and I want to print these all information in the local console window.
stdin, stdout, stderr = ssh.client.exec_command("ls")
for line in stdout.read()
print line,
ssh.client.close()
So if I write the code like this, all the output information will be sent back to me until the command finishes executing while I want to print the output in live.
Thanks a lot.
Of course there is a way to do this. Paramiko
execute_command
is async, bufferes are filled while data arrives regardless of your main thread.In your example
stdout.read(size=None)
will try to read the full buffer size at once. Since new data is always arriving, it won't ever exit. To avoid this, you could just try to read fromstdout
in smaller chunks. Here's an example that reads buffers bytewise and yields lines once a\n
is received.You can increase performance by tweaking the code to use
select.select()
and by having bigger chunk sizes, see this answer that also takes into account common hang and remote command exit detection scenarios that may lead to empty responses.