This Python script performs a network port scan using the nmap library to identify open ports on a target IP address. Below is a detailed breakdown of its functionality.
- Introduction
- Features
- Dependencies
- Usage
- Code Explanation
- Sample Output
- Security & Ethical Considerations
- Limitations
- Future Improvements
- Conclusion
- Scans a target IP address for open ports
- Provides:
- IP status (up/down)
- Protocol(s) in use (TCP/UDP)
- Open ports and their states (open/filtered/closed)
- Uses
nmapfor reliable scanning - Simple command-line input-based execution
- Python 3.6+
- Nmap Python Library (
python-nmap)- Install via pip:
pip install python-nmap
- (Optional) Install Nmap itself for advanced scanning:
sudo apt-get install nmap # Linux/Unix
- Install via pip:
python port_scanner.pyInteractive Input:
[+] Target IP ==> [INPUT YOUR TARGET IP HERE]
Example output:
Host : 192.1xx.x.x
State : up
Protocol : tcp
port : 22 state : open
port : 80 state : open
port : 443 state : open
Open Ports: -p 22,80,443 192.168.1.1
import nmap
nm = nmap.PortScanner() # Create Nmap scanner object
open_ports = "-p " # Will store open ports in Nmap format (e.g., "-p 80,443")print("[Info] This is a PortScanner to scan open ports on a target IP address.")
print(" || Uses NMAP (network mapper) library for Python 3.")
target_ip = input("[+] Target IP ==> ") # Prompt user for IPscan_results = nm.scan(
hosts=target_ip,
arguments="-sT -n -Pn -T4"
)Scan Arguments Explained:
| Flag | Explanation |
|---|---|
-sT |
TCP Connect Scan: Reliable, but detectable (3-way handshake) |
-n |
No DNS resolution: Faster scan (skips hostname lookup) |
-Pn |
No Ping: Bypasses host discovery (assumes host is up) |
-T4 |
Timing Aggressiveness: Faster scan (higher values risk detection) |
print("\nHost : %s" % target_ip)
print("State : %s" % nm[target_ip].state())
for protocol in nm[target_ip].all_protocols():
print("Protocol : %s" % protocol)
for port in nm[target_ip][protocol].keys():
print("port : %s\tstate : %s" % (port, nm[target_ip][protocol][port]["state"])) count = 0
for port in nm[target_ip][protocol].keys():
if nm[target_ip][protocol][port]["state"] == "open":
if count == 0:
open_ports += str(port)
count = 1
else:
open_ports += "," + str(port)
print("\nOpen Ports: " + open_ports + " " + target_ip) ⚠️ Legal Issues: Unauthorized scanning is illegal in many jurisdictions. Always obtain permission before scanning.⚠️ Detection Risks: Using-sT(full TCP handshake) makes the scan more detectable.- ✅ Use Cases:
- Network admins verifying firewall rules
- Security audits (when authorized)
- Pen-testing exercises
- ⛔ Do Not Use for:
- Malicious purposes
- Scanning networks without consent
- Add UDP Scanning
nm.scan(hosts=target_ip, arguments="-sU -sS") - OS & Version Detection
nm.scan(hosts=target_ip, arguments="-O -sV") - Output to File (CSV/JSON)
with open("scan_results.json", "w") as f: json.dump(scan_results, f)
- Multi-IP / Subnet Scanning
target_ip = "192.1xx.x.x/24" # Scan entire subnet
This script provides a basic yet effective way to scan a network for open ports using Python and Nmap. Enhancements can make it more flexible for security professionals and network administrators.