gpt4 book ai didi

python - Scapy - 计算嗅探数据包的数量?

转载 作者:行者123 更新时间:2023-12-03 18:41:17 24 4
gpt4 key购买 nike

我如何计算我使用的捕获的数据包数量

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

功能?这可能吗?

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

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

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

最佳答案

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()

关于python - Scapy - 计算嗅探数据包的数量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19794798/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com