This repository walks you through creating a Simple Port Scanner using Python. A port scanner is a tool that helps check which ports are open on a target host. Open ports can give insights into what services are running, which is useful for testing and auditing networks.
Disclaimer: This port scanner is intended for educational purposes. Only scan systems you have permission to test.
- Python 3.6+: Make sure Python is installed. If not, you can download it from python.org.
- Basic understanding of networking: Knowing how ports and IP addresses work will be helpful.
- Permission: Ensure you have permission to scan the target machine.
Clone or download this repository, or open your terminal and create a new Python file:
mkdir SimplePortScanner
cd SimplePortScanner
touch port_scanner.pyFor this project, weβll use Python's socket library, which is built-in and lets us create connections to different ports.
# port_scanner.py
import socketDefine the IP address or hostname you want to scan, as well as the port range (e.g., ports 1-1024).
# Target and port range
target = input("Enter the target IP address or hostname: ")
start_port = int(input("Enter the starting port number: "))
end_port = int(input("Enter the ending port number: "))This function will attempt to connect to each port in the specified range and determine if itβs open or closed.
def scan_port(port):
"""Scan a single port to see if it's open."""
try:
# Create a socket connection
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(1) # Set timeout for each port scan
# Try to connect to the target and port
result = sock.connect_ex((target, port))
# If result is 0, the port is open
if result == 0:
print(f"Port {port} is open")
sock.close()
except Exception as e:
print(f"Error scanning port {port}: {e}")Now that we have a function to scan a single port, letβs call it in a loop to scan all ports in the specified range.
print(f"\nScanning {target} from port {start_port} to {end_port}...")
for port in range(start_port, end_port + 1):
scan_port(port)
Once everything is set up, run the program:
python3 port_scanner.pyEnter the IP or hostname and the port range you want to scan. For example:
Enter the target IP address or hostname: 192.168.1.1
Enter the starting port number: 1
Enter the ending port number: 1024The scanner will display open ports on the specified target.
Consider adding these features to expand your scanner:
Threading: Use threading to speed up scans by checking multiple ports simultaneously. Service Identification: Use socket.getservbyport(port) to identify which service typically runs on open ports.
Happy scanning! π₯οΈ