'(Python) Netmiko : Compare the configuration of a router with a template
I want to do a comparison between a router configuration and template which is in a txt file using netmiko library.
using the "show run" command to display all the router configuration, the comparison must be done block by block for example: the "access-list snmp" block from the output of the command with the "access-list snmp" block from template etc because some permet x.x.x.x are repeated in 3 ACL and I have to check that they are in these 3 ACL.
I don't know how to do this comparison, if you have any ideas please help me.
I tried to do it with this code but here the comparison is done line by line :
cisco = {
'device_type': 'cisco_ios',
'host': 'router',
'username': 'admin',
'password': 'cisco123',
}
try
ssh = ConnectHandler(**cisco)
ssh.send_command('terminal length 0')
output = ssh.send_command("show run")
except Exception as e:
# exceptions
try:
template_file = open("template.txt", "r")
for l in file:
line = l.strip()
if line not in output:
f = open("ligne_not_in_config.txt", "a")
f.write(l, "is not in config\n")
except FileNotFoundError as e:
# exceptions
Solution 1:[1]
My sample code comparing two lists in notepad is as follows. You can use this.
ARBOR = []
with open('MO_ARBOR.txt') as f:
lines = f.readlines()
lines2 = "".join(lines)
lines3 = lines2.split("/32")
lines4 = "".join(lines3)
lines5 = lines4.strip()
lines6 = lines5.split()
#print(lines6)
with open('UPE_PUBLIC.txt') as f:
lines10 = f.readlines()
lines20 = "".join(lines10)
lines30 = lines20.replace("\n",",")
lines30 = lines30.split(",")
while '' in lines30: # A method that removes specific characters. (for example "" > empty)
lines30.remove('')
for i in lines30:
if i in lines6:
print(i + ":Tan?m Var")
with open('var.txt', 'a') as f:
f.write(i +" "+"OK"+"\n")
else:
print(i + ":Tan?m Yok")
with open('yok.txt', 'a') as f:
f.write(i +" "+"NOK"+"\n")
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | tripleee |
