2015-10-07 373 views
0

我想捕获使用python脚本的两台主机之间的数据包。功能如下:tshark给出了无效的捕获过滤器错误

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,似乎没有问题。我也用官方文件进行了验证。我通过的每个选项都很好。

我确定我错过了这里的东西。接收任何暗示的指示将非常有帮助。

回答

1

你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 

该命令混音的命令行标志参数(那些开头-)和捕获过滤器参数。捕获过滤器参数必须要么跟从所有命令行标志的论点,即

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) 
+0

哦。这很有帮助。非常感谢。你的答案完成了这项工作! –

相关问题