Iterating through dictionary of dictionaries to get data from multiple routers behind a custom proxy using Netmiko

I’ve been wanting to improve my netmiko script which connects to a custom (non-ssh) proxy, connects to a few routers there and outputs some info.

I’m doing a few things here: first is a dictionary of dictionaries myrouters. Then I create an embedded loop which first goes through the main key (R1, R2, R3) and then through the subkeys of the embedded dictionary (Berlinrouter etc.).

Then I use a simple regex re.findall(r’Uptime .?minutes’, result) to only get uptime and another regex to get the serial number from output of the show version command.

Finally, i want to know how long eigrp tunnels have been up (because it’s not necessarily the same as router uptime) but the string that I get from show ip eigrp neighour is a bit complex so I split it into a list and take some elements from the list. By the way the way I do this below is really not optimal so reworking could involve textfsm templates (mentioned a few posts back) . Unfortunately I can’t spontaneously come up with a nice regex to only get what i want from the output even after grepping it.

from netmiko import ConnectHandler
import time
from netmiko import redispatch
import re
jumpserver= {'device_type':'terminal_server','ip':'1.1.1.1','username':'cisco','password':'cisco','port':22,'global_delay_factor':2,'session_log': 'output.txt'}
net_connect=ConnectHandler(**jumpserver)
net_connect.find_prompt()
myrouters = {"R1": {"FrankfurtRouter": "192.168.0.1"}, "R2": {"BerlinRouter": "192.168.0.2"}, "R3": {"Hamburgrouter": "192.168.0.3"}}
for mainkey in myrouters.keys():
mainval = myrouters[mainkey]
for subkey in mainval.keys():
subval = mainval[subkey]
print('i am now on router ' + mainkey + ' whose name is ' + subkey + ' and IP is ' + subval)
net_connect.write_channel("connect " + subval + " \n")
time.sleep(3)
redispatch(net_connect, device_type="cisco_ios")
net_connect.enable
result = net_connect.send_command_timing("show ver")
endresult = re.findall(r'Uptime .?minutes', result) endresult.insert(0,subkey)
print(endresult)
time.sleep(1)
newresult = net_connect.send_command_timing("show version")
endresultser = re.findall(r'Processor .', newresult)
endresultser.insert(0,subkey)
print(endresultser)
time.sleep(1)
eigrpresult = net_connect.send_command_timing("show ip eigrp vrf 1001 nei 172.16.0.1 | i Tu1001")
eigrpresult.split()
print("For " + subkey + " eigrp datatunnel uptime is " + eigrpresult[55:62])
net_connect.write_channel("exit\n")
time.sleep(1)

and the output…

i am now in mainkey R1 whose name is FrankfurtRouter and IP is 192.168.0.1
[‚FrankfurtRouter’, ‚Uptime for this control processor is 11 weeks, 3 hours, 53 minutes’]
[‚FrankfurtRouter’, ‚Processor board ID FCZ3557C1AL’]
For FrankfurtRouter eigrp datatunnel uptime is 4w4d
i am now in mainkey R2 whose name is BerlinRouter and IP is 192.168.0.2
[‚BerlinRouter’, ‚Uptime for this control processor is 41 weeks, 2 days, 17 hours, 18 minutes’]
[‚BerlinRouter’, ‚Processor board ID FCZ8445C1AH’]
For BerlinRouter eigrp datatunnel uptime is 4w4d
i am now in mainkey R3 whose name is Hamburgrouter and IP is 192.168.0.3
[‚Hamburgrouter’, ‚Uptime for this control processor is 29 weeks, 21 hours, 38 minutes’]
[‚Hamburgrouter’, ‚Processor board ID FCZ368990W7’]
For Hamburgrouter eigrp datatunnel uptime is 2d06h

One important thing to mention here is that I use some time.sleep stuff, which I personally don’t like. It would be a better idea to use pexpect to wait for router prompts, but pexpect library won’t work on python in Windows. You’d need to run it from a Linux machine.

Skomentuj

Wprowadź swoje dane lub kliknij jedną z tych ikon, aby się zalogować:

Logo WordPress.com

Komentujesz korzystając z konta WordPress.com. Wyloguj /  Zmień )

Zdjęcie na Google

Komentujesz korzystając z konta Google. Wyloguj /  Zmień )

Zdjęcie z Twittera

Komentujesz korzystając z konta Twitter. Wyloguj /  Zmień )

Zdjęcie na Facebooku

Komentujesz korzystając z konta Facebook. Wyloguj /  Zmień )

Połączenie z %s