【问题标题】:suppress scapy warning message when importing the module导入模块时抑制 scapy 警告消息
【发布时间】:2012-10-26 07:20:47
【问题描述】:

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

我可以通过在我的 sr1 命令中添加选项 verbose=0 来抑制大多数 scapy 输出。在每次输出之前我仍然得到的,我假设它在我加载模块时返回这个警告,是:

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

我可以通过这样调用我的脚本轻松地重定向该输出:

 ./myscript 2> /dev/null

但我想将其合并到脚本中。为此,我找到了一个提示,可以有一个 NullDevice 类,它不写任何东西,然后将 sys.stderr 设置为该 NullDevice 类的实例化。

不幸的是,这仅在我已经加载模块后才有效,所以我仍然有警告,它只会将以下任何消息重定向到 stderr。

如何禁止该警告消息出现在我的屏幕上?

【问题讨论】:

    标签: python hide stderr scapy output


    【解决方案1】:

    你可以通过添加 scapy 来消除警告:

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

    导入 Scapy 之前。这将抑制所有严重程度低于错误消息的消息。


    例如:

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

    【讨论】:

      【解决方案2】:

      我认为这是正确的方法。

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

      【讨论】:

        【解决方案3】:

        我认为也许 python3 版本的 scapy 会打印来自不同记录器或更高级别的消息。这是我用来抑制模块导入输出的一些代码。

        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
        

        【讨论】:

          【解决方案4】:

          使用 Python3,redefining sys.stderr to None 抛出异常 AttributeError: 'NoneType' object has no attribute 'write'。相反,将其定义为 os.devnull 就可以了:

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

          【讨论】:

          • scapy 不兼容 python3。你需要 scapy3k
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-02-09
          • 1970-01-01
          • 2018-04-11
          • 1970-01-01
          • 2012-06-20
          相关资源
          最近更新 更多