Zero Days
Zero Days

Reputation: 859

('Nmap not found', <class 'nmap.nmap.PortScannerError'>)

Where is the problem?

import nmap

I installed nmap and python, and when I use import nmap there is no any problem. But when use:

nmap.PortScanner()

this error is thrown:

Traceback (most recent call last):
  File "<pyshell#3>", line 1, in <module>
    nmap.PortScanner()
  File "./nmap/nmap.py", line 153, in __init__
    raise PortScannerError('nmap program was not found in path. PATH is:{0}'.format(os.getenv('PATH')))
nmap.nmap.PortScannerError: 'nmap program was not found in path. PATH is : /usr/lib    /lightdm/lightdm:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:    /usr/local/games'"

Upvotes: 11

Views: 36460

Answers (10)

ramenos
ramenos

Reputation: 1

In my case, in a Windows 10 environment, adding os.environ to the python code solved the problem. Like this.

import os

import nmap

os.environ['PATH'] += r";C:\Program Files (x86)\Nmap"

nm = nmap.PortScanner()

nm.scan(hosts='192.168.0.0', ports='1-65535', arguments='-sS')

print(nm.all_hosts())

Upvotes: 0

Ronnie Atuhaire
Ronnie Atuhaire

Reputation: 11

For Windows: I found this helpful:

choco install nmap

You must run this under an elevated command if possibly Powershell I assume you have already done pip install python-nmap

Upvotes: 1

Anurag Pandey
Anurag Pandey

Reputation: 1

I have a perfect solution for this..

First type:- apt-get remove nmap

Then :- apt autoremove

Then :- go to www.pypi.org

And type python nmap and download the 0.6 version

Extract it using command :- tar -zxvf filename

cd to the new extracted file

Type:- python setup.py install

And then

apt-get install nmap

And you are ready to go.

Upvotes: 0

metalheadcoder
metalheadcoder

Reputation: 525

for macOS user simply use brew install nmap instead of using pip

Upvotes: 0

Dennis Jensen
Dennis Jensen

Reputation: 39

Note about nmap

I used nmap to search the mask 192.168.1.0/24, but it didnt seam to find ALL ip´s. Eg: my laptop on 192.168.1.119 wasnt found, so I ended up using a combination of:

def ping(self, ip):
    # Use the system ping command with count of 1 and wait time of 1.
    ret = subprocess.call(['ping', '-c', '1', '-W', '1', ip],
                          stdout=open('/dev/null', 'w'),
                          stderr=open('/dev/null', 'w'))

    return ret == 0 # Return True if our ping command succeeds

inside a multithreaded Pinger

Pinger I got from: http://blog.boa.nu/2012/10/python-threading-example-creating-pingerpy.html

I created my own IpInfo class to store information and search for open ports on each IP, and here I use nmap: (Code is "work in progress", but you will get the idea. Ideas to tune performance would be nice)

class IpInfo(object):
ip = None
hostname = None
ports = []
lastSeenAt = strftime("%Y-%m-%d %H:%M:%S", gmtime())


def findHostName(self):
    if(ip):
        self.hostname = str(socket.gethostbyaddr(ip)[0])
    else:
        raise NameError('IP missing')

def findOpenPorts(self):
    print('findOpenPorts')
    nm = nmap.PortScanner()
    nm.scan(host)
    nm.command_line()
    nm.scaninfo()

    for proto in nm[self.ip].all_protocols():
        print('----------')
        print('Protocol : %s' % proto)

        lport = nm[self.ip][proto].keys()   #<------ This 'proto' was changed from the [proto] to the ['tcp'].
        lport.sort()

        for port in lport:
            if(nm[self.ip][proto][port]['state'] == 'open'):
                self.ports.append(port)

Upvotes: 0

Dennis Jensen
Dennis Jensen

Reputation: 39

Running on Raspberry Pi 3 with Jessy lite

I had to:

sudo apt-get update

sudo apt-get upgrade

then I could:

sudo apt-get install nmap

nmap --version

Upvotes: 0

diogovk
diogovk

Reputation: 2228

python-nmap seems to depend on nmap, which is the binary that does the actual network scanning and auditing.

You can check in a terminal if nmap is in your $PATH with the following command:

 which nmap

Debian-like

You can install nmap in debian-like distros with:

apt-get install nmap

Arch linux:

pacman -Sy nmap

Already installed nmap

If you're sure the nmap binary is installed, but you think it is not in your $PATH, you might have to add the directory where nmap is installed to your $PATH.

To do that, edit the .bashrc file in your user's directory, or /etc/bashrc (which will change for all users) and add the following:

export PATH="$PATH:/usr/local/nmap/bin"

but changing /usr/local/nmap/bin for the directory where the nmap binary is installed. After changing the file, be sure to open a new shell session, or type exec bash to refresh it.

You also have to make sure, that it has execute permission (chmod +x <file>).

When you execute:

nmap --version

You should see something like this:

Nmap version 6.46 ( http://nmap.org )
Platform: i686-pc-linux-gnu
Compiled with: liblua-5.2.3 openssl-1.0.1g libpcre-8.34 libpcap-1.5.3 nmap-libdnet-1.12 ipv6
Compiled without:
Available nsock engines: epoll poll select

If you do, nmap is installed and in your $PATH.

Upvotes: 14

Chad Mx
Chad Mx

Reputation: 1324

For Windows users:

I would suggest first closing all terminals and IDLE or any other window you currently have opened when trying to run your script.

Next, open a command line and type

pip uninstall python-nmap

If you are unsure if Nmap binaries are installed on your current system, do a simple search for

nmap

from your start menu. If it is installed, continue to the next step, if not, go to Nmap's official download page

Download the windows self install and run it. Record the directory it is being installed to.

Go to that directory. For me it was

C:\Program Files (x86)\Nmap

Open your system's environment variables editor usually found in

My PC > System Information > Advance settings > Environment Variables

Or right click

My PC or My Computer or whatever yours is called and select properties then advance settings then Environment Variables at the bottom of the Advanced tab

select Path for both You and the System

press Edit and enter the full path to your Nmap director

eg ;C:\Program Files (x86)\Nmap\

Press ok and exit the editor.

Now go back to your command line and enter: pip install python-nmap Allow it to install and then restart your ide and test your code again.

Upvotes: 15

Numa
Numa

Reputation: 301

Faced similar issue while trying to run nm= nmap.PortScanner()

I tried most of the solutions given above, but they did not work for me. The thing that worked for me was installing nmap for Mac OS X using home brew (Information at: http://brew.sh) and running the command

$ brew install nmap.

Now nm= nmap.PortScanner() runs without the earlier error.

Upvotes: 0

Kangaroo
Kangaroo

Reputation: 11

I have had the same problem. Just type in a terminal:

sudo apt-get install nmap

and problem solved.

Upvotes: 1

Related Questions