简体   繁体   中英

Iterating over fields of a layer in Scapy

I want to iterate over the names of the fields such as src and dst and also have their values., I tried this:

for field in pkt['Ethernet']:
    print field

But I need a function that will give me a dictionary with the name of the field and his value, for example -

{'dst':'00:0a:95:9d:68:16','src':'00:0a:95:9d:68:16','type':'tcp'}

First, get all the field names from the protocol type, then use getattr to get the value from the packet / frame:

field_names = [field.name for field in Ether.fields_desc]
fields = {field_name: getattr(frame, field_name) for field_name in field_names}

fields will then be equal:

{'dst': 'ff:ff:ff:ff:ff:ff', 'src': '00:00:00:00:00:00', 'type': 36864}

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