首页 > 解决方案 > 将参数传递给 Scapy 的 Automaton.run() 方法

问题描述

我正在尝试使用 Scapy 的 Automaton 类创建一个自动机来解析不同的数据包。

为了做到这一点,我需要能够将数据包作为参数传递给自动机。一种方法是在创建自动机时传递数据包:

my_automaton = MyAutomaton(packet)

此参数将由parse_args自动机代码中重载的函数处理:

class MyAutomaton(Automaton):
  def parse_args(self, pkt, **kargs):
    Automaton.parse_args(self, **kargs)
    self.pkt = pkt
  ... REST OF CLASS ...

如果我为每个传入的数据包创建一个新的自动机,这会很好地工作。

但我只想创建一个自动机并使用不同的数据包运行它。就像是:

my_automaton = MyAutomaton()
my_automaton.run(pkt1)
my_automaton.run(pkt2)

根据文档,这应该是可能的(链接):

The parse_args() method is called with arguments given at __init__() and run(). Use that to parametrize the behaviour of your automaton.

通过在调用该方法时打印到控制台,parse_args我验证了它确实在自动机创建和调用该run方法时被调用。

但我似乎无法通过run函数传递任何参数,我在这里错过了什么?

标签: pythonscapy

解决方案


如文档所示,您需要在初始化自动机时传递参数:

>>> TFTP_read("my_file", "192.168.1.128").run()

在你的情况下,那将是

my_automaton = MyAutomaton(pkt1)
my_automaton.run()
my_automaton2 = MyAutomaton(pkt2)
my_automaton2.run()

推荐阅读