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()
Sathish
  • 409
  • 8
  • 24

1 Answers1

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

Community
  • 1
  • 1
Sathish
  • 409
  • 8
  • 24