简体   繁体   English

导入模块时禁止scapy警告消息

[英]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. 我正在编写一个小脚本,使用scapy收集一些信息,然后返回一些xml代码,我将传递给metasploit的xmlrpc接口。 I'd like it that my script only returns xml, and no additional warnings etc. 我希望我的脚本只返回xml,没有其他警告等。

I can suppress most scapy output, with adding the option verbose=0 to my sr1 command. 我可以通过向我的sr1命令添加选项verbose=0来抑制大多数scapy输出。 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?) 警告:找不到IPv6目标的路由::(没有默认路由?)

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. 为此,我找到了一个提示,一个可以有一个NullDevice类,它不会写任何东西,然后将sys.stderr设置为该NullDevice类的实例化。

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. 这只在我已经加载模块后不幸地工作,所以我仍然有警告,它只重定向发送到stderr的任何后续消息。

How can I suppress that warning message to appear on my screen? 如何禁止显示在屏幕上的警告消息?

You can get rid of warnings by scapy by adding: 您可以通过添加以下内容来消除scapy的警告:

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

before importing Scapy. 导入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 *
...

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

I think perhaps the python3 version of scapy prints a message from a different logger or at a high level. 我想scapy的python3版本可能会打印来自不同记录器或高级别的消息。 Here's some code I've used to suppress output on module import. 这是我用来抑制模块导入输出的一些代码。

from contextlib import contextmanager

# It looks like redirect_stderr will be part of Python 3.5 as follows:
# from contextlib import redirect_stderr
# Perhaps if you're looking at this code and 3.5 is out, this function could be
# removed.
@contextmanager
def redirect_stderr(new_target):
    """
    A context manager to temporarily redirect stderr. Example use:
    with open(os.devnull, 'w') as f:
        with redirect_stderr(f):
            # stderr redirected to os.devnull. No annoying import messages
            # printed on module import
            from scapy.all import *
    # stderr restored
    """
    import sys
    old_target, sys.stderr = sys.stderr, new_target # replace sys.stdout
    try:
        yield new_target # run some code with the replaced stdout
    finally:
        sys.stderr = old_target # restore to the previous value


# Don't print the annoying warning message that occurs on import
with open(os.devnull, 'w') as errf:
    with redirect_stderr(errf):
        from scapy.all import sr, ICMP, IP, traceroute

With Python3, redefining sys.stderr to None threw an exception AttributeError: 'NoneType' object has no attribute 'write' . 使用Python3, 将sys.stder重新定义为None会引发异常AttributeError:'NoneType'对象没有属性'write' Instead, defining it to os.devnull does the job: 相反,将它定义为os.devnull完成这项工作:

import os
import sys
sys.stderr = os.devnull # suppress stderr
from scapy.all import *
sys.stderr = sys.__stderr__ # restore stderr

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM