我是python的新手,喜欢执行命令并将其输出捕获到变量中。下面的代码运行良好,但问题是它给我的是逐行输出,而不是整个数据包。
我试图寻找替代方案,但没有从谷歌获得足够的信息来应对这种特定的情况。
import subprocess
command='tcpdump -nqt -s 0 -A -i eth0 port 5060'
process = subprocess.Popen([command], stdout=subprocess.PIPE, shell=True)
var = None
while True:
nextline = process.stdout.readline()
print "Output (%s)" % nextline
欢迎所有建议。
您可以使用Popen.communicate((方法(https://docs.python.org/3/library/subprocess.html#subprocess.Popen.communicate):
import subprocess
command='tcpdump -nqt -s 0 -A -i eth0 port 5060'
process = subprocess.Popen([command], stdout=subprocess.PIPE, shell=True)
var,stde = process.communicate()
print var
print stde #this print stderrror value.