Scapy - 统计嗅探的数据包数量?

3

如何通过Wireshark来计算已捕获的数据包数量?

packets = sniff(filter='udp and host fe80::xx:xx:xx:xx',count=0)

函数?这有可能吗?

我一直试图在这个函数中使用prn:

def packetCount(packets): 
    global counter 
    counter += 1 

我在程序开头定义了计数器变量。但是,我需要它在每次执行sniff()时重新设为 0。尝试过的方法都不起作用...

1个回答

9

sniff接受几个可能有用的参数。

>>> print sniff.__doc__
Sniff packets
sniff([count=0,] [prn=None,] [store=1,] [offline=None,] [lfilter=None,] + L2ListenSocket args) -> list of packets

  count: number of packets to capture. 0 means infinity
  store: wether to store sniffed packets or discard them
    prn: function to apply to each packet. If something is returned,
         it is displayed. Ex:
         ex: prn = lambda x: x.summary()
lfilter: python function applied to each packet to determine
         if further action may be done
         ex: lfilter = lambda x: x.haslayer(Padding)
offline: pcap file to read packets from, instead of sniffing them
timeout: stop sniffing after a given time (default: None)
L2socket: use the provided L2socket
opened_socket: provide an object ready to use .recv() on
stop_filter: python function applied to each packet to determine
             if we have to stop the capture after this packet
             ex: stop_filter = lambda x: x.haslayer(TCP)

你可能会发现timeoutcount很有用。
编辑:要找到嗅探的数据包数量,你可以使用len()函数。
len(packets)

for i in range(len(packets)):
    print packets[i].summary()

# or better:
for i in packets:
    print i.summary()

是的,应该适用于你,我没有注意到它。请检查我的更新。 - user1301404
我不理解什么是lambda表达式...但好吧!那么在嗅探完成后,变量保存的是长度?是x还是lambda? - geeoph
1
如果您使用CTRL+C停止嗅探,则可以简单地执行len(p),它将返回嗅探到的数据包数量。p = sniff(count=100) - user1301404
不需要做那么多...只需执行 for i in packets: #do stuff - 在这种情况下,i 是从所有被嗅探的数据包中获取的一个数据包。因此,例如 for i in packets: print i.summary() - user1301404
没问题 :) 如果您觉得答案有用,请考虑接受并递增答案。我会更新我的回答。 - user1301404
显示剩余6条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接