Scapi sniffs in a non-blocking way

In blocking mode, I can do this:

from scapy.all import * sniff(filter"tcp and port 80", count=10, prn = labmda x:x.summary()) # Below code will be executed only after 10 packets have been received do_stuff() do_stuff2() do_stuff3() 

I want to be able to sniff packets with scapy in a non-blocking way, something like this:

 def packet_recevied_event(p): print "Packet received event!" print p.summary() # The "event_handler" parameter is my wishful thinking sniff(filter"tcp and port 80", count=10, prn=labmda x:x.summary(), event_handler=packet_received_event) #I want this to be executed immediately do_stuff() do_stuff2() do_stuff3() 

To summarize: my question is pretty clear, I want to be able to continue executing the code without the sniff function blocking it. One option is to open a separate thread for this, but I would like to avoid it and use scapy native tools, if possible.

Environmental Information:

python: 2.7

scapy: 2.1.0

os: ubuntu 12.04 64bit

+7
sniffing scapy
source share
2 answers

Scapy does not have an asynchronous version of the sniff function. You will have to start threads.

other problems , mainly related to resource locking.

+1
source share

This functionality was added at https://github.com/secdev/scapy/pull/1999 . I will be available with Scapy 2.4. 3+ (or with github branch). Check out the document: https://scapy.readthedocs.io/en/latest/usage.html#asynchronous-sniffing.

 >>> t = AsyncSniffer(prn=lambda x: x.summary(), store=False, filter="tcp") >>> t.start() >>> time.sleep(20) >>> t.stop() 
0
source share

All Articles