[英]Real time output of wget command by using exec_command in Paramiko
I am trying to download a file in all the machine and therefore I have created a python script. 我试图在所有机器上下载文件,因此创建了一个python脚本。 It uses the module
paramiko
它使用模块
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. 另外,我尝试了以下命令:
echo one && sleep 5 && echo two && sleep 5 && echo three
,并且输出是实时的,具有line_buffered
函数。 But, for wget
command, it is not working.. 但是,对于
wget
命令,它不起作用。
wget
's progress information outputs to stderr so you should write line_buffered(err)
. wget
的进度信息输出到stderr,因此您应该编写line_buffered(err)
。
Or you can use exec_command("wget xyz", get_pty=True)
which would combine stdout and stderr . 或者,您可以使用
exec_command("wget xyz", get_pty=True)
,它将结合stdout和stderr 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.