代码之家  ›  专栏  ›  技术社区  ›  Stanford Martinez

获取错误:AttributeError:模块“nmap”没有属性“PortScanner”

  •  0
  • Stanford Martinez  · 技术社区  · 4 年前

    我一直在寻找如何解决Python问题的答案:

    AttributeError:模块“nmap”没有属性“PortScanner”

    我想了解更多关于端口扫描的信息,但我甚至无法在我正在使用的Visual Studio代码上安装该模块。我尝试了我和许多人能想到的一切:

    1. 卸载并重新安装了python nmap以及nmap(因为它们是相互连接的)。
    2. 我试着重新命名模块本身。
    3. 我已经在不同的IDE上发布了我的代码
    4. 我已经创建了一个单独的文件夹,并将模块和我的项目放在那里。

    到目前为止没有成功。。

    这是我的代码:

    import nmap
    
    
    nm = nmap.PortScanner()
    nm.scan('127.0.0.1', '22-443')
    

    以及输出:

    /usr/local/bin/python3 /Users
    /user2132/Desktop/PYTHONProjects/portscannning.py
    Traceback (most recent call last):
    File "/Users/user2132/Desktop/PYTHONProjects/portscannning.py", line 3, in <module>
    nm = nmap.PortScanner()
    AttributeError: module 'nmap' has no attribute 'PortScanner'
    

    下一步我能试试什么?

    另外,我正在使用MacOS

    0 回复  |  直到 4 年前
        1
  •  0
  •   gremur    4 年前

    我能够重现这个错误。问题在于 nmap 图书馆 pip install nmap 安装 nmap python library python-nmap 要求 nmap binary 此外 扫描器 python库与 python nmap 因为它们共享相同的模块名。正确的 扫描器 可以从以下位置安装 Nmap's official download page

    请按照以下步骤操作:

    第一步。卸载库

    pip uninstall nmap
    pip uninstall python-nmap
    

    第二步。安装 python nmap

    pip install python-nmap
    

    第三步。检查一下 扫描器 安装到您的系统中

    which nmap
    

    第三步。如果已安装,则继续下一步,如果未安装:

    Nmap的官方下载页面 ,下载并安装操作系统的nmap。

    请确保 add to PATH 选项在安装过程中被选中。

    第四步。重新启动系统(重新启动计算机)

    检查 扫描器 安装 which nmap 终端命令。


    之后你可以检查一下 PortScanner 扫描器 .

    import nmap
    dir(nmap)
    

    退货

    ['ET',
     'PortScanner', <=== IS HERE!
     'PortScannerAsync',
     'PortScannerError',
     'PortScannerHostDict',
     'PortScannerTimeout',
     'PortScannerYield',
     'Process',
     '__author__',
     '__builtins__',
     '__cached__',
     '__doc__',
     '__file__',
     '__last_modification__',
     '__loader__',
     '__name__',
     '__package__',
     '__path__',
     '__spec__',
     '__version__',
     'convert_nmap_output_to_encoding',
     'csv',
     'io',
     'nmap',
     'os',
     're',
     'shlex',
     'subprocess',
     'sys']
    

    期末考试

    import nmap
    nm = nmap.PortScanner()
    nm.scan('127.0.0.1', '22-443')
    

    退货

    {'nmap': {'command_line': 'nmap -oX - -p 22-443 -sV 127.0.0.1',
      'scaninfo': {'tcp': {'method': 'syn', 'services': '22-443'}},
      'scanstats': {'timestr': 'Tue Mar 29 15:07:02 2022',
       'elapsed': '7.82',
       'uphosts': '1',
       'downhosts': '0',
       'totalhosts': '1'}},
    ...