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. For Python 2.x, use python-nmap-0.1.4.tar.gz.
$ wget http://xael.org/norman/python/python-nmap/python-nmap-0.1.4.tar.gz
2. Once the download is finished extract the content with the tar utility.
$ tar xf python-nmap-0.1.4.tar.gz
$ cd python-nmap-0.1.4
$ python setup.py install
Verify if the python-nmap library is installed properly.
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.
python
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('127.0.0.1','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 127.0.0.1'}, 'scan': {u'127.0.0.1': {'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.
test_scanner['nmap']['command_line']
Here is the output.
u'nmap -oX - -p 80 -sV 127.0.0.1'
Conclusion
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.
File "", line 1, in
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'
Traceback (most recent call last):
File "", line 1, in
File "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
Hi David,
Make sure you have installed nmap and have it in $PATH
$ sudo apt install nmap
$ echo $PATH