2

I am trying to read the output of a long-running ssh command continuously. I understand that exec_command is non-blocking. But as soon as I use stdout.readlines() it becomes blocking. I don't want to wait for 10 minutes for my ssh command to finish to read all the output lines. I want to get the output as soon as the ssh command writes in stdout. Is there a way to do it?

import paramiko
#import select
ssh=paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname,username=username,password=password)
#transport = ssh.get_transport()
#channel = transport.open_session()
stdin,stdout,stderr = ssh.exec_command(command)
print stdout.readlines()
0

1 Answer 1

3
import paramiko
ssh=paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname,username=username,password=password)
stdin,stdout,stderr = ssh.exec_command(command)
for line in iter(lambda: stdout.readline(2048), ""):
    print(line)

The above code helped. I got this answer suggestion from get output from a paramiko ssh exec_command continuously

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.