#!/usr/bin/env python3 """ Discover GNS3 device IPs and generate deployment configuration. This script connects to your GNS3 lab and automatically discovers device IP addresses, then updates the deployment script. """ import telnetlib import time import re import json # GNS3 devices (from API) GNS3_SWITCHES = [ {'name': 'SW-HQ-Core', 'console_port': 5046, 'type': 'cisco_ios'}, {'name': 'SW-West', 'console_port': 5059, 'type': 'cisco_ios'}, {'name': 'SW-East', 'console_port': 5069, 'type': 'cisco_ios'}, ] def get_device_ip(host, port, timeout=5): """ Connect to device console and get management IP. Args: host: Telnet host (usually localhost) port: Console port number timeout: Connection timeout Returns: IP address or None """ try: print(f"Connecting to console port {port}...") tn = telnetlib.Telnet(host, port, timeout=timeout) # Send some enters to get prompt tn.write(b"\r\n") time.sleep(0.5) tn.write(b"\r\n") time.sleep(0.5) # Try to enter enable mode tn.write(b"enable\r\n") time.sleep(0.5) # Get interface IP addresses tn.write(b"show ip interface brief\r\n") time.sleep(1) output = tn.read_very_eager().decode('ascii', errors='ignore') tn.close() # Parse output for IP addresses # Look for lines like: Vlan1 192.168.1.10 YES manual up up ip_pattern = r'(\d+\.\d+\.\d+\.\d+)' ips = re.findall(ip_pattern, output) # Filter out 0.0.0.0 and get first valid IP valid_ips = [ip for ip in ips if ip != '0.0.0.0' and not ip.startswith('127.')] if valid_ips: return valid_ips[0] return None except Exception as e: print(f" Error: {e}") return None def discover_devices(host='localhost'): """Discover all devices and their IPs""" discovered = [] print("\n" + "="*70) print("Discovering GNS3 Lab Devices") print("="*70 + "\n") for device in GNS3_SWITCHES: print(f"Device: {device['name']}") ip = get_device_ip(host, device['console_port']) if ip: print(f" āœ“ Found IP: {ip}\n") discovered.append({ 'name': device['name'], 'ip': ip, 'console_port': device['console_port'], 'type': device['type'] }) else: print(f" āœ— No IP found (device may not be configured yet)\n") return discovered def generate_deployment_config(devices): """Generate Python config for deployment script""" print("\n" + "="*70) print("Deployment Configuration") print("="*70 + "\n") print("# Copy this into examples/deploy_to_gns3_lab.py:\n") print("GNS3_DEVICES = [") for dev in devices: print(f" {{") print(f" 'name': '{dev['name']}',") print(f" 'hostname': '{dev['ip']}',") print(f" 'device_type': DeviceType.CISCO_IOS,") print(f" 'vendor': 'cisco',") print(f" 'model': 'vios',") print(f" 'role': 'switch',") print(f" 'description': 'GNS3 Lab Switch',") print(f" 'console_port': {dev['console_port']}") print(f" }},") print("]") # Also save to JSON with open('/home/gpaasch/overgrowth/discovered_devices.json', 'w') as f: json.dump(devices, f, indent=2) print(f"\nāœ“ Saved to: /home/gpaasch/overgrowth/discovered_devices.json") def main(): """Main entry point""" print("\nšŸ” GNS3 Device Discovery Tool\n") print("This will connect to your GNS3 lab devices via console") print("and automatically discover their management IP addresses.\n") input("Press Enter to start discovery... ") devices = discover_devices() if devices: print(f"\nāœ… Discovered {len(devices)} devices with IP addresses") generate_deployment_config(devices) print("\n" + "="*70) print("Next Steps:") print("="*70) print("1. Copy the configuration above into examples/deploy_to_gns3_lab.py") print("2. Run: python examples/deploy_to_gns3_lab.py --list") print("3. Test: python examples/deploy_to_gns3_lab.py --device SW-HQ-Core --dry-run") print("4. Deploy: python examples/deploy_to_gns3_lab.py --all --production\n") else: print("\nāš ļø No devices discovered.") print("Make sure:") print(" 1. GNS3 project 'overgrowth' is open") print(" 2. Devices are started") print(" 3. Devices have IP addresses configured") print("\nYou can manually configure IPs on devices and run this again.\n") if __name__ == '__main__': main()