Sitemap

Part 2: Python-NMAP Module

4 min readMar 7, 2025

--

Python-Nmap is a module that provides functionality to discover what ports or services a specific host has open for listening.

Press enter or click to view image in full size

The tool is also significant for system Administrators or computer security consultants because it automates penetration-testing processes and network troubleshooting.

The above library also provides us with advanced scripting features from the Nmap Scripting Engine (NSE) to automate attacks or detect vulnerable services on the target machine.

Installation:

Kali> pip install python-nmap

Our First Script (my_first_scan.py).

Press enter or click to view image in full size
https://github.com/ALLann123/Python-Network-Programming/blob/main/Python-Nmap/my_first_scan.py

Algorithm:

· We import the library Nmap which we will use for the scan

· Get user input i.e. target IP, port range to scan, and the option we want to execute in our nmap command.

· Then create an object called port_scan from the PortScanner() class imported from the Nmap library.

· After creating the object port_scan we can now call the scan function that requires three arguments that is target IP, port, and option flag to begin scanning the target.

· We then print the results of our scan.

Results:

Press enter or click to view image in full size

In the above we provide the target as scanme.nmap.org, Range of ports 22–80 and the option in our scan to be a stealth scan –sS.

We get in return the IP address of the target as 45.33.32.156 and two open ports 22 and 80.

Extracting Information with Nmap Library

Python-Nmap library provides functions to extract information more efficiently.

Open terminal and type:

Kali> python

Press enter or click to view image in full size

Import Nmap, create an object, and use the scan () function to scan the site. Now let’s extract the information below.

Get the IP of the target.

Press enter or click to view image in full size

Get the status of the target

Press enter or click to view image in full size

We get to see that the target is up and not offline.

Get own_the_net’s stories in your inbox

Join Medium for free to get updates from this writer.

Get the Command that was executed

Press enter or click to view image in full size

Simple Python-Nmap Scanner (nmap_scanner.py)

Press enter or click to view image in full size
https://github.com/ALLann123/Python-Network-Programming/blob/main/Python-Nmap/scan_range.py

Algorithm:

· We first import the Nmap library

· Create an object from the PortScanner() class imported from Nmap. These provide access to functionalities from the class such as scan().

· Get user inputs such as host_scan which is the target IP/hostname, the port range to scan, and the optional flags to run with our nmap scan.

· Line 16 we carry the scan of the target by calling the scan() function which accepts three arguments the IP, argument options for the scan, and port range.

· “hosts_list = [(x, port_scanner[x][‘status’][‘state’]) for x in port_scanner.all_hosts()]” Is responsible for returning all the hosts discovered during the scan and returns its status as either up or down.

Press enter or click to view image in full size

· In the first for loop we use it to display the status of the host if up or down.

· In the final nested for loop, the outer one is responsible for displaying the protocol used, and then the inner one iterates through the list of ports and displays the open ports that were found.

Run The Script:

Kali> python nmap_scanner.py

Press enter or click to view image in full size

NOTE: My target is a vulnerable Metasploitable 2 in my virtual box.

Intermediate port scanner with python-nmap

The library also provides us with the capability of getting the services running on the ports and the operating system of the host computer.

Press enter or click to view image in full size
https://github.com/ALLann123/Python-Network-Programming/blob/main/Python-Nmap/nmap_os_detect.py

Algorithm:

· Import the library nmap

· Create an nmap scan function accepting one parameter ip_address.

· Create an object from the PortScanner()class which will use for our scan.

· Call the scan() function using the object created passing the arguments ip_address and the arguments for our scan. –O and –V will use detect the operating system of the target.

· Save the results of the above in a dictionary.

· Print the command we executed in our scan to the screen.

· Then extract the TCP ports information from the scan result.

· Save the keys of the dictionary onto a list in order to iterate through it.

· The keys are the ports discovered now we use a For block to iterate through the list while displaying it using the print statement.

· Finally, we get the OS family of the target and display to the screen.

Run the program and use 127.0.0.1(local host) as target:

Press enter or click to view image in full size

Conclusion

Python-Nmap Library allows us to automate the majority of tasks using scripts. The next article will cover using the library for vulnerability Scanning.

--

--

own_the_net
own_the_net

Written by own_the_net

No one cared who I was until I put on the mask.