Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
sbyrnes committed Mar 18, 2024
1 parent 830aea1 commit eac963f
Showing 1 changed file with 23 additions and 18 deletions.
41 changes: 23 additions & 18 deletions lab-2/4_netmiko_seek_helper_addrs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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!")

0 comments on commit eac963f

Please sign in to comment.