Python supress warnings while creating a empty pcap file with Scapy
Question:
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?
Answers:
The text “WARNING: …” is not part of the exception and will thus not be surpressed by your try clause.
An idea I got was to redirect stdout during the call
from scapy.all import *
try:
old_stdout = sys.stdout
sys.stdout = None
wrpcap("my.pcap","")
sys.stdout = old_stdout
except:
pass
that should surpress any and all output during the call
Python has a built in method in the standard library for supressing warnings:
import warnings
warnings.simplefilter("ignore", Warning)
You can read more about the warnings library in the docs.
EDIT
It doesn’t look like scapy
uses the warnings
library to generate this. Instead it logs them to a logger called scapy.runtime
with a level of warning
. There’s obviously some default logging config to echo that to stdout
. You could add your own logging handler for scapy.runtime
to supress them.
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()
You can suppress this warning by disabling warning logging before the call wrpcap
:
import logging
logging.getLogger("scapy.runtime").setLevel(logging.ERROR)
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?
The text “WARNING: …” is not part of the exception and will thus not be surpressed by your try clause.
An idea I got was to redirect stdout during the call
from scapy.all import *
try:
old_stdout = sys.stdout
sys.stdout = None
wrpcap("my.pcap","")
sys.stdout = old_stdout
except:
pass
that should surpress any and all output during the call
Python has a built in method in the standard library for supressing warnings:
import warnings
warnings.simplefilter("ignore", Warning)
You can read more about the warnings library in the docs.
EDIT
It doesn’t look like scapy
uses the warnings
library to generate this. Instead it logs them to a logger called scapy.runtime
with a level of warning
. There’s obviously some default logging config to echo that to stdout
. You could add your own logging handler for scapy.runtime
to supress them.
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()
You can suppress this warning by disabling warning logging before the call wrpcap
:
import logging
logging.getLogger("scapy.runtime").setLevel(logging.ERROR)