SoFunction
Updated on 2025-04-14

Detailed explanation of the installation and common methods of python-nmap

1. Preface

python-nmap​ is a Python-based network scanner interface that allows users to easily call the functionality of nmap through Python scripts. nmap​ is a powerful network scanning and discovery tool for detecting whether the host is online, scanning ports, and discovering network services. ​​python-nmap​​ makes the functions of ​​nmap​​​can be controlled through Python programs, thereby improving the automation and integration capabilities of network scanning.

2. Install python-nmap

2.1 System dependency

Before installing python-nmap​​, make sure that your system has the nmap​​ tool installed. Most Linux distributions and macOS systems already have nmap installed, but for Windows users, you may need toDownload and install ​​​nmap​​.

2.2 Installation process

​python-nmap​​ Usually it can be done through​pip​​ to install it, it is​Python​package manager. Open the terminal and enter the following command to install​python-nmap​​:

pip install python-nmap

If​pip​​ If you do not have permission to install dependencies or encounter permission problems, you may need to use ​​sudo​​ command to install:

sudo pip install python-nmap

2.3 Verify installation

After the installation is complete, you can verify it by:​python-nmap​​ Whether the installation was successful:

import nmap
 
# Create an empty scan objectnm = ()
 
# Try to scan port 80 of 127.0.0.1('127.0.0.1', '80')
 
# Print the resultsprint(nm['127.0.0.1']['tcp'][80]['state'])

This code creates an empty nmap scan object, and then tries to scan port 80 of localhost 127.0.0.1 and prints out the connection status of the port. If the installation is successful, you should see output like 'open'​​ or 'closed'​​​.

3. Using python-nmap

3.1 Basic Scan

Use​python-nmap​​ It is very easy to perform a basic scan. Here is a simple example:

import nmap
 
# Create an empty scan objectnm = ()
 
# Scan ports 22 and 80 of 192.168.1.1('192.168.1.1', '22,80')
 
# Print the resultsfor host in nm.all_hosts():
    print(f"Host: {host}")
    for port in nm[host]['tcp']:
        print(f"\tPort {port}: {nm[host]['tcp'][port]['state']}")

This code creates an empty​nmap​​ Scan the object, and then scan the​192.168.1.1​port on the host​22​and​80​, and print out the connection status of each port.

3.2 Advanced Scan

​python-nmap​Advanced scanning options are also supported, such as specifying ​​nmap​parameters of ​. Here is an example of using advanced scanning:

import nmap
 
# Create an empty scan objectnm = ()
 
# Use advanced options to scanoptions = {
    'arguments': '-p80,443',
    'host_timeout': 5,
    'scan_delay': 1,
    'max_retries': 3,
    'port_timeout': 2
}
('192.168.1.1', '22,80', options=options)
 
# Print the resultsfor host in nm.all_hosts():
    print(f"Host: {host}")
    for port in nm[host]['tcp']:
        print(f"\tPort {port}: {nm[host]['tcp'][port]['state']}")

In this example, we set up some advanced options, such as specifying the scanned port (`-p8Python-nmap is a Python library that provides an interface to the Nmap (Network Mapper) command line tool. Nmap is a popular network scanning tool for discovering hosts and services on the network. Python-nmap makes using Nmap more convenient, especially when writing scripts and automating tasks.

First, you need to install Python-nmap. If you are using Python 2, you can install it in the following ways:

pip install python-nmap

If you are using Python 3, you may need to use​pip3​Command:

pip3 install python-nmap

Or, if you already have a Python 3 environment, you can use it directly​pip​​:

pip install --upgrade pip
pip install python-nmap

After the installation is complete, you can use Python-nmap to perform various scans of Nmap. Here are some basic example code:

import nmap
 
# Create an nmap objectnm = ()
 
# Scan the port on a hostresult = ('', '80,443')
print(result)
 
# Check if a port is openif nm.is_up('', 80):
    print('Port 80 is open.')
else:
    print('Port 80 is closed.')
 
# Get information on all open portsopen_ports = nm.get_open_ports('')
for port in open_ports:
    print(f'Open port: {port}')
 
# Get all host informationhosts = nm.get_hosts()
for host in hosts:
    print(f'Host: {host}')
 
# Get detailed information about the specified hosthost_info = nm.get_host_scan_data('')
print(host_info)

In the above code, we create an object and then use it to scan the port on the specified host, check if the port is open, get information for all open ports, and get all host information and detailed information for the specified host.

Please note that when using Nmap for network scanning, you need to comply with relevant laws and regulations and do not scan networks or hosts that you do not have permission to scan. It is legal to use Nmap in development and testing environments, but in production environments you need to make sure you have sufficient permissions and comply with relevant policies. In the installation and common methods of python-nmap, the code part is used to demonstrate how to use Python to call Nmap for network scanning. Here is a simple example showing how to install and use python-nmap:

First, you need to install python-nmap. Enter the following command in the terminal:

pip install python-nmap

You can then use the following Python code to perform a basic Nmap scan:

import nmap
 
# Create an Nmap objectnm = ()
 
# Specify the host to scanhost = '192.168.1.1'  # Replace with the IP address you want to scan 
# Perform a TCP SYN scan(host, '1-10000', 'SYN', arguments='-T5')
 
# Get scan resultsnm.all_hosts()  # All scanned hostsnm.all_ports()  # All scanned portsnm.all_protocols()  # All scanned protocolsnm.all_service_info()  # All service informationnm.all_tasks()  # All scan tasks 
# Print open portfor port in nm[host]['tcp']:
    print(f"Port {port} is {nm[host]['tcp'][port]}")
 
# Print host statusprint(nm[host])

This code creates an Nmap object and then uses the scan method to perform the scan. In this example, we are performing a TCP SYN scan and the -T5 parameter is set, which means we will use very fast scanning speeds.

After the scan is completed, we can get the scan results by accessing the properties of the Nmap object, such as all_hosts, all_ports, all_service_info, etc.

Note that Nmap is a powerful tool that can perform multiple types of scans. With python-nmap, you can easily call these functions from Python scripts. In actual use, you may need to adjust the scanning parameters according to your needs and comply with relevant ethical and legal guidelines.

The above is a detailed explanation of the installation and common methods of python-nmap. For more information on the installation and usage of python-nmap, please follow my other related articles!