Search code examples
python-3.xscapy

Is there a way to capture packets in background and send packets using scapy?


I am trying to capture incoming packets while sending continues packets in an interface (interval - 1 pps) using scapy. I tried sniff function with different parameters associated with it (ex. prn). But it didn't worked, since sniff not completes the action (sniffinf) to proceed send packets or not capturing packets in background.

My Requirements:

  1. Start capture using scapy
  2. Send traffic/packets
  3. stop capture using scapy

Tried:

sniff(iface="eno2") <-- This listen for packets to capture (Actually Peer will send packets once it receives packets from this interface(control packets exchanges) sendp(pkt, iface="eno2") <--- This command not get executed since sniff execution not complete

Thanks.


Solution

  • You can try AsyncSniffer like Cukic0d stated or you can just create a Python thread which targets your sniffing function.

    import threading
    thread = threading.Thread(target=sniff_def, args=(arg1, arg2,))
    thread.start()