tshark 给出无效的捕获过滤器错误

tshark gives invalid capture filter error

我正在尝试使用 python 脚本捕获两台主机之间的数据包。函数如下:

def wire_cap(IP1,IP2,op_fold,file_name,duration):  # invoke tshark to capture traffic during session
    batcmd='"c:\Program Files\Wireshark\tshark.exe" -i 1 src ' + str(IP1) + ' or src '+ str(IP2) +' -a duration:'+str(duration)+' -P -w '+ op_fold+file_name+'.pcap'
    p = subprocess.Popen(batcmd, shell=True,stderr=subprocess.PIPE)
    while True:
        out = p.stderr.read(1)
        if out == '' and p.poll() != None:
            break
        if out != '':
            sys.stdout.write(out)
            sys.stdout.flush()
    thread.exit()

但是,这会产生以下错误:

Capturing on 'Local Area Connection'
tshark: Invalid capture filter "src 172.28.3.87 or src 172.28.3.56 -a duration:40 -P -w C:\Python_Scripts\wire_capture.pcap" for interface 'Local Area Connection'!

That string isn't a valid capture filter (syntax error).
See the User's Guide for a description of the capture filter syntax.
0 packets captured

最初,我认为问题出在接口上,它被传递为'1',但在使用Wireshark 进行检查后,似乎没有问题。我也通过官方文档进行了验证。我通过的每个选项看起来都不错。

我确定我在这里错过了一些东西。收到任何建议的指示真的很有帮助。

你的 tshark 命令是:

c:\Program Files\Wireshark\tshark.exe" -i 1 src 172.28.3.87 or src 172.28.3.56 -a duration:40 -P -w C:\Python_Scripts\wire_capture.pcap

该命令混合了命令行标志参数(以 - 开头的参数)和捕获过滤器参数。捕获过滤器参数必须在 all 命令行标志参数之后,即

c:\Program Files\Wireshark\tshark.exe" -i 1 -a duration:40 -P -w C:\Python_Scripts\wire_capture.pcap src 172.28.3.87 or src 172.28.3.56

或者是命令行标志参数的一部分,即 -f 参数:

c:\Program Files\Wireshark\tshark.exe" -i 1 -f "src 172.28.3.87 or src 172.28.3.56" -a duration:40 -P -w C:\Python_Scripts\wire_capture.pcap

这是 UN*X 命令的标准约定 - 以及使用 UN*X 风格语法的 Windows 命令(这通常意味着源自 UN*X 的命令,如 tshark 所做的,或者是试图保持与 UN*X 命令的兼容性)。

所以试试

    batcmd='"c:\Program Files\Wireshark\tshark.exe" -i 1 -a duration:'+str(duration)+' -P -w '+ op_fold+file_name+'.pcap src ' + str(IP1) + ' or src '+ str(IP2)