Question

In my virtualenv I installed python-nmap and nmap is installed (OS X). But if I call mmap like (virtualenv activated...):

import sys
sys.path.append('/usr/local/bin')

import nmap
nm = nmap.PortScanner()

I get the following error:

Raise PortScannerError('nmap program was not found in path')

nmap.nmap.PortScannerError: 'nmap program was not found in path'

Is there still another way to enter the path to nmap?

Was it helpful?

Solution 3

sys.path governs where the Python interpreter looks for importing modules. The "path" in your error is the OS's PATH environment variable, which tells the OS where to look for Nmap. You can either set this directly with os.putenv or you can pass the full path to the nmap binary in the nmap.PortScanner constructor:

nm = nmap.PortScanner('/usr/local/bin/nmap')

OTHER TIPS

Portet the project to Python 3 and update nmap to 0.3.3 now it works.

Thanks a lot!

install nmap with homebrew

brew install nmap

Then your install will work correctly.

I have tried these methods but not worked then I further looked into it.

Error:

>>> import nmap
>>> nm = nmap.PortScanner()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/nmap/nmap.py", line 137, in __init__
raise PortScannerError('nmap program was not found in path')
nmap.nmap.PortScannerError: 'nmap program was not found in path'

so I looked into file "/usr/local/lib/python2.7/dist-packages/nmap/nmap.py". The code where they are checking for nmap is not working for some reason.

code:

# regex used to detect nmap
    regex = re.compile('Nmap version [0-9]*\.[0-9]*[^ ]* \( http://nmap\.org \)')
    # launch 'nmap -V', we wait after 'Nmap version 5.0 ( http://nmap.org )'
    p = subprocess.Popen(['nmap', '-V'], bufsize=10000, stdout=subprocess.PIPE)
    self._nmap_last_output = p.communicate()[0] # store stdout
    for line in self._nmap_last_output.split('\n'):
        if regex.match(line) is not None:
            is_nmap_found = True
            # Search for version number
            regex_version = re.compile('[0-9]+')
            regex_subversion = re.compile('\.[0-9]+')

            rv = regex_version.search(line)
            rsv = regex_subversion.search(line)

            if rv is not None and rsv is not None:
                # extract version/subversion
                self._nmap_version_number = int(line[rv.start():rv.end()])
                self._nmap_subversion_number = int(line[rsv.start()+1:rsv.end()])
            break

    if is_nmap_found == False:
        raise PortScannerError('nmap program was not found in path')

I found out that first regex is not working so I changed this:

from :

regex = re.compile('Nmap version [0-9]*\.[0-9]*[^ ]* \( http://nmap\.org \)')

to:

regex = re.compile('Nmap version [0-9]*\.[0-9]*)

Now it is working just like it suppose to work!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top