为什么python-tcpdum命令无法捕获文件中的数据包

时间:2015-05-25 22:05:07

标签: python tcpdump

我有以下用Python编写的TcpDump命令但它没有给我任何带有请求数据包的输出文件,尽管我在我的Ubuntu VM上安装并测试了TcpDump:

    command = 'sudo /usr/sbin/tcpdump -i eth1 {} -c {} -s 0  -w {}'\
            .format( 'tcp host 10.0.2.15','30000',
                    '/home/results/xyz.pcap')

1 个答案:

答案 0 :(得分:0)

cat test.py
  import os
  command = '/usr/sbin/tcpdump -i eth1 {} -c {} -s 0  -w {}'.format( 'host 192.168.254.74','30000','res.pcap')
  print(command)
  os.system(command)

sudo python test.py
  /usr/sbin/tcpdump -i eth1 host 192.168.1.10 -c 30000 -s 0 -w res.pcap
  tcpdump: listening on eth1, link-type EN10MB (Ethernet), capture size 10 bytes
  ^C0 packets captured
  6 packets received by filter
  0 packets dropped by kernel

ls -l | grep test
  -rw------- 1 admin admin        155 Dec  2 23:05 test.py

似乎对我来说很好。 测试文件是“ test.py”。我在sudo下运行它,过了一段时间后退出。我可以看到捕获了6个数据包,文件大小> 0。 确保命令本身在python外部正常运行。