How to Install python-nmap Library on Linux?

05 Oct, 2021
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.
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())
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.
Run the following command
root@gfg:~# sudo apt-get install nmap
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.
So we will run the following command:
root@gfg:~# pip install 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
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 
Use tar to extract the contents
Step 3: Change directory to the extracted contents
root@gfg:~#  cd python-nmap-0.1.4
Change directory to the extracted contents
Step 4: Installation of Python-Nmap
root@gfg:~#  python setup.py install
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
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
>>>
how-to-install
Picked
How To
Installation Guide
Linux-Unix
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Set Git Username and Password in GitBash?
How to Install Jupyter Notebook on MacOS?
How to Install and Use NVM on Windows?
How to Install Python Packages for AWS Lambda Layers?
How to Install Git in VS Code?
Installation of Node.js on Linux
Installation of Node.js on Windows
How to Install Jupyter Notebook on MacOS?
How to Install and Use NVM on Windows?
How to Install Pygame on Windows ?