如何从python脚本中运行的命令中提取输出?

如何从python脚本中运行的命令中提取输出?,python,raspberry-pi,Python,Raspberry Pi,是否可以从子流程中运行的命令中提取实时输出 我想在脚本中使用名为wifite()的程序的输出。Wifite应该在我的脚本中的子进程中运行——在特定的时间Wifite将输出它的扫描结果,并每秒钟更新一次(或类似的时间)。我想有这一行的输出,而它运行,以显示在我的树莓皮Adafruit液晶显示器 所以我想这样做: wifite_scan = subprocess.Popen('./wifite.py', shell=True, stdout =PIPE) wfite_scanOut= wifite_

是否可以从子流程中运行的命令中提取实时输出

我想在脚本中使用名为wifite()的程序的输出。Wifite应该在我的脚本中的子进程中运行——在特定的时间Wifite将输出它的扫描结果,并每秒钟更新一次(或类似的时间)。我想有这一行的输出,而它运行,以显示在我的树莓皮Adafruit液晶显示器

所以我想这样做:

wifite_scan = subprocess.Popen('./wifite.py', shell=True, stdout =PIPE)
wfite_scanOut= wifite_scan.communicate()
lcd.message(wifite_scanOut)
但这是行不通的(如果我错了就纠正我)

…要在我的lcd上实时获取此输出的最后一行,请执行以下操作:


此输出每5秒更新一次,并在控制台中以新值发布相同的输出。所以我需要一种方法,在一个变量中每5秒获取最后一行

实现lcd实时输出的最佳方式是什么

试过吗?我已经成功地将其用于各种软件

示例-使用mysql客户端计算
测试的表数(必须至少有一个表才能不中断):


调用
expect(expr)
后,您可以使用
child.before
child.buffer
检查缓冲区的内容,我不确定它是否适合您的问题(因为我不知道下标的输出是什么样子),但这可能是一个解决方案:

import subprocess

wifite_scan = subprocess.Popen('wifite.py', shell=True, stdout=subprocess.PIPE)
while True:
    output = wifite_scan.stdout.readline()
    if output == '':
        # end of stream
        print("End of Stream")
        break
    if output is not None:
        # here you could do whatever you want with the output.
        print("Output Read: "+output)
我用一个简单的文件尝试了上面的脚本,该文件生成了一些输出:

# wifite.py
import time

for i in range(10):
    print(i)
    time.sleep(1)

为我工作。

欢迎来到Stackoverfow!如果您包括您的输入、您尝试过的内容、您的预期输出与实际输出以及您收到的任何错误的完整堆栈跟踪,您将大大增加获得问题答案的机会。您还可以读取可能的副本而不是副本。我想获得在正常控制台中运行./wifite.py时可以看到的实时输出。它不能正常工作,不能显示整个输出,并且剪切得非常奇怪。你能告诉我如何从这些子进程中提取输出吗?我在文档中看到的是,你可以用它控制python上的程序。
import subprocess

wifite_scan = subprocess.Popen('wifite.py', shell=True, stdout=subprocess.PIPE)
while True:
    output = wifite_scan.stdout.readline()
    if output == '':
        # end of stream
        print("End of Stream")
        break
    if output is not None:
        # here you could do whatever you want with the output.
        print("Output Read: "+output)
# wifite.py
import time

for i in range(10):
    print(i)
    time.sleep(1)