Practice Lab - Nmap Scanner With Single Port Scan in Python3 v1
Practice Lab - Nmap Scanner With Single Port Scan in Python3 v1
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:
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:
6 www.networkwalks.com [email protected]
Output Example
7 www.networkwalks.com [email protected]
Complete Code
● ● ● Python3
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'])
8 www.networkwalks.com [email protected]