2014-03-02 33 views
2

我有一个在终端的伟大工程的命令:Python的子进程输出读取错误

sudo tshark -V -l -i "any" -f 'udp port 4729' 

我试图读取从我的python脚本输出:

import subprocess 
command = ['tshark', '-V', '-l', '-i', '"any"', '-f', '"udp port 4729"'] # the shell command 
process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=None) 
output, error = process.communicate() 
print output 

它不工作。在列表中编写命令可能会带来一些麻烦。

我收到的错误:

[email protected]:~/workspace/glade_tests/src$ sudo ./main.py 
tshark: Lua: Error during loading: 
[string "/usr/share/wireshark/init.lua"]:45: dofile has been disabled 
Running as user "root" and group "root". This could be dangerous. 
Capturing on "any" 
tshark: The capture session could not be initiated (No such device exists). 
Please check to make sure you have sufficient permissions, and that you have the proper interface or pipe specified. 
0 packets captured 
+0

如果您将sudo添加到列表'['sudo','tshark',...]'并将'stderr = None,shell = True'添加到'Popen',会发生什么? – User

+0

为什么使用'subprocess.Popen'而不是简单的'subprocess.call'?如果你使用'subprocess.call',会发生什么?另外,请注意'Popen'启动一个[新进程(在哪个目录?)](http://docs.python.org/2/library/subprocess.html),所以使用'call'调试应该更容易。 – smci

+0

尝试'output = subprocess.check_output(command)'。 –

回答

0

你的这个:

import subprocess 

command = "sudo tshark -V -l -i "any" -f 'udp port 4729'" 
try: 
    output = subprocess.check_output(command, shell=True) 
except subprocess.CalledProcessError as e: 
    print "An error has been occured", e 
    raise 

print "The subprocess output:", output 

也许,这将需要添加标准输出= subprocess.PIPE说法。

相关问题