简体   繁体   中英

Can't extract individual fields from scapy packet

I've been experimenting with scapy and Python 3 and I want to use the ARP protocol to find mac addresses of computers on the network. This is my code:

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

Then to extract the data from this packet I used the following line:

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

However for some reason this produces the error:

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

Every tutorial I read used the method I used for extracting field data, why wouldn't it work for me?

It has to do with the difference between the functions srp(), and srp1() (Those being for network layer 2, for layer 3 you would use sr() and sr1() respectively).

srp() sends the packet, and saves all packets, whether they are answered or not. To get say the source MAC address I'd do this:

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

srp1() sends a packet, and waits for one answer, and saves only the reply packet. This means the format is different, since you don't have to deal with the unanswered packets, and my previous method would work:

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

So basically I used the command srp() and tried to decode the reply like I was using srp1()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM