0

I am trying to download a file in all the machine and therefore I have created a python script. It uses the module paramiko

just a snippet from the code:

from paramiko import SSHClient, AutoAddPolicy
ssh = SSHClient()
ssh.load_system_host_keys()
ssh.set_missing_host_key_policy(AutoAddPolicy())
ssh.connect(args.ip,username=args.username,password=args.password)

stdin, stdout, stderr = ssh.exec_command("wget xyz")
print(stdout.read())

The output will be printed after the download has been completed.!

Is there a way I can print the output real time?

Edit: I have looked at this answer and applied something like this:

def line_buffered(f):
    line_buf = ""
    print "start"
    print f.channel.exit_status_ready()
    while not f.channel.exit_status_ready():
        print("ok")
        line_buf += f.read(size=1)
        if line_buf.endswith('\n'):
            yield line_buf
            line_buf = ''

 in, out, err = ssh.exec_command("wget xyz")
 for l in line_buffered(out):
        print l

But, It is not printing the data real time.! It waits for the file to download and then prints the whole status of the download.

Also, I have tried this command : echo one && sleep 5 && echo two && sleep 5 && echo three and the output is realtime with line_buffered function. But, for wget command, it is not working..

Jay Joshi
  • 1,402
  • 1
  • 13
  • 32
  • `wget` actually does print anything on `stdout`, so I'm bit confused by your question. What output do you get in `stdout` after the file is downloaded? – Martin Prikryl Jul 12 '18 at 10:18

1 Answers1

2

wget's progress information outputs to stderr so you should write line_buffered(err).

Or you can use exec_command("wget xyz", get_pty=True) which would combine stdout and stderr.

pynexj
  • 19,215
  • 5
  • 38
  • 56