A beginner-friendly Python project that uses the python-nmap library to perform quick and informative port scans on any host. Itβs a great way to understand how Nmap can be automated with Python for network analysis, system auditing, or learning purposes.
-
Scans a target host using Nmap directly from Python
-
Displays:
- Host IP and hostname
- Protocols detected (TCP/UDP)
- Port numbers, states (open/closed), and detected services
- Product and version information (via
-sV)
-
Saves the results to a text file (
scan_results.txt) -
Easy to modify for automation or batch scanning
Make sure you have the following installed:
-
Python 3.7+
-
Nmap
-
Verify it works by running:
nmap -v
-
python-nmap library Install it using pip:
pip install python-nmap
β οΈ Note: Use a Bash or Command Prompt terminal to install packages and run the script. (If you encounter issues on PowerShell, try running the same commands in Bash or Command Prompt.)
git clone https://github.com/<your-username>/simple-port-scanner.git
cd simple-port-scannerpython port_scanner.pyBy default, it scans:
ip = "45.33.32.156" # Nmap test serverHost: 45.33.32.156 (scanme.nmap.org)
State: up
Protocol: tcp
Port: 22
State: open
Service: ssh
Product: OpenSSH
Version: 6.6.1p1 Ubuntu
Port: 80
State: open
Service: http
Product: Apache httpd
Version: 2.4.7
After the scan finishes, results are saved to:
scan_results.txt
import nmap
nm = nmap.PortScanner()
ip = "45.33.32.156"
nm.scan(ip, arguments="-sC -sV")
for host in nm.all_hosts():
print(f"\nHost: {host} ({nm[host].hostname()})")
print(f"State: {nm[host].state()}")
for proto in nm[host].all_protocols():
print(f"\nProtocol: {proto}")
ports = nm[host][proto]
for port, info in ports.items():
print(f" Port: {port}")
print(f" State: {info['state']}")
print(f" Service: {info.get('name', 'N/A')}")
print(f" Product: {info.get('product', 'N/A')}")
print(f" Version: {info.get('version', 'N/A')}")This script is for educational and authorized use only. Always ensure you have explicit permission before scanning any external network or host.
Scanning systems without consent can be illegal in many regions.
- Add multiple IP / subnet scanning
- Include
richfor colorized terminal output - Add concurrent scanning for faster results
- Save outputs in JSON or CSV format
This project is open-source under the MIT License β feel free to use and modify it.