@@ -104,32 +104,78 @@ async def main():
104104 """Main function for command-line usage."""
105105 parser = argparse .ArgumentParser (description = 'Network scanner for DialogChain' )
106106 parser .add_argument ('--network' , '-n' , default = '192.168.1.0/24' ,
107- help = 'Network to scan in CIDR notation' )
107+ help = 'Network to scan in CIDR notation (default: 192.168.1.0/24) ' )
108108 parser .add_argument ('--service' , '-s' , action = 'append' ,
109- help = 'Service types to scan (rtsp, http, etc.)' )
110- parser .add_argument ('--port' , '-p' , type = int , action = 'append' ,
111- help = 'Specific ports to scan' )
109+ help = 'Service types to scan (rtsp, http, https, ssh, etc.)' )
110+ parser .add_argument ('--port' , '-p' , default = None ,
111+ help = 'Comma-separated list of ports or port ranges to scan (e.g., 80,443,8000-9000) ' )
112112 parser .add_argument ('--timeout' , '-t' , type = float , default = 1.0 ,
113- help = 'Connection timeout in seconds' )
113+ help = 'Connection timeout in seconds (default: 1.0)' )
114+ parser .add_argument ('--verbose' , '-v' , action = 'store_true' ,
115+ help = 'Show detailed output' )
114116
115117 args = parser .parse_args ()
116118
119+ # Process ports from --port argument
120+ ports = []
121+ if args .port :
122+ for port_str in args .port .split (',' ):
123+ port_str = port_str .strip ()
124+ if '-' in port_str :
125+ # Handle port ranges (e.g., 8000-9000)
126+ try :
127+ start , end = map (int , port_str .split ('-' ))
128+ ports .extend (range (start , end + 1 ))
129+ except (ValueError , IndexError ):
130+ print (f"Warning: Invalid port range '{ port_str } '. Skipping..." )
131+ elif port_str .isdigit ():
132+ ports .append (int (port_str ))
133+
117134 scanner = SimpleNetworkScanner (timeout = args .timeout )
118- services = await scanner .scan_network (
119- network = args .network ,
120- ports = args .port ,
121- service_types = args .service
122- )
123135
124- # Print results
125- print ("\n Scan Results:" )
126- print ("-" * 60 )
127- print (f"{ 'IP' :<15} { 'Port' :<6} { 'Service' :<10} { 'Status' } " )
128- print ("-" * 60 )
136+ if args .verbose :
137+ print (f"Starting network scan on { args .network } " )
138+ if ports :
139+ print (f"Scanning ports: { ', ' .join (map (str , ports ))} " )
140+ if args .service :
141+ print (f"Scanning services: { ', ' .join (args .service )} " )
142+
143+ try :
144+ services = await scanner .scan_network (
145+ network = args .network ,
146+ ports = ports if ports else None ,
147+ service_types = args .service
148+ )
149+
150+ # Filter only active services
151+ active_services = [s for s in services if s .is_up ]
152+
153+ if args .verbose or not active_services :
154+ print ("\n Scan Results:" )
155+ print ("-" * 60 )
156+ print (f"{ 'IP' :<15} { 'Port' :<6} { 'Service' :<10} { 'Status' } " )
157+ print ("-" * 60 )
158+
159+ if not active_services :
160+ print ("No active services found." )
161+ if not args .verbose :
162+ print ("Use -v for more detailed output." )
163+ return
164+
165+ for svc in sorted (active_services , key = lambda x : (x .ip , x .port )):
166+ status = "UP"
167+ print (f"{ svc .ip :<15} { svc .port :<6} { svc .service :<10} { status } " )
168+
169+ except KeyboardInterrupt :
170+ print ("\n Scan interrupted by user." )
171+ except Exception as e :
172+ print (f"\n Error during scan: { e } " )
173+ if args .verbose :
174+ import traceback
175+ traceback .print_exc ()
176+ return 1
129177
130- for svc in sorted (services , key = lambda x : (x .ip , x .port )):
131- status = "UP" if svc .is_up else "DOWN"
132- print (f"{ svc .ip :<15} { svc .port :<6} { svc .service :<10} { status } " )
178+ return 0
133179
134180if __name__ == "__main__" :
135181 asyncio .run (main ())
0 commit comments