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 some examples.

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

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'

About Oltjano Terpollari

Oltjano Terpollari is a very passionate computer geek studying python, linux, netcat power tools and living a binary life. He goes by the nickname Ambition and is very happy living a science life. He also loves technical blogging and sharing his knowledge with others.

Author Archive Page

Have anything to say?

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

All comments are subject to moderation.