('Nmap not found', )

2020-02-11 07:09发布

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

标签: python
7条回答
成全新的幸福
2楼-- · 2020-02-11 07:45

Under windows, you must first install nmap for windows, you can install with this link https://nmap.org/download.html

查看更多
3楼-- · 2020-02-11 07:49

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.

查看更多
来,给爷笑一个
4楼-- · 2020-02-11 07:49

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
查看更多
霸刀☆藐视天下
5楼-- · 2020-02-11 07:58

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.

查看更多
叛逆
6楼-- · 2020-02-11 07:58

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.

查看更多
小情绪 Triste *
7楼-- · 2020-02-11 08:05

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)
查看更多
登录 后发表回答