import socket
import threading
# Function to scan a single port
def scan_port(ip, port, open_ports):
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(1) # Set timeout for the connection
result = sock.connect_ex((ip, port))
if result == 0:
open_ports.append(port)
sock.close()
except Exception as e:
print(f"Error scanning port {port}: {e}")
# Function to perform the port scan
def scan_ports(ip, start_port, end_port):
open_ports = []
threads = []
print(f"Scanning {ip} for open ports from {start_port} to {end_port}...")
for port in range(start_port, end_port + 1):
thread = threading.Thread(target=scan_port, args=(ip, port, open_ports))
threads.append(thread)
thread.start()
for thread in threads:
thread.join() # Wait for all threads to complete
return open_ports
if __name__ == "__main__":
target_ip = input("Enter the IP address to scan: ")
start_port = int(input("Enter the starting port number: "))
end_port = int(input("Enter the ending port number: "))
open_ports = scan_ports(target_ip, start_port, end_port)
print("\nOpen ports:")
if open_ports:
for port in open_ports:
print(f"Port {port} is open.")
else:
print("No open ports found.")