0% found this document useful (0 votes)
26 views9 pages

Practice Lab - Nmap Scanner With Single Port Scan in Python3 v1

This document provides a step-by-step guide to creating a basic Nmap port scanner program in Python 3. It outlines the necessary setup, including installing the python-nmap package and the Nmap program, and details the code required to scan a target IP address and port. The final code snippet demonstrates how to implement the scanner and print the port's status.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views9 pages

Practice Lab - Nmap Scanner With Single Port Scan in Python3 v1

This document provides a step-by-step guide to creating a basic Nmap port scanner program in Python 3. It outlines the necessary setup, including installing the python-nmap package and the Nmap program, and details the code required to scan a target IP address and port. The final code snippet demonstrates how to implement the scanner and print the port's status.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Create a basic Network Ports

Scanner (Nmap) in Python


Networkwalks Academy

IT, Networking, Cybersecurity,


www.networkwalks.com [email protected]
Programming, Linux
Practice Lab
Create a basic Network Scanner (Nmap) in Python
(Python for Network Engineers, Cybersecurity & Ethical Hacking)

In this lab practice, you are required to use your python programming skills to create a program in as simple as
possible way. We will use python version3.

Task
Create a basic Nmap Port Scanner program in Python3. The program should
ask user to input a single Target IP Address and a single Target Port. Then it
should scan the target & show the results.

Related Info:
‐ Please use Python version 3
‐ You can use any method to write this program (no restrictions on programming style or libraries
usage)

1 www.networkwalks.com [email protected]
*Note: Nmap setup for python in Windows and Linux is not straight-forward like importing
other modules in Python programs. So please pay close attention while setting it up.

Solution

We will use Python’s NMAP module in this Lab. The NMAP module/library in Python is used
to perform network discovery, port scanning and vulnerability assessments
programmatically from within Python.
Nmap is a security scanner used to discover hosts and services on a computer network by
sending packets and analyzing the responses. Nmap is used for:

• Discover active devices on a network.


• Scan open ports on a target host.
• Identify the services (e.g., HTTP, FTP) running on open ports.
• Perform OS detection, service version detection, and more.
To use the nmap module in Python, you need to install the python-nmap package.
You can install it using pip $pip install python-nmap. Additionally, you must have
Nmap program installed on your system. You can download and install it from
Nmap's official site: https://nmap.org/download.html#windows.

Step1 Install python-nmap package:

pip install python-nmap

Step2 Download and Install nmap program from Nmap’s official site:

2 www.networkwalks.com [email protected]
https://nmap.org/download.html#windows

3 www.networkwalks.com [email protected]
4 www.networkwalks.com [email protected]
5 www.networkwalks.com [email protected]
Step3 Import the NMAP module:

import nmap

Step4 Ask the user to input the IP Address and port of target device:

target_ip = input("Please enter target IP address to scan: ")


target_port = input("Please enter target Port to scan: ")

Step5 Create Nmap scanner function:

res = nmap.PortScanner().scan(target_ip, target_port)

Step6 Print the results:


print("\n")
print("Port " + target_port + " is: " +
res['scan'][target_ip]['tcp'][int(target_port)]['state'])

*The result res is a multi-nested dictionary print(res) containing several


information. We only need to check if the port is open or closed. So, we will
access only that information from the results dictionary.

6 www.networkwalks.com [email protected]
Output Example

7 www.networkwalks.com [email protected]
Complete Code

● ● ● Python3

#Nmap Port/Network Scanner tool in python (www.networkwalks.com)


#pip install python-nmap

import nmap
target_ip = input("Please enter target IP address to scan: ")
target_port = input("Please enter target Port to scan: ")
res = nmap.PortScanner().scan(target_ip, target_port)

print("\n")
-End-
print("Port " + target_port + " is: " +
res['scan'][target_ip]['tcp'][int(target_port)]['state'])

© All Rights are reserved, Networkwalks Academy


Leave your feedback at: [email protected]. Your Technical Questions, comments & suggestions are always Welcomed.
www.networkwalks.com

8 www.networkwalks.com [email protected]

You might also like