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':'','username':'cisco','password':'cisco','port':22,'global_delay_factor':2,'session_log': 'output.txt'}
myrouters = {"R1": {"FrankfurtRouter": ""}, "R2": {"BerlinRouter": ""}, "R3": {"Hamburgrouter": ""}}
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")
redispatch(net_connect, device_type="cisco_ios")
result = net_connect.send_command_timing("show ver")
endresult = re.findall(r'Uptime .?minutes', result) endresult.insert(0,subkey)
newresult = net_connect.send_command_timing("show version")
endresultser = re.findall(r'Processor .', newresult)
eigrpresult = net_connect.send_command_timing("show ip eigrp vrf 1001 nei | i Tu1001")
print("For " + subkey + " eigrp datatunnel uptime is " + eigrpresult[55:62])

and the output…

i am now in mainkey R1 whose name is FrankfurtRouter and IP is
[‚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
[‚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
[‚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.


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


Komentujesz korzystając z konta 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