-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathport_scanner.py
More file actions
39 lines (34 loc) · 1.18 KB
/
port_scanner.py
File metadata and controls
39 lines (34 loc) · 1.18 KB
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
import socket
import sys
from datetime import datetime
def scan_port(target, port):
"""Attempt a TCP connection to a single port."""
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(1)
result = sock.connect_ex((target, port))
sock.close()
return result == 0
except socket.error:
return False
def scan_target(target, start_port, end_port):
"""Scan a range of ports on the target host."""
print(f"\nScanning target {target}")
# Fixed the f-string below:
print(f"Started at {datetime.now().strftime('%H:%M:%S')}")
print("-" * 40)
open_ports = []
for port in range(start_port, end_port + 1):
# Added the missing colon (:) below:
if scan_port(target, port):
open_ports.append(port)
try:
service = socket.getservbyport(port)
except OSError:
service = "unknown"
print(f" Port {port}: OPEN ({service})")
print(f"\nScan complete. {len(open_ports)} open port(s) found.")
return open_ports
if __name__ == "__main__":
target = input("Target IP or hostname: ")
scan_target(target, 1, 1024)