8

In the blocking way 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 sum-up: My question is pretty clear, I want to be able to continue executing 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.

Environment details:

python: 2.7

scapy: 2.1.0

os: ubuntu 12.04 64bit

RyPeck
  • 7,830
  • 3
  • 38
  • 58
Michael
  • 938
  • 2
  • 11
  • 18
  • It can be easily done in java with jpcap so I don't see a reason it will not be possible with scapy. – Michael Nov 17 '13 at 09:32

2 Answers2

2

This functionality was added in https://github.com/secdev/scapy/pull/1999. I'll be available with Scapy 2.4.3+ (or the github branch). Have a look at the doc over: 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()
Cukic0d
  • 5,111
  • 2
  • 19
  • 48
0

Scapy doesn't have an async version of the sniff function. You're going to have to fire threads.

There may be other issues with this, mostly having to do with resource locking.

Community
  • 1
  • 1
VooDooNOFX
  • 4,674
  • 2
  • 23
  • 22