3

如何计算使用

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

功能?这可能吗?

编辑:

我实际上一直在尝试将 prn 与此功能一起使用:

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

我在程序开始时定义了计数器变量。但我需要它在我所做的每个 sniff() 中重置为 0。我尝试过的任何东西都不会起作用......

4

1 回答 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()
于 2013-11-05T18:27:09.597 回答