Give me code that collects a Cisco IOS or NXOS device information and configuration, including things like current privilege level, running configuration, LLDP and CDP neighbors
Give me code that collects a Cisco IOS or NXOS device information and configuration, including things like current privilege level, running configuration, LLDP and CDP neighbors
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 | from netmiko import ConnectHandler, NetmikoTimeoutException, NetmikoAuthenticationException def get_device_info(device_ip, username, password): """ Collects device information and configuration from a Cisco IOS or NXOS device. Args: device_ip (str): IP address of the device. username (str): Username for authentication. password (str): Password for authentication. Returns: dict: Dictionary containing device information and configuration. """ try: device = ConnectHandler( ip=device_ip, username=username, password=password, device_type="cisco_ios", ) except (NetmikoTimeoutException, NetmikoAuthenticationException): return None device_info = { "current_privilege_level": device.check_config_mode(), "running_configuration": device.send_command("show running-config"), "lldp_neighbors": device.send_command("show lldp neighbors"), "cdp_neighbors": device.send_command("show cdp neighbors"), } device.disconnect() return device_info if __name__ == "__main__": device_ip = "192.168.1.1" username = "admin" password = "password" device_info = get_device_info(device_ip, username, password) if device_info is not None: print("Device Information:") for key, value in device_info.items(): print(f" {key}: {value}") else: print("Failed to collect device information.") |
Testing if the previous output contains the string show version
: False
Testing if the previous output contains the string show lldp neighbors detail
: False
Testing if the previous output contains the string show lldp neighbors
: True
Testing if the previous output contains the string show cdp neighbors detail
: False
Testing if the previous output contains the string show cdp neighbors
: True
Testing if the previous output contains the string show run
: True
Testing if the previous output contains the string show privilege
: False
Give me code that collects a Cisco IOS or NXOS device information and configuration, including things like current privilege level, running configuration, LLDP and CDP neighbors
Give me code that collects a Cisco IOS or NXOS device information and configuration, including things like current privilege level, running configuration, LLDP and CDP neighbors
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 | import napalm import yaml # Create a NAPALM connection to the device device = napalm.get_network_driver("ios")("10.10.10.1", "username", "password") device.open() # Collect device information facts = device.get_facts() # Collect running configuration config = device.get_config() # Collect LLDP neighbors lldp_neighbors = device.get_lldp_neighbors() # Collect CDP neighbors cdp_neighbors = device.get_cdp_neighbors() # Print the collected information print(yaml.dump(facts, default_flow_style=False)) print(yaml.dump(config, default_flow_style=False)) print(yaml.dump(lldp_neighbors, default_flow_style=False)) print(yaml.dump(cdp_neighbors, default_flow_style=False)) # Close the NAPALM connection device.close() |
Testing if the previous output contains the string show version
: False
Testing if the previous output contains the string show lldp neighbors detail
: False
Testing if the previous output contains the string show lldp neighbors
: False
Testing if the previous output contains the string show cdp neighbors detail
: False
Testing if the previous output contains the string show cdp neighbors
: False
Testing if the previous output contains the string show run
: False
Testing if the previous output contains the string show privilege
: False
Give me code that collects a Cisco IOS or NXOS device information and configuration, including things like current privilege level, running configuration, LLDP and CDP neighbors
Give me code that collects a Cisco IOS or NXOS device information and configuration, including things like current privilege level, running configuration, LLDP and CDP neighbors
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 52 53 54 55 56 57 58 59 60 61 62 63 | import getpass import telnetlib def get_device_info(ip_address, username, password): """ Collects device information and configuration from a Cisco IOS or NXOS device. Args: ip_address (str): The IP address of the device. username (str): The username to use for authentication. password (str): The password to use for authentication. Returns: dict: A dictionary containing the collected information. """ # Connect to the device using Telnet. tn = telnetlib.Telnet(ip_address) # Log in to the device. tn.read_until(b"Username: ") tn.write(username.encode("ascii") + b"\n") tn.read_until(b"Password: ") tn.write(password.encode("ascii") + b"\n") # Get the current privilege level. tn.write(b"show privilege\n") privilege_level = tn.read_until(b"#").decode("ascii").strip() # Get the running configuration. tn.write(b"show running-config\n") running_config = tn.read_until(b"#").decode("ascii") # Get the LLDP neighbors. tn.write(b"show lldp neighbors\n") lldp_neighbors = tn.read_until(b"#").decode("ascii") # Get the CDP neighbors. tn.write(b"show cdp neighbors\n") cdp_neighbors = tn.read_until(b"#").decode("ascii") # Close the connection to the device. tn.close() # Return the collected information. return { "privilege_level": privilege_level, "running_config": running_config, "lldp_neighbors": lldp_neighbors, "cdp_neighbors": cdp_neighbors, } # Get the device information and configuration. device_info = get_device_info( ip_address="10.10.10.1", username="admin", password="password", ) # Print the device information. print(device_info) |
Testing if the previous output contains the string show version
: False
Testing if the previous output contains the string show lldp neighbors detail
: False
Testing if the previous output contains the string show lldp neighbors
: True
Testing if the previous output contains the string show cdp neighbors detail
: False
Testing if the previous output contains the string show cdp neighbors
: True
Testing if the previous output contains the string show run
: True
Testing if the previous output contains the string show privilege
: True
Give me code that collects a Cisco IOS or NXOS device information and configuration, including things like current privilege level, running configuration, LLDP and CDP neighbors
Give me code that collects a Cisco IOS or NXOS device information and configuration, including things like current privilege level, running configuration, LLDP and CDP neighbors
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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 | import json import re from getpass import getpass from netmiko import ConnectHandler, NetmikoTimeoutException, NetmikoAuthenticationException def get_device_info(device): """Collects device information and configuration. Args: device (dict): Dictionary containing device information, including IP address, username, and password. Returns: dict: Dictionary containing device information and configuration. """ # Connect to the device try: net_connect = ConnectHandler(**device) except (NetmikoTimeoutException, NetmikoAuthenticationException) as e: print(f"Failed to connect to {device['ip']}: {e}") return # Get the device information device_info = { "hostname": net_connect.find_prompt(), "os_version": net_connect.send_command("show version | include Version"), "current_privilege_level": net_connect.check_config_mode(), "running_config": net_connect.send_command("show running-config"), "lldp_neighbors": get_lldp_neighbors(net_connect), "cdp_neighbors": get_cdp_neighbors(net_connect), } # Close the connection net_connect.disconnect() return device_info def get_lldp_neighbors(net_connect): """Gets LLDP neighbors. Args: net_connect (netmiko.ConnectHandler): Netmiko connection object. Returns: list: List of LLDP neighbors. """ lldp_neighbors = [] output = net_connect.send_command("show lldp neighbors detail") for line in output.splitlines(): if "Port Description:" in line: port_description = line.split(": ")[-1] elif "System Name:" in line: system_name = line.split(": ")[-1] elif "Chassis Id:" in line: chassis_id = line.split(": ")[-1] elif "Port Id:" in line: port_id = line.split(": ")[-1] elif "TTL:" in line: ttl = line.split(": ")[-1] elif re.match(r"^\s+$", line): lldp_neighbors.append({ "port_description": port_description, "system_name": system_name, "chassis_id": chassis_id, "port_id": port_id, "ttl": ttl, }) return lldp_neighbors def get_cdp_neighbors(net_connect): """Gets CDP neighbors. Args: net_connect (netmiko.ConnectHandler): Netmiko connection object. Returns: list: List of CDP neighbors. """ cdp_neighbors = [] output = net_connect.send_command("show cdp neighbors detail") for line in output.splitlines(): if "Device ID:" in line: device_id = line.split(": ")[-1] elif "Platform:" in line: platform = line.split(": ")[-1] elif "Capabilities:" in line: capabilities = line.split(": ")[-1] elif "Interface:" in line: interface = line.split(": ")[-1] elif "Port ID:" in line: port_id = line.split(": ")[-1] elif re.match(r"^\s+$", line): cdp_neighbors.append({ "device_id": device_id, "platform": platform, "capabilities": capabilities, "interface": interface, "port_id": port_id, }) return cdp_neighbors if __name__ == "__main__": # Get device information from the user device = { "ip": input("Enter device IP address: "), "username": input("Enter username: "), "password": getpass("Enter password: "), } # Collect device information and configuration device_info = get_device_info(device) # Print the device information and configuration print(json.dumps(device_info, indent=4)) |
Testing if the previous output contains the string show version
: True
Testing if the previous output contains the string show lldp neighbors detail
: True
Testing if the previous output contains the string show lldp neighbors
: True
Testing if the previous output contains the string show cdp neighbors detail
: True
Testing if the previous output contains the string show cdp neighbors
: True
Testing if the previous output contains the string show run
: True
Testing if the previous output contains the string show privilege
: False
Give me code that collects a Cisco IOS or NXOS device information and configuration, including things like current privilege level, running configuration, LLDP and CDP neighbors
Give me code that collects a Cisco IOS or NXOS device information and configuration, including things like current privilege level, running configuration, LLDP and CDP neighbors
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 | import re from netmiko import ConnectHandler # Define the device information device = { "host": "device_hostname_or_ip", "username": "username", "password": "password", "device_type": "cisco_ios" # or 'cisco_nxos' } # Connect to the device net_connect = ConnectHandler(**device) # Enable privileged mode if necessary output = net_connect.send_command("show privilege") if "Current privilege level: 15" not in output: net_connect.enable() # Collect the necessary information current_privilege_level = re.search(r"Current privilege level: (\d+)", output).group(1) running_config = net_connect.send_command("show running-config") lldp_neighbors = net_connect.send_command("show lldp neighbors detail") cdp_neighbors = net_connect.send_command("show cdp neighbors detail") # Disconnect from the device net_connect.disconnect() # Print the collected information print(f"Current privilege level: {current_privilege_level}") print(f"Running configuration:\n{running_config}") print(f"LLDP neighbors:\n{lldp_neighbors}") print(f"CDP neighbors:\n{cdp_neighbors}") |
Testing if the previous output contains the string show version
: False
Testing if the previous output contains the string show lldp neighbors detail
: True
Testing if the previous output contains the string show lldp neighbors
: True
Testing if the previous output contains the string show cdp neighbors detail
: True
Testing if the previous output contains the string show cdp neighbors
: True
Testing if the previous output contains the string show run
: True
Testing if the previous output contains the string show privilege
: True