Scapy的自动机,执行周期2次

时间:2015-02-15 02:44:01

标签: python scapy automata

我写了这段代码:

from scapy.layers.inet import ICMP,IP
from scapy.all import Raw

class GetIP(Automaton):
    def parse_args(self, **kargs):
        Automaton.parse_args(self, **kargs)

    def master_filter(self, pkt):
        return (ICMP in pkt and pkt[ICMP].type==8)

    @ATMT.state(initial=1)
    def BEGIN(self):
        print("begin state")
        raise self.WAITING()

    @ATMT.state()
    def WAITING(self):
        print("waiting state")
        pass

    @ATMT.state()
    def RECIEVED(self):
        print("recieved state")
        pass

    @ATMT.state(final=1)
    def END(self):
        print("END state")
        raise self.BEGIN()

    @ATMT.receive_condition(WAITING)
    def wait_icmp(self,pkt):
        print("wait-receive condition")
        self.client_ip = pkt[IP].src
        raise self.RECIEVED()

    @ATMT.condition(RECIEVED)
    def got_icmp(self):
        print("received condition")
        raise self.END()

    @ATMT.action(got_icmp)
    def post_icmp(self):
        print("action")
        print(self.client_ip)
GetIP().run()

获取ICMP请求以从标头显示IP,但IP被打印两次。

使用命令发送1个数据包:

ping 127.1 -c1

脚本输出:

WARNING: No route found for IPv6 destination :: (no default route?)
begin state 
waiting state 
wait-receive condition 
recieved state
received condition action
127.0.0.1 
END state 
begin state 
waiting state 
wait-receive condition 
recieved state 
received condition action
127.0.0.1 
END state 
begin state 
waiting state

1 个答案:

答案 0 :(得分:0)

Loopback接口非常特殊,Scapy有时会混淆它们。

你应该使用"真实"接口