0

我正在使用 pyshark 来解析 pcap 文件。我想使用变量访问层字段,如下面的简单示例所示:
例如访问 ntp 服务器 ip:

p = cap[0]
print(p.bootp.option_ntp_server)

但是,我想像这样访问它:

option_list = {
    "12": "option_hostname",
    "60": "option_vendor_class_id",
    "43": "option_ntp_server"
    }

p = cap[0]
print(p.bootp.%s %(option_list["43"]))

当然,这种访问是不可能的。所以我尝试这样使用getattr()

getOption = getattr(p.bootp, option_list["43"])
getOption()

它给了我以下错误:

'LayerFieldsContainer' object is not callable

似乎 pyshark 数据包或层类是不可调用的。

如何使用变量访问图层字段?或者您能建议我另一种使用选项类型编号访问选项的方法吗?因为我想使用选项类型编号(如选项 12、选项 43)访问此字段,而不是使用标题。

4

1 回答 1

0

我认为 OBJ pyshark.packet.fields.LayerFieldsContainer 具有特殊格式,不能将其用作字符串。dir (LayerFieldsContaniner) 有:

['__add__', '__class__', '__class__', '__contains__', '__delattr__', '__delattr__', '__dict__', '__dir__', '__doc__', '__doc__', '__eq__', '__format__', '__format__', '__ge__', '__getattr__', '__getattribute__', '__getattribute__', '__getitem__', '__getnewargs__', '__getslice__', '__getstate__', '__getstate__', '__gt__', '__hash__', '__hash__', '__init__', '__init__', '__le__', '__len__', '__lt__', '__mod__', '__module__', '__module__', '__mul__', '__ne__', '__new__', '__new__', '__reduce__', '__reduce__', '__reduce_ex__', '__reduce_ex__', '__repr__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__setattr__', '__setstate__', '__setstate__', '__sizeof__', '__sizeof__', '__slots__', '__str__', '__str__', '__subclasshook__', '__subclasshook__', '__weakref__', '_formatter_field_name_split', '_formatter_parser', 'add_field', 'all_fields', 'alternate_fields', 'base16_value', 'binary_value', 'capitalize', 'center', 'count', 'decode', 'encode', 'endswith', 'expandtabs', 'fields', 'find', 'format', 'get_default_value', 'hex_value', 'hide', 'index', 'int_value', 'isalnum', 'isalpha', 'isdigit', 'islower', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'main_field', 'name', 'partition', 'pos', 'raw_value', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'show', 'showname', 'showname_key', 'showname_value', 'size', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'unmaskedvalue', 'upper', 'zfill']

你可以使用它。

于 2018-05-08T04:39:12.777 回答