我想使用python计算ARP应答数据包。 假设我有列表
list=[{"ipdst":"192.168.1.1","macdst":"aa:aa:aa","ipsrc":"192.168.1.2",count:1},
{"ipdst":"192.168.1.4","macdst":"cc:cc:cc","ipsrc":"192.168.1.5",count:1}
,我想添加一个列表中具有不同ipdst,macdst和ipsrc的项目(对于尚未记录在列表中的新数据包)。如果列表中有一个具有相同ipdst,macdst和ipsrc的数据包,我想添加计数+1,因为我想计算从目标发送到源的数据包数量。 我已经尝试过
for item in list
if item["ipdst"]==dst and item["macdst"]==mac and item["ipsrc"]=src:
item["count"]+=1
else
list.append(["ipdst"]==dst,["macdst"]==mac,["ipsrc"]==src
但输出是
[{"ipdst":"192.168.1.1","macdst":"aa:aa:aa","ipsrc":"192.168.1.2",count:1},
{"ipdst":"192.168.1.4","macdst":"cc:cc:cc","ipsrc":"192.168.1.5",count:1},{"ipdst":"192.168.1.1","macdst":"aa:aa:aa","ipsrc":"192.168.1.2",count:2},{"ipdst":"192.168.1.4","macdst":"cc:cc:cc","ipsrc":"192.168.1.5",count:1}]
我希望输出是
{"ipdst":"192.168.1.4","macdst":"cc:cc:cc","ipsrc":"192.168.1.5",count:2"}
(对于将2个数据包发送到源的目的地)
我该怎么做?
答案 0 :(得分:0)
代替每次获取新数据包时都遍历字典中的所有项目,您可以使用字典来跟踪每个目标+源+ mac的计数。
counts = {}
然后,每当您获得新商品时,您只需要在counts字典中增加相应的值,就可以得到商品的计数。
dst_src_mac = item["ipdst"] + item["macdst"] + item["ipsrc"]
if (dst_src_mac in counts):
counts[dst_src_mac] += 1
else:
counts[dst_src_mac] = 1
item['count'] = counts[dst_src_mac]