Scapy - 计算嗅探包的数量?

时间:2013-11-05 17:13:03

标签: python scapy sniffing

如何计算使用

捕获的数据包数量
 packets = sniff(filter='udp and host fe80::xx:xx:xx:xx',count=0)

功能?这可能吗?

编辑:

我实际上一直在尝试使用这个函数的prn:

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

我在程序开头定义了计数器变量。但我需要将它重置为0,每次嗅探()。我尝试过的任何东西都不会起作用......

1 个答案:

答案 0 :(得分:6)

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()
相关问题