How to Install python-nmap Library in Linux

In this tutorial, I will teach you how to install a python library which helps in using nmap port scanner. The library is called python-nmap.

What is nmap

"Nmap (Network Mapper) is a security scanner originally written by Gordon Lyon (also known by his pseudonym Fyodor Vaskovich)[1] used to discover hosts and services on a computer network, thus creating a "map" of the network. To accomplish its goal, Nmap sends specially crafted packets to the target host and then analyzes the responses."

Read more about nmap on wiki page and Nmap Commands.

Install python-nmap in linux

1. Open a new terminal and use the wget utility to download the python-nmap library.


how to install python-nmap in linux

2. Once the download is finished extract the content with the tar utility.

tar xf python-nmap-0.1.4.tar.gz

how to install python-nmap in linux

cd python-nmap-0.1.4

how to install python-nmap in linux

python install

how to install python-nmap in linux

Verify if the python-nmap library is installed properly.

how to install python-nmap in linux

how to install python-nmap in linux

On Ubuntu and Debian distributions install python-nmap, use:

sudo apt-get update
sudo apt-get install python-nmap

How to use python-nmap

1. Open a new terminal, and run python with the following command.


2. Import the nmap module.

import nmap
test = nmap.PortScanner()

3. Use the following line to scan your localhost for opening ports

test_scanner = test.scan('','80')

4. Print the test_scanner variable

>>> test_scanner
{'nmap': {'scanstats': {'uphosts': u'1', 'timestr': u'Fri Dec 20 21:33:55 2013', 'downhosts': u'0', 'totalhosts': u'1', 'elapsed': u'0.12'}, 'scaninfo': {u'tcp': {'services': u'80', 'method': u'syn'}}, 'command_line': u'nmap -oX - -p 80 -sV'}, 'scan': {u'': {'status': {'state': u'up', 'reason': u'localhost-response'}, 'hostname': u'localhost', u'tcp': {80: {'state': u'closed', 'reason': u'reset', 'name': u'http'}}}}}

As you can see from the above output a nested dictionary is printed on the screen. It has information about the host status, command-line arguments and port state.

5. Use the following piece of code to get information about the command used by nmap in our example.


Here is the output.

u'nmap -oX - -p 80 -sV'


In this article, we learned how to install python nmap on Linux. This library helps network administrators to scanning tasks and nmap scripts. Please let us know your suggestions.

Oltjano Terpollari 3:00 am


Your email address will not be published. Required fields are marked *

1 Comment

  1. File "", line 1, in
    File "/usr/local/lib/python2.7/dist-packages/nmap/", line 137, in __init__
    raise PortScannerError('nmap program was not found in path')
    nmap.nmap.PortScannerError: 'nmap program was not found in path'