Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

suppress scapy warning message when importing the module

I'm writing a small script, that gathers some information using scapy and then returns some xml code, that I'll pass on to the xmlrpc interface of metasploit. I'd like it that my script only returns xml, and no additional warnings etc.

I can suppress most scapy output, with adding the option verbose=0 to my sr1 command. What I still get before every output, and I assume it returns this warning when I'm loading the module, is:

WARNING: No route found for IPv6 destination :: (no default route?)

I can easily redirect that output, by calling my script like this:

 ./myscript 2> /dev/null

but I'd like to incorporate this into the script. For that I've found a hint, that one could have a NullDevice class, that doesn't write anything, and then set sys.stderr to an instantiation of that NullDevice class.

This only works unfortunately after I've already loaded the module, so I still have the Warning, and it only redirects any following messages sent to stderr.

How can I suppress that warning message to appear on my screen?

like image 513
user857990 Avatar asked Nov 06 '12 10:11

user857990


2 Answers

You can get rid of warnings by scapy by adding:

logging.getLogger("scapy.runtime").setLevel(logging.ERROR)

before importing Scapy. This will suppress all messages that have a lower level of seriousness than error messages.


for example:

import logging
logging.getLogger("scapy.runtime").setLevel(logging.ERROR)
from scapy.all import *
...
like image 58
halex Avatar answered Nov 05 '22 05:11

halex


I think this is the correct way.

>>> import sys
>>> sys.stderr = None            # suppress stderr
>>> from scapy.all import *
>>> sys.stderr = sys.__stderr__  # restore stderr
>>> print("other errors can be shown", file=sys.stderr)
other errors can be shown
>>> 
like image 39
Joel Maatkamp Avatar answered Nov 05 '22 06:11

Joel Maatkamp