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..

1
  • 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? Commented Jul 12, 2018 at 10:18

1 Answer 1

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.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.