suppress scapy warning message when importing the module

user857990 picture user857990 · Nov 6, 2012 · Viewed 20.7k times · Source

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?

Answer

halex picture halex · Nov 6, 2012

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 *
...