-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimple_connect_scan.py
More file actions
51 lines (43 loc) · 1.53 KB
/
Copy pathsimple_connect_scan.py
File metadata and controls
51 lines (43 loc) · 1.53 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
40
41
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/env python3
"""simple_connect_scan.py
Lightweight unprivileged TCP connect port scanner.
Prints lines like: 22/tcp open
"""
import socket
import argparse
from concurrent.futures import ThreadPoolExecutor, as_completed
def parse_ports(s):
parts = [p.strip() for p in s.split(',') if p.strip()]
out = []
for p in parts:
if '-' in p:
a, b = p.split('-', 1)
out += list(range(int(a), int(b) + 1))
else:
out.append(int(p))
return sorted(set(out))
def probe(host, port, timeout):
try:
with socket.create_connection((host, port), timeout=timeout):
return port, 'open'
except ConnectionRefusedError:
return port, 'closed'
except socket.timeout:
return port, 'filtered'
except Exception:
return port, 'filtered'
def main():
p = argparse.ArgumentParser(description='Simple TCP connect port scanner')
p.add_argument('host')
p.add_argument('--ports', default='22,80,443', help='comma-separated ports or ranges (e.g. 20-25,80,443)')
p.add_argument('--timeout', type=float, default=1.0)
p.add_argument('--workers', type=int, default=50)
args = p.parse_args()
ports = parse_ports(args.ports)
with ThreadPoolExecutor(max_workers=args.workers) as ex:
futures = {ex.submit(probe, args.host, port, args.timeout): port for port in ports}
for fut in as_completed(futures):
port, state = fut.result()
print(f"{port}/tcp {state}")
if __name__ == '__main__':
main()