简体   繁体   English

遍历Scapy中图层的字段

[英]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: 我想遍历诸如srcdst之类的字段的名称,并且还具有它们的值。

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: 首先,从协议类型中获取所有字段名称,然后使用getattr从数据包/帧中获取值:

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: fields将相等:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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