导入模块时禁止显示虚假警告消息 [英] suppress scapy warning message when importing the module

查看:68
本文介绍了导入模块时禁止显示虚假警告消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个小脚本,该脚本使用scapy收集一些信息,然后返回一些xml代码,然后将其传递给metasploit的xmlrpc接口.我希望我的脚本只返回xml,而没有其他警告等.

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.

我可以通过在我的sr1命令中添加选项verbose=0来抑制大多数漏斗输出.在每次输出之前,我仍然会得到以下信息,并且我认为在加载模块时它会返回此警告:

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:

警告:找不到IPv6目标::的路由((没有默认路由?)

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

但是我想将其合并到脚本中.为此,我发现了一个提示,即可以有一个NullDevice类,不编写任何内容,然后将sys.stderr设置为该NullDevice类的实例.

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.

不幸的是,这仅在加载模块后才有效,因此我仍然收到警告,并且它仅重定向以下任何发送到stderr的消息.

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?

推荐答案

您可以通过添加以下内容来摆脱警告:

You can get rid of warnings by scapy by adding:

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

之前导入Scapy.这将抑制所有严重性比错误消息低的消息.

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

例如:

import logging
logging.getLogger("scapy.runtime").setLevel(logging.ERROR)
from scapy.all import *
...

这篇关于导入模块时禁止显示虚假警告消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆