RatDon RatDon - 5 months ago 58
Python Question

Python supress warnings while creating a empty pcap file with Scapy

I want to create an empty

pcap
file. I'm using
wrpcap
module of 'Scapy'. But
wrpcap
takes exactly
2 arguments
: one is the
file name
and other is the
packet list
like:

wrpcap("my.pcap",my_pkt_list)


Since I want to make it, empty and I don't have a packet list, I'm writing an empty string to the
pcap
file. Which is creating the file but also giving a warning as well as errors since a string doesn't match to a packet type.

WARNING: PcapWriter: unknown LL type for str. Using type 1 (Ethernet)
Traceback (most recent call last):
File "test.py", line 35, in <module>
wrpcap("pcap/FU.pcap","")
File "/usr/lib/python2.7/site-packages/scapy/utils.py", line 466, in wrpcap
PcapWriter(filename, *args, **kargs).write(pkt)
File "/usr/lib/python2.7/site-packages/scapy/utils.py", line 646, in write
self._write_packet(pkt)
File "/usr/lib/python2.7/site-packages/scapy/utils.py", line 688, in _write_packet
sec = int(packet.time)
AttributeError: 'str' object has no attribute 'time'


For now, I'm able to suppress the errors with
try and except
but unable to suppress the warning.

Code

from scapy.all import *
try:
wrpcap("my.pcap","")
except:
pass


and the warning is still there:

WARNING: PcapWriter: unknown LL type for str. Using type 1 (Ethernet)


How to suppress it from inside the python code?

Answer

You can avoid that warning by using PcapWriter method of scapy.

from scapy.all import *
try:
    writer=PcapWriter("my.pcap")
except:
    pass

This creates your empty pcap file. When you want to write some packets to it, just use the following code:

writer.write(<Your_packets>)
writer.flush()