我想抓取并打印ping请求的源地址。 我有以下脚本:
pkt = sniff(filter="icmp", timeout =15, count = 15)
if pkt[ICMP].type == '8':
print pkt[IP].src
当数据包到达时脚本崩溃
AttributeError:'list' object has no attribute 'type'
然而,在scapy控制台上,我可以清楚地看到它存在!
>>>packet=IP()/ICMP()/"AAAAAA"
>>>packet[ICMP].type
8
>>>
有什么想法吗?
为了测试目的(!),我将脚本更改为以下内容:
pkts=sniff(filter="icmp", timeout=120,count=15)
for packet in pkts:
if packet.haslayer(IP) and str(packet.getlayer(IP).src)=="127.0.0.1"
print "packet arrived"
if packet.haslayer(ICMP) and str(packet.getlayer(ICMP).type)=="8":
print(packet[IP].src)
执行ping操作后的上述内容:
ping localhost -c 3
产生以下尴尬的结果:
packet arrived
127.0.0.1
packet arrived
127.0.0.1
packet arrived
packet arrived
packet arrived
127.0.0.1
packet arrived
127.0.0.1
packet arrived
packet arrived
packet arrived
127.0.0.1
packet arrived
127.0.0.1
packet arrived
我们可以忽略"到达的数据包"多次,因为其他数据包也到达我的主机。但是当我发送3个回应请求时,为什么我看到127.0.0.1的6倍?即使我删除for循环,也会发生相同的结果。
答案 0 :(得分:1)
sniff()
返回的内容不是数据包列表,即使您可以迭代它,就好像它是一个列表一样。见下面的例子:
>>> from scapy.all import *
>>> pkts = sniff(count = 15)
>>> pkts
<Sniffed: TCP:4 UDP:4 ICMP:0 Other:7>
>>> pkts[TCP]
<TCP from Sniffed: TCP:4 UDP:0 ICMP:0 Other:0>
>>>
如果sniff()
只返回了一个数据包列表,那么您示例中的pkt[ICMP]
将无法正常工作。 pkt[ICMP]
的作用是检索pkt
中所有ICMP数据包的列表。