[英]Real time output of wget command by using exec_command in Paramiko
我試圖在所有機器上下載文件,因此創建了一個python腳本。 它使用模塊paramiko
只是代碼片段:
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())
下載完成后,將打印輸出。
有沒有辦法可以實時打印輸出?
編輯:我已經看過這個答案,並應用了這樣的東西:
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
的進度信息輸出到stderr,因此您應該編寫line_buffered(err)
。
或者,您可以使用exec_command("wget xyz", get_pty=True)
,它將結合stdout和stderr 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.