-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path401Ops43b.py
54 lines (47 loc) · 1.98 KB
/
401Ops43b.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/usr/bin/env python3
"""
Script Name: Ops 42: Create a Port Scanner
Author: Juan Miguel Cano
Date of latest revision: 03/06/2024
Description: This script scans a range of ports on a specified host IP address.
"""
import socket
import concurrent.futures
import ipaddress
# ANSI escape codes for colors
GREEN = '\033[92m'
RED = '\033[91m'
RESET = '\033[0m'
def scan_port(hostip, port):
try:
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sockmod:
sockmod.settimeout(1) # Set a timeout value
result = sockmod.connect_ex((hostip, port))
if result == 0:
print(f"Port {port} {GREEN}open{RESET}")
elif result == 111: # Connection refused
print(f"Port {port} {RED}closed{RESET}")
else:
print(f"Port {port} status: {RED}{result}{RESET}")
except socket.error as e:
print(f"Error while scanning port {port}: {RED}{e}{RESET}")
def portScanner(hostip, start_port, end_port):
print(f"Scanning ports {start_port} to {end_port} on host {hostip}...")
with concurrent.futures.ThreadPoolExecutor() as executor:
executor.map(lambda port: scan_port(hostip, port), range(start_port, end_port + 1))
if __name__ == "__main__":
try:
hostip = input("Enter the host IP: ") # Collect host IP from the user
ipaddress.ip_address(hostip) # Validate the entered IP address
except ValueError:
print(f"{RED}Invalid IP address format.{RESET}")
exit(1)
try:
port_range = input("Enter the port range (start-end): ") # Collect port range from the user
start_port, end_port = map(int, port_range.split('-'))
if not (0 < start_port <= end_port <= 65535):
raise ValueError("Invalid port range.")
except (ValueError, IndexError):
print(f"{RED}Invalid port range format.{RESET}")
exit(1)
portScanner(hostip, start_port, end_port)