44Usage:
55 python -m cyber_security.port_scanner 192.168.1.10 --ports 1-1024 --threads 200 --timeout 0.5 --banner
66"""
7+
78from __future__ import annotations
89
910import argparse
@@ -68,12 +69,25 @@ def try_connect(host: str, port: int, timeout: float, banner: bool) -> ScanResul
6869
6970
7071def parse_args (argv : list [str ]) -> argparse .Namespace :
71- parser = argparse .ArgumentParser (description = "TCP port scanner with multi-threading and banner grab." )
72+ parser = argparse .ArgumentParser (
73+ description = "TCP port scanner with multi-threading and banner grab."
74+ )
7275 parser .add_argument ("host" , help = "Target IPv4/hostname" )
73- parser .add_argument ("--ports" , default = "1-1024" , help = "Port spec, e.g., 1-1024,22,80,443" )
74- parser .add_argument ("--threads" , type = int , default = DEFAULT_THREADS , help = "Max worker threads" )
75- parser .add_argument ("--timeout" , type = float , default = DEFAULT_TIMEOUT , help = "Socket timeout in seconds" )
76- parser .add_argument ("--banner" , action = "store_true" , help = "Attempt to grab service banner" )
76+ parser .add_argument (
77+ "--ports" , default = "1-1024" , help = "Port spec, e.g., 1-1024,22,80,443"
78+ )
79+ parser .add_argument (
80+ "--threads" , type = int , default = DEFAULT_THREADS , help = "Max worker threads"
81+ )
82+ parser .add_argument (
83+ "--timeout" ,
84+ type = float ,
85+ default = DEFAULT_TIMEOUT ,
86+ help = "Socket timeout in seconds" ,
87+ )
88+ parser .add_argument (
89+ "--banner" , action = "store_true" , help = "Attempt to grab service banner"
90+ )
7791 return parser .parse_args (argv )
7892
7993
@@ -94,9 +108,12 @@ def main(argv: list[str] | None = None) -> int:
94108
95109 tasks : list [Tuple [str , int ]] = [(host , p ) for p in ports ]
96110
97- with concurrent .futures .ThreadPoolExecutor (max_workers = max (1 , args .threads )) as executor :
111+ with concurrent .futures .ThreadPoolExecutor (
112+ max_workers = max (1 , args .threads )
113+ ) as executor :
98114 future_to_port = {
99- executor .submit (try_connect , host , port , args .timeout , args .banner ): port for _ , port in tasks
115+ executor .submit (try_connect , host , port , args .timeout , args .banner ): port
116+ for _ , port in tasks
100117 }
101118 for future in concurrent .futures .as_completed (future_to_port ):
102119 res = future .result ()
0 commit comments