From eac963f25de08a10d42be2dcf6da75cacc6ad240 Mon Sep 17 00:00:00 2001 From: Shannon Byrnes Date: Sun, 17 Mar 2024 18:47:47 -0700 Subject: [PATCH] wip --- lab-2/4_netmiko_seek_helper_addrs.py | 41 ++++++++++++++++------------ 1 file changed, 23 insertions(+), 18 deletions(-) diff --git a/lab-2/4_netmiko_seek_helper_addrs.py b/lab-2/4_netmiko_seek_helper_addrs.py index 9caf730..3d8663c 100644 --- a/lab-2/4_netmiko_seek_helper_addrs.py +++ b/lab-2/4_netmiko_seek_helper_addrs.py @@ -6,6 +6,7 @@ password = "clab@123" device_type = "cisco_xr" hosts = ["172.16.x.2", "172.16.x.3", "172.16.x.4"] # TODO +old_ip_helper = "fill me in!" # TODO target_ip_helper = "fill me in!" # TODO for host in hosts: @@ -28,27 +29,31 @@ print(f"Inspecting {intf_name} on {host}...") # Retrieve the helper address, if it exists. - helper_address_line = intf.re_search_children("^ ipv4 helper-address") + helper_address_line = intf.re_search_children(f"^ ipv4 helper-address .* {old_ip_helper}") if not helper_address_line: # Nothing to see here! Skip. - # Don't configure a new IP helper on interfaces that don't have one. + # Only configure our new IP helper on the interface if the old one existed. continue - - # Get the last "word" in the line, which is the helper IP address. - ip = helper_address_line[-1].text - - if ip != target_ip_helper: + else: print(f"Found old IP helper!:\n{intf}") - commands = [ - intf_name, - f"no ipv4 helper-address {ip}", - f"ipv4 helper-address {target_ip_helper}", - "commit" - ] - # Let's observe: - print(f"Running: {commands}") - connection.send_config_set(commands) - print(f"Running: show run int {intf_name}") - print(connection.send_command(f"show run int {intf_name}")) + + # Variable `helper_address_line` is a list of all matching lines. + # We know Cisco won't have duplicate lines, so we can always assume it is the first + # in the list. + raw_config_line = helper_address_line[0].text + + # We only need the last "word" in the line, which is the IP address. + ip = raw_config_line.split()[-1] + + commands = [ + intf_name, + f"no ipv4 helper-address {ip}", + f"ipv4 helper-address {target_ip_helper}", + "commit" + ] + # Let's observe: + print(f"Before:\n{connection.send_command(f'show run {intf_name}')}\n") + connection.send_config_set(commands) + print(f"After:\n{connection.send_command(f'show run {intf_name}')}") print("Done!")