2

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?

RatDon
  • 3,403
  • 8
  • 43
  • 85
  • Did `python -W ignore file.py`, or any other solution [here](http://stackoverflow.com/questions/14463277/how-to-disable-python-warnings) not work? – Ozgur Vatansever Feb 26 '15 at 06:58
  • Looks like a duplicate of http://stackoverflow.com/questions/11175388/ignoring-python-warnings . This should do the trick: `import warnings` `warnings.simplefilter("ignore", UserWarning)` – RomanK Feb 26 '15 at 07:03
  • @RomanK Thanks for the reference. I tried that but it's not working. do we need to write `UserWarning` or anything else at that place? – RatDon Feb 26 '15 at 07:40
  • @RatDon - Maybe you can use Warning instead of UserWarning? The former is the parent class for all warning types. – RomanK Feb 26 '15 at 08:44

4 Answers4

3

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.

Ben
  • 6,687
  • 2
  • 33
  • 46
2

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()
wonder
  • 885
  • 1
  • 18
  • 32
  • Thanks. It is a nice method I never looked. May be I need to change my title of the question. :P – RatDon Jun 22 '16 at 09:10
1

You can suppress this warning by disabling warning logging before the call wrpcap:

import logging
logging.getLogger("scapy.runtime").setLevel(logging.ERROR)
ecdhe
  • 11
  • 1
0

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

Johan
  • 1,633
  • 15
  • 22
  • The idea is good. But will not suppress any other outputs of the program? – RatDon Feb 26 '15 at 07:44
  • It will disable all output to stdout during the call to wrpcap , so if you have several processes their output will have their output be surpressed aswell. – Johan Feb 26 '15 at 08:05