""" Simple CLI tool for network scanning. Usage: python cli.py scan 192.168.1.0/24 python cli.py hosts python cli.py topology """ import asyncio import sys from pathlib import Path # Add parent directory to path sys.path.insert(0, str(Path(__file__).parent.parent)) from examples.usage_example import NetworkScannerClient async def cmd_scan(network_range: str, scan_type: str = "quick"): """Start a network scan.""" client = NetworkScannerClient() print(f"Starting {scan_type} scan of {network_range}...") scan_id = await client.start_scan(network_range, scan_type) print(f"Scan ID: {scan_id}") print("Waiting for scan to complete...") result = await client.wait_for_scan(scan_id) print("\nScan Results:") print(f" Status: {result['status']}") print(f" Hosts found: {result['hosts_found']}") print(f" Ports scanned: {result['ports_scanned']}") async def cmd_hosts(): """List discovered hosts.""" client = NetworkScannerClient() hosts = await client.get_hosts(status="online") print(f"\nDiscovered Hosts ({len(hosts)}):") print("-" * 80) for host in hosts: services = host.get('services', []) print(f"\n{host['ip_address']:15} {host.get('hostname', 'N/A'):30}") print(f" Status: {host['status']}") print(f" Services: {len(services)}") if services: for svc in services[:3]: print(f" - {svc['port']:5} {svc.get('service_name', 'unknown')}") async def cmd_topology(): """Show network topology.""" client = NetworkScannerClient() topology = await client.get_topology() print(f"\nNetwork Topology:") print(f" Nodes: {len(topology['nodes'])}") print(f" Edges: {len(topology['edges'])}") print("\nNodes by Type:") node_types = {} for node in topology['nodes']: node_type = node['type'] node_types[node_type] = node_types.get(node_type, 0) + 1 for node_type, count in sorted(node_types.items()): print(f" {node_type:15} {count}") async def cmd_stats(): """Show network statistics.""" client = NetworkScannerClient() stats = await client.get_statistics() print("\nNetwork Statistics:") print(f" Total hosts: {stats['total_hosts']}") print(f" Online: {stats['online_hosts']}") print(f" Offline: {stats['offline_hosts']}") print(f" Services: {stats['total_services']}") print(f" Scans: {stats['total_scans']}") def main(): """Main CLI entry point.""" if len(sys.argv) < 2: print("Usage:") print(" python cli.py scan [scan_type]") print(" python cli.py hosts") print(" python cli.py topology") print(" python cli.py stats") print("\nExamples:") print(" python cli.py scan 192.168.1.0/24") print(" python cli.py scan 192.168.1.0/24 standard") print(" python cli.py hosts") sys.exit(1) command = sys.argv[1].lower() try: if command == "scan": if len(sys.argv) < 3: print("Error: Network range required") print("Usage: python cli.py scan [scan_type]") sys.exit(1) network_range = sys.argv[2] scan_type = sys.argv[3] if len(sys.argv) > 3 else "quick" asyncio.run(cmd_scan(network_range, scan_type)) elif command == "hosts": asyncio.run(cmd_hosts()) elif command == "topology": asyncio.run(cmd_topology()) elif command == "stats": asyncio.run(cmd_stats()) else: print(f"Unknown command: {command}") sys.exit(1) except KeyboardInterrupt: print("\n\nInterrupted by user") sys.exit(0) except Exception as e: print(f"\nError: {e}") sys.exit(1) if __name__ == "__main__": main()