python中的Nmap模块问题

时间:2014-02-04 00:39:54

标签: python python-3.x module nmap

我已经安装了nmap.exe和nmap模块。我不知道如何配置nmap路径。 输入nmap路径的代码块如下

class PortScanner(object):
"""
PortScanner class allows to use nmap from python

"""

def __init__(self, nmap_search_path=('nmap','/usr/bin/nmap','/usr/local/bin/nmap','/sw/bin/nmap','/opt/local/bin/nmap') ):
    """
    Initialize PortScanner module

    * detects nmap on the system and nmap version
    * may raise PortScannerError exception if nmap is not found in the path

    :param nmap_search_path: tupple of string where to search for nmap executable. Change this if you want to use a specific version of nmap.
    :returns: nothing

    """
    self._nmap_path = 'C:/Program Files (x86)/Nmap/'   # nmap path
    self._scan_result = {}
    self._nmap_version_number = 0       # nmap version number
    self._nmap_subversion_number = 0    # nmap subversion number
    self._nmap_last_output = ''  # last full ascii nmap output
    is_nmap_found = False       # true if we have found nmap

    self.__process = None

    # regex used to detect nmap
    regex = re.compile('Nmap version [0-9]*\.[0-9]*[^ ]* \( http://.* \)')
    # launch 'nmap -V', we wait after 'Nmap version 5.0 ( http://nmap.org )'
    # This is for Mac OSX. When idle3 is launched from the finder, PATH is not set so nmap was not found
    for nmap_path in nmap_search_path:
        try:
            p = subprocess.Popen([nmap_path, '-V'], bufsize=10000, stdout=subprocess.PIPE)
        except OSError:
            pass
        else:
            self._nmap_path = nmap_path # save path 
            break
    else:
        raise PortScannerError('nmap program was not found in path. PATH is : {0}'.format(os.getenv('PATH')))            

我将路径放在self._nmap_path变量中。但是,它似乎不起作用。 任何有nmap经验的人都可以帮助我吗?我如何开始使用nmap?我已经研究了几个小时,但仍然没有得到答案。 我收到的错误是

Traceback (most recent call last):
  File "<pyshell#1>", line 1, in <module>
    nmap.PortScanner()
  File "C:\Python33\Lib\site-packages\nmap\nmap.py", line 192, in __init__
    raise PortScannerError('nmap program was not found in path')
nmap.PortScannerError: 'nmap program was not found in path'

1 个答案:

答案 0 :(得分:0)

您的环境路径看起来没有正确设置。

如果您打开C:\Python33\Lib\site-packages\nmap\nmap.py文件进行编辑,请查看第192行。它在哪里?

使用自安装程序重新安装可能是值得的,安装程序应该为您设置路径变量。

http://nmap.org/book/inst-windows.html