2

我有一个在终端上运行良好的命令:

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

这没用。也许在列表中编写命令有些麻烦。

我收到错误:

gooman@ubuntu:~/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
4

1 回答 1

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

也许,需要添加 stdout=subprocess.PIPE 参数。

于 2014-03-04T12:56:30.460 回答