【问题标题】:Check if object is in list given by pyshark检查对象是否在 pyshark 给出的列表中
【发布时间】:2015-12-24 02:38:40
【问题描述】:

我正在尝试使用 pyshark 读取 pcap 文件。似乎 pyshark 创建了一个对象列表,其中每个对象 有关于包内每一层的信息。

我只想评估数据包中是否存在层。也许有人可以帮助我。

对象“层”的列表是这样的:

[<ETH Layer>, <IP Layer>, <SCTP Layer>, <DATA Layer>]

但是这个评估失败了,因为列表里面是对象而不是字符串。

if <ETH Layer> in layers: print "Yes, Ethernet layer exists"
if '<ETH Layer>' in layers: print "Yes, Ethernet layer exists"

以下是我的测试...

Python 2.7.10 (default, May 23 2015, 09:40:32) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> import pyshark
>>> file = pyshark.FileCapture('C:\\files\\input.pcap')
>>> packet=file[0]
>>> layers=packet.layers
>>> layers
[<ETH Layer>, <IP Layer>, <SCTP Layer>, <DATA Layer>, <SCTP Layer>, <DATA Layer>]
>>> if packet.eth in layers: print "Yes, Ethernet layer exists"
...
Yes, Ethernet layer exists
>>>
Yes, Ethernet layer exists
>>> if '<ETH Layer>' in layers: print "Yes, Ethernet layer exists"
...
>>>

这个评估给出了正确的输出

if packet.eth in layers: print "Yes, Ethernet layer exists"

这两个失败,因为评估为 False

if <ETH Layer> in layers: print "Yes, Ethernet layer exists"
if '<ETH Layer>' in layers: print "Yes, Ethernet layer exists"

*更新:

如果我测试 packet.ip、packet.eth、packet.sctp,它只有在层 ip、eth 或 sctp 存在时才有效,如果某些层不存在(即 tcp),我会收到以下错误。

>>> if packet.tcp in layers: print "Yes, Ethernet layer exists"
...
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python27\lib\site-packages\pyshark\packet\packet.py", line 110, in __getattr__
    raise AttributeError()
AttributeError

谢谢

【问题讨论】:

  • 只检查if packet.eth有什么问题?
  • 嗨转,如果我测试 packet.ip、packet.eth、packet.sctp 它只有在层 ip、eth 或 sctp 存在时才有效,如果某些层不存在(即 tcp)。请看我的更新。谢谢。
  • 是的,请看下面我的回答。

标签: python list object


【解决方案1】:

&lt;ETH Layer&gt; 不是实际对象,它只是对象的 __repr__ 方法返回的内容。你的 Python REPL 调用它来查看如何打印出评估表达式的表示。类似地,如果您向print 表达式请求它,它可能会调用__str__。比较这些:

>> packet.layers[0]
<ETH Layer>

>> print packet.layers[0]
Layer ETH:
    Destination: 52:54:00:12:37:02 (52:54:00:12:37:02)
    .... ..1. .... .... .... .... = LG bit: Locally administered address (this is NOT the factory default)
    Address: 52:54:00:12:37:02 (52:54:00:12:37:02)
    .... ...0 .... .... .... .... = IG bit: Individual address (unicast)
    Type: IP (0x0800)
    Source: 08:00:28:1d:ae:8b (08:00:28:1d:ae:8b)
    .... ..0. .... .... .... .... = LG bit: Globally unique address (factory default)
    Address: 08:00:28:1d:ae:8b (08:00:28:1d:ae:8b)
    .... ...0 .... .... .... .... = IG bit: Individual address (unicast)

但要从功能上回答您的问题,我相信如果您想查看数据包中是否存在层,您可以执行以下操作:

if 'eth' in packet:
    # ...

【讨论】:

  • 出色的转身。它似乎工作。我同样是 python 和 pyshark 的新手。那么当你说 时,它会在数据包中搜索字符串“eth”?我只是想弄清楚如何打印或处理层或数据包的属性。再次感谢。
  • 老实说,我不太确定,因为看起来 pyshark 有点模糊了它的结构(大概是为了可用性),但看起来 packet 是一个类似 dict 的对象,你是测试是否有密钥eth
  • 我明白了。非常感谢您的帮助转。
猜你喜欢
  • 1970-01-01
  • 2016-04-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-03
  • 2021-06-03
  • 1970-01-01
  • 2017-11-26
相关资源
最近更新 更多