diff --git a/lab-2/3_netmiko_update_description_to_lldp_neighbors.py b/lab-2/3_netmiko_update_description_to_lldp_neighbors.py index 079dcf1..65637a5 100644 --- a/lab-2/3_netmiko_update_description_to_lldp_neighbors.py +++ b/lab-2/3_netmiko_update_description_to_lldp_neighbors.py @@ -5,12 +5,12 @@ username = "clab" password = "clab@123" -device_type = "ios_xr" +device_type = "cisco_xr" hosts = ["172.16.x.2", "172.16.x.3", "172.16.x.4"] # TODO command_to_run = "show lldp neighbors detail" # TODO for host in hosts: - # Create a variable that represents an SSH connection to our router. + ### Here's the old stuff! ### connection = Netmiko( username=username, password=password, @@ -21,6 +21,7 @@ # Send a command to the router, and get back the output "dictionaried" by textfsm. textfsm_output = connection.send_command(command_to_run, use_textfsm=True) + ### Here's the new stuff! ### # We know that textfsm_output looks something like this for a host: # [ # { @@ -56,9 +57,8 @@ configuration = [ f"int {my_interface}", - f"description ->{neighbor_name}, {neighbor_interface}" + f"description -> {neighbor_name}, {neighbor_interface}" ] - output = connection.send_config_set(configuration) - print(output) + connection.send_config_set(configuration) print(connection.send_command(f"show run int {my_interface}")) diff --git a/lab-2/4_netmiko_seek_helper_addrs.py b/lab-2/4_netmiko_seek_helper_addrs.py index 4f547a4..4d112a2 100644 --- a/lab-2/4_netmiko_seek_helper_addrs.py +++ b/lab-2/4_netmiko_seek_helper_addrs.py @@ -4,7 +4,7 @@ username = "clab" password = "clab@123" -device_type = "ios_xr" +device_type = "cisco_xr" hosts = ["172.16.x.2", "172.16.x.3", "172.16.x.4"] # TODO target_ip_helper = "fill me in!" @@ -21,6 +21,12 @@ parser = CiscoConfParse(raw_config.split("\n")) for intf in parser.find_objects("^interface .*"): + # Get the interface name. + intf_name = intf.text + + # Give us nice messages + print(f"Inspecting {intf_name} on {host}...") + # Retrieve the helper address, if it exists. helper_address_line = intf.re_search_children("^ ipv4 helper-address") if not helper_address_line: @@ -28,21 +34,21 @@ # Don't configure a new IP helper on interfaces that don't have one. continue - # Get the interface name. - intf_name = intf.text - # Get the last "word" in the line, which is the helper IP address. - ip = helper_address_line.split()[-1] + ip = helper_address_line[-1] if ip != target_ip_helper: + print(f"Found old IP helper!:\n{intf}") commands = [ intf_name, f"no ipv4 helper-address {ip}", - f"ipv4 helper-address {target_ip_helper}" + f"ipv4 helper-address {target_ip_helper}", + "commit" ] # Let's observe: - print(f"Running on {host}: {commands}") - output = connection.send_config_set(commands) - print(output) + print(f"Running: {commands}") + connection.send_config_set(commands) + new_interface_config = connection.send_command(f"show run int {intf_name}") + print(f"Post-configuration:\n{new_interface_config}") print("Done!") diff --git a/pyproject.toml b/pyproject.toml index b0588d5..e273d6a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -9,6 +9,7 @@ packages = [{include = "2024_lonisummit_workshop_automation"}] [tool.poetry.dependencies] python = "^3.9" netmiko = "^4.3.0" +ciscoconfparse = "^1.9.41" [tool.poetry.group.dev.dependencies] black = "^24.2.0"