-
Notifications
You must be signed in to change notification settings - Fork 0
/
port_scan
30 lines (22 loc) · 859 Bytes
/
port_scan
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
import socket
def port_scan(target, ports):
"""Scans a target host for open ports."""
open_ports = []
for port in ports:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(2) # Timeout after 2 seconds
result = sock.connect_ex((target, port))
if result == 0:
open_ports.append(port)
sock.close()
return open_ports
if __name__ == "__main__":
target_host = input("Enter the target host (e.g., 127.0.0.1): ")
ports_to_scan = [22, 80, 443, 8080] # Add more ports as needed
open_ports = port_scan(target_host, ports_to_scan)
if open_ports:
print("Open ports on {}:".format(target_host))
for port in open_ports:
print("Port {}: Open".format(port))
else:
print("No open ports found on {}.".format(target_host))