【发布时间】:2018-07-16 17:45:12
【问题描述】:
我正在尝试在所有机器上下载一个文件,因此我创建了一个 python 脚本。它使用模块paramiko
只是代码中的一个 sn-p:
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())
下载完成后会打印输出!
有没有办法可以实时打印输出?
编辑:我查看了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
但是,它不是实时打印数据。!它等待文件下载,然后打印下载的整个状态。
另外,我试过这个命令:echo one && sleep 5 && echo two && sleep 5 && echo three 并且输出是实时的,带有line_buffered 函数。但是,对于wget 命令,它不起作用..
【问题讨论】:
-
wget实际上会在stdout上打印任何内容,所以我对你的问题有点困惑。下载文件后,stdout得到什么输出?