在 python 中运行 shell 脚本时捕获它的实时输出 [英] Capturing LIVE output of shell script while running it in python

查看:53
本文介绍了在 python 中运行 shell 脚本时捕获它的实时输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个 python 脚本,通过 ssh 进入一个 linux 服务器并执行一个已经存储在 linux 服务器上的 shell 脚本.

I am writing a python script to ssh into a linux server and execute a shell script that is already stored on the linux server.

这是我目前代码的样子

command = ['ssh into the remote server',
           'cd into the directory of the shell script,
           './running the shell script',
           ]

process = subprocess.Popen(command,
                            shell=True,
                            stdin=subprocess.PIPE,
                            stdout=subprocess.PIPE,
                            stderr=subprocess.PIPE)

err, out = process.communicate()

if out: 
    print "standard output of subprocess is : "
    print out
if err:
    print "standard error of subprocess is : "
    print err
print "returncode of subprocess: "
print process.returncode

第一个问题:我可以通过stderr获取我的shell脚本的输出,但是我只有在整个shell脚本执行完毕后才能获取.因此,如果 shell 脚本需要 10 分钟才能完成,我只能在 10 分钟后看到 shell 脚本的输出.我想让我的 shell 脚本的输出逐行返回给我,就像我在远程服务器中手动执行脚本一样.可以这样做吗?

1st question: I can obtain the output of my shell scripts through stderr, but I only obtain it after the entire shell script has finished executing. So if the shell script takes 10 minutes to finish, I only get to see the output of the shell script after 10 minutes. I want to have the output of my shell scripts return line by line to me just as if I was executing the script manually in the remote server. Can this be done?

第二个问题:如您所见,我的命令列表中有三个命令(这只是我所有命令的一小部分),如果我将所有命令都放在列表中,我只会获得所有命令的输出仅当我的所有命令都执行完毕后才通过 stdout 命令.如果我的第一个问题不能完成,有没有办法至少在每个命令执行后获得每个命令的输出,而不是只有在所有命令执行完毕后才一次接收它们.

2nd question: as you can see, I have three commands in my command list (which is only a small portion of all my commands,) if I put all my commands in the list, I only obtain the output of ALL my commands through stdout ONLY when all my commands has finished executing. If my 1st question cannot be done, is there a way to at least obtain the output of each command after each one has been executed instead of receiving them all at once only when all the commands has finished being executed.

推荐答案

要立即查看输出,请不要重定向它:

To see the output immediately, don't redirect it:

from subprocess import Popen, PIPE

p = Popen(['ssh', 'user@hostname'], stdin=PIPE) 
p.communicate(b"""cd ..
echo 1st command
echo 2nd command
echo ...
""")

如果您希望两者都在变量中捕获实时"输出将其显示在终端中,那么解决方案取决于您是否需要处理 stdin/stdout/stderr 并发.

If you want both to capture the "live" output in a variable and to display it in the terminal then the solution depends on whether you need to handle stdin/stdout/stderr concurrently.

如果输入很小并且您想要组合 stdout/stderr,那么您可以一次传递所有命令并逐行读取合并的输出:

If input is small and you want to combine stdout/stderr then you could pass all commands at once and read the merged output line-by-line:

from subprocess import Popen, PIPE, STDOUT

p = Popen(['ssh', 'user@hostname'], stdin=PIPE,
          stdout=PIPE, stderr=STDOUT, bufsize=1) 
p.stdin.write(b"""cd ..
echo 1st command
echo 2nd command
echo ...
""")
p.stdin.close() # no more input

lines = [] # store output here
for line in iter(p.stdout.readline, b''): # newline=b'\n'
    lines.append(line) # capture for later
    print line, # display now
p.stdout.close()
p.wait()

如果您想单独捕获实时"stdout/stderr,请参阅:

If you want to capture "live" stdout/stderr separately, see:

这篇关于在 python 中运行 shell 脚本时捕获它的实时输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆