I am attempting to continuously sniff packets while concurrently saving them to a PCAP file using PyShark’s LiveCapture
method with the display_filter
param. I am attempting to replicate the feature from Wireshark where you can stop and save a capture at any given moment with any filter specified. This setup in python would involve an indefinite timeout and no restriction on packet counts, allowing a process interruption (such as a keyboard interrupt) to halt the process. Here is an example with try/catch where I can print out packets with no problem:
import pyshark
interf = "Wi-Fi"
capture = pyshark.LiveCapture(interface=interf, display_filter='tcp')
try:
for packet in capture.sniff_continuously():
print(packet)
except KeyboardInterrupt:
print("Capture stopped.")
And now after adding the param for output_file, nothing happens:
import pyshark
interf = "Wi-Fi"
capture = pyshark.LiveCapture(interface=interf, display_filter='tcp', output_file="HERE.pcap")
try:
for packet in capture.sniff_continuously():
print(packet)
except KeyboardInterrupt:
print("Capture stopped.")