简体   繁体   English

使用Paramiko中的exec_command实时输出wget命令

[英]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) ,它将结合stdoutstderr

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法停止 paramiko exec_command() 的实时 stream - Cannot stop real time stream of paramiko exec_command() 使用paramiko,我如何实时而不是在完成后获得exec_command()的标准输出? - Using paramiko, how could I get the stdout of exec_command() in real time instead of after completion? 使用 Python Paramiko exec_command 执行时命令输出损坏 - Command output is corrupted when executed using Python Paramiko exec_command 如何使用 Paramiko exec_command 获取每个依赖命令执行 output - How to get each dependent command execution output using Paramiko exec_command 在设备上使用 Paramiko exec_command 执行命令不起作用 - Executing command using Paramiko exec_command on device is not working Python paramiko脚本,在exec_command()期间读取输出的问题 - Python paramiko script, problems reading output during exec_command() 从 Paramiko SSH exec_command 连续获取输出 - Get output from a Paramiko SSH exec_command continuously 您如何使用python将exec_command的输出存储在paramiko中 - How would you store the output of exec_command in paramiko with python 使用带有西里尔文输出的 paramiko exec_command 时如何获得正确的编码输出 - How to get right encoded output when using paramiko exec_command with cyrillic output Paramiko exec_command挂在docker exec上 - Paramiko exec_command hangs on docker exec
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM