How to Install python-nmap Library on Linux?
Last Updated :
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.
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())

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.

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

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
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
>>>
Similar Reads
How to Install Python-MIDI Library on Linux?
The MIDI library in Python is used to manipulate MIDI data. This library or package is cross-platformed as it can work with various operating systems like Windows, Linux, macOS. So, in this article, we will be installing the MIDI package in Python3 on Linux operating system. Features MIDI is a High-
2 min read
How to Install Astropy Python Library on Linux?
Astropy is an open-source library written purely in Python, specifically designed for use in astronomy and astrophysics. Anyone can develop astronomy software with Astropy Astronomy Tools. Astropy library is a cross-platform library for various operating systems such as Windows, Linux, and macOS. In
2 min read
How to Install Chainer Python Library on Linux?
Deep learning is a subset of machine learning that uses artificial neural networks to mimic the human brain. Chainer is an open-source deep learning framework written entirely in Python, based on the NumPy and CuPy Python libraries. Chainer is a powerful, flexible, and intuitive deep learning framew
2 min read
How to Install python-libgmail on Linux?
python-libgmail is a Python library that allows you to interact with your Gmail account using the Gmail API. It allows you to read, send, and delete emails, as well as manage your contacts and labels. In order to use python-libgmail, you will first need to install it on your Linux system. The instal
3 min read
How to Install Python-jabberpy on Linux?
Python is a high-level, interpreted programming language that is widely used for web development, machine learning, and scientific computing. Jabberpy, on the other hand, is a Python library that allows developers to create XMPP clients, which are used for instant messaging and real-time communicati
3 min read
How to Install Python on Linux
This guide explains how to install Python on Linux machines. Python has become an essential programming language for developers, data scientists, and system administrators. It's used for various applications, including web development, data science, automation, and machine learning. This comprehensi
15+ min read
How to Install Python 3 on Kali Linux
Python 3 is a powerful and versatile programming language widely used for various tasks, from web development to data science, security automation, and AI and ML. Recent versions of Kali Linux come with Python 3 pre-installed. For some reason if you want a different version than the one already inst
3 min read
How to install Librosa Library in Python?
Librosa is a Python package for music and audio analysis. Librosa is basically used when we work with audio data like in music generation(using LSTM's), Automatic Speech Recognition. It provides the building blocks necessary to create the music information retrieval systems. Librosa helps to visuali
1 min read
How to Install python-kinterbasdb on Linux?
In this article, we will be looking at the stepwise procedure to install the python-kinterbasdb for Python in Linux. KInterbasDB is a Python DB API 2.0 module for the relational databases Firebird and Interbase. It also exposes most of the native client API of the database engine, including two-phas
2 min read
How to Install Nose in Python on Linux?
The nose is a Python package essentially needed for automation framework testing. Basically, Nose generates uni-test modules that will help to execute the testing result much better. Also, Nose provides another advantage in that it will auto-discover the test cases. Based on the test case result it
3 min read