无法从scapy数据包中提取单个字段

时间:2016-10-12 16:39:55

标签: python python-3.x attributes scapy arp

我一直在尝试使用scapy和Python 3,我想使用ARP协议来查找网络上计算机的mac地址。这是我的代码:

>>> packet = srp(Ether(dst="ff:ff:ff:ff:ff:ff")/ARP(pdst=IP_OF_HOST))

然后从这个数据包中提取数据我使用了以下行:

>>> packet[0][Ether].src

但由于某种原因,这会产生错误:

AttributeError: 'list' object has no attribute 'src'

我读过的每个教程都使用了我用来提取字段数据的方法,为什么它对我不起作用?

1 个答案:

答案 0 :(得分:0)

它与函数srp()和srp1()之间的区别有关(那些用于网络层2,对于第3层,你将分别使用sr()和sr1())。

srp()发送数据包,并保存所有数据包,无论它们是否得到应答。要说出源MAC地址,我会这样做:

answered_packets, unanswered_packets = packet
for packet_i_sent, packet_i_received in answered_packets:
    print(packet_i_received.src)

srp1()发送数据包,等待一个应答,并仅保存应答数据包。这意味着格式不同,因为您不必处理未答复的数据包,我以前的方法可以工作:

print(packet.src)     #The [Ether] part doesn't matter, 
                      #that's just for looking fancier when you print(packet)

所以基本上我使用命令srp()并尝试解码回复,就像我使用的是srp1()