Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
wip
  • Loading branch information
sbyrnes committed Mar 18, 2024
1 parent ee42593 commit 8ba0aec
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 14 deletions.
10 changes: 5 additions & 5 deletions lab-2/3_netmiko_update_description_to_lldp_neighbors.py
Expand Up @@ -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,
Expand All @@ -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:
# [
# {
Expand Down Expand Up @@ -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}"))
24 changes: 15 additions & 9 deletions lab-2/4_netmiko_seek_helper_addrs.py
Expand Up @@ -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!"

Expand All @@ -21,28 +21,34 @@
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:
# Nothing to see here! Skip.
# 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!")
1 change: 1 addition & 0 deletions pyproject.toml
Expand Up @@ -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"
Expand Down

0 comments on commit 8ba0aec

Please sign in to comment.