Open In App

How to Install python-nmap Library on Linux?

Improve
Improve
Like Article
Like
Save
Share
Report

Python-Nmap is a network mapper that is exceptionally vital in handling security auditing and discovering the network. It exists as an open-source project; thus it is available for free.  

All the core operating systems support Python-Nmap. However, the binary packages are only available for Windows, Linux, and Mac OS X. Nmap is essential in port scanning tasks by manipulating Nmap scanning results programmatically. Here we are going to see how to install python-nmap Library in Linux.

Critical features of Nmap worth diving into include:

  • Os Detection – This involves identifying the host Operating System as well as the network device’s hardware characteristics
  • Port Scanning – Nmap can count and list the entire target and open ports
  • Scriptable interaction with the target – In Nmap, we can write scripts that carry out operations on the networking devices with Nmap Scripting Engine combined with Lua programming languages.
  • Version Detection – We can establish the application version number and application name by interrogating the network services located on the remote devices.
  • Host Discovery – This is the concept when we want to find out the hosts in a particular network.  For instance, when we want to find out if specific ports are open. Or when they make ICMP requests. Besides, Nmap can also list hosts responding to TCP.

There are two varied ways to test the Nmap methods:

Method 1: The first approach is writing scripts in python and running them on the terminal using the following command.

root@gfg:~# python script_name.py
# script_name.py

import nmap
nmap =nmap.PortScanner()
host = '127.0.0.1'
nmap.scan(host, '1-10')
print(nmap.command_line())
How to Install python-nmap Library in Linux

testing Nmap methods using a script

Method 2: The second approach involves testing the Nmap methods over a command-line interface. To achieve this, open the terminal and run the command “python” so that you get an interface like the one below.

How to Install python-nmap Library in Linux

Installing Nmap in Ubuntu (Debian):

Run the following command

root@gfg:~# sudo apt-get install nmap

Installing Python-Nmap in Ubuntu (Debian):

We also need the python module called python-nmap. It is responsible for host and service discovery within the computers’ network. It does so by analyzing responses from identified hosts after initializing sending specific packets to these hosts.

There are two ways to install python-nmap. The first involves installation on the terminal while the second one involves downloading the python-nmap library and doing a manual installation.

Method 1: Installing Python-Nmap through the terminal

So we will run the following command:

root@gfg:~# pip install python-nmap

Method 2: Manual installation of Python-Nmap

In this second approach, we will list all the steps necessary to download the python-nmap library till it is ready for use.

Step 1: Download python-nmap library using wget as follows.

root@gfg:~# wget http://xael.org/norman/python/python-nmap/python-nmap-0.1.4.tar.gz
How to Install python-nmap Library in Linux

Download python-nmap library using wget

Step 2: Use tar to extract the contents

root@gfg:~# tar xf python-nmap-0.1.4.tar.gz 
How to Install python-nmap Library in Linux

Use tar to extract the contents

Step 3: Change directory to the extracted contents

root@gfg:~#  cd python-nmap-0.1.4
How to Install python-nmap Library in Linux

Change directory to the extracted contents

Step 4: Installation of Python-Nmap

root@gfg:~#  python setup.py install
How to Install python-nmap Library in Linux

Installation of Python-Nmap

Step 5: Confirm that Python-Nmap is successfully installed and ready for use.

root@gfg:~# python

Confirm that Python-Nmap is successfully installed and ready for use

We also need a virtual environment to separate concerns between the different modules and to differentiate the other projects. This is important if you decide to write python scripts and run them instead of the command-line interface.

The following command will aid in the creation of a virtual environment

python -m venv my_virtual_environment_name

Testing Nmap on the Command-line Interface

Open the terminal by pressing Ctrl + Alt + T on the keyboard. Then, type the word python and press enter on your keyboard

root@gfg:~# python

To test the various Nmap commands, we will first import the Nmap module using the following command.

>>> import nmap

Next, we need to instantiate the Nmap’s port scanner as follows

>>> nmap =nmap.PortScanner()
>>> host = '127.0.0.1'

What’s next is setting both the port and host range to scan as below:

>>> nmap.scan(host, '1-10')

Next, we can print the command_line command used for the scan as shown

>>> print(nmap.command_line())

Using Nmap to get the hostname of 127.0.0.1

>>> nmap['127.0.0.1'].hostname()
'localhost'
>>>

If we want to get scan information on the Nmap, do as follows:

>>> print nmap.scaninfo()

The results will be like:

{'tcp': {'services': '1-10', 'method': 'connect'}}

To scan every host, we need to run the following commands

>>> for host in nmap.all_hosts():
...     print('Host : %s (%s)' % (host, nmap[host].hostname()))
...     print('State : %s' % nmap[host].state())

Similarly, we can also scan for all the protocols. It returns the protocol for the specific network being scanned.

>>> nmap['127.0.0.1'].all_protocols()

The result in our case was:

[tcp]

Get the state of a specific host as follows

>>> nmap['127.0.0.1'].state()

It will be indicated if the host is up or down. In our case,

'up'

Keys() method is responsible for displaying information on all the active ports while providing the range. The following is how to use the keys() method.

>>> nmap['127.0.0.1']['tcp'].keys()
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>>

Validate port information of a given port. For instance, for 20 on the given host is as follows

>>> nmap['127.0.0.1'].has_tcp(20)
False
>>>

Last Updated : 05 Oct, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads