Feb-19-2019, 11:08 AM
Hi All,
parsing configuration, and updating dict, in second loop I am parsing network routes, post matching, updating dict. But facing error, is this code correct?
parsing configuration, and updating dict, in second loop I am parsing network routes, post matching, updating dict. But facing error, is this code correct?
Error:Traceback (most recent call last):
File "ipstaticroutes-1.py", line 57, in <module>
for value in customer_details.items():
RuntimeError: dictionary changed size during iterationfrom ciscoconfparse import CiscoConfParse
from ciscoconfparse.ccp_util import IPv4Obj
from mysql.connector import Error
from mysql.connector import errorcode
import os
import re
from netaddr import *
import pprint
import mysql.connector
from ipaddress import *
if __name__ == "__main__":
global customer_details
customer_details = {}
#mydb = mysql.connector.connect(host="localhost",
# user='root',
# password='Compaqnx6320',
# database='illcustomers')
#mycursor = mydb.cursor()
# Reading all configuration files
conf_files_list = [x for x in os.listdir('.') if x.endswith('.conf')]
for files in conf_files_list:
confparse = CiscoConfParse(files)
# get the system name
system_regex_pattern = r"^sysname"
hostname = confparse.find_lines(system_regex_pattern)
for line in hostname:
sysname = line.strip().split(' ')[1]
#Start checking ip route-static block
ip_static_routes= confparse.find_blocks(r"^ip route-static ")
for lines in ip_static_routes:
#Find ip routes
routes = re.findall( r'[0-9]+(?:\.[0-9]+){3}', lines )
#list of ip route should be 3, Null0 routes are not considered
if (len(routes)) == 3:
route = (' '.join(routes))
lan_network = route.split(' ')
lan_ip_pool = lan_network[0]+"/"+lan_network[1]
#converted to subnet form
ip_add = IPNetwork(lan_ip_pool)
ip_address = str(ip_add)
next_hop = lan_network[2]
# required next hop
next_hop_int_ip = str(IPv4Address(next_hop)- 1)
customer_details.update({'sysname':sysname, 'ip_address': ip_address,'next_hop': next_hop, 'next_hop_int_ip':next_hop_int_ip})
#Checking for BGP network routes
ip_bgp_pools = confparse.find_blocks(r"network")
for bgp_subnets in ip_bgp_pools:
bgp_subnet = re.findall( r'[0-9]+(?:\.[0-9]+){3}',bgp_subnets )
if len(bgp_subnet) == 2:
bgp_network = (' '.join(bgp_subnet))
bgp_network = bgp_network.split(' ')
bgp_anouced_subnet = bgp_network[0]+"/"+bgp_network[1]
#Converted to subnet form
bgp_pool = IPNetwork(bgp_anouced_subnet)
#Checking BGP ip pools in Dict by comparing dict vlaue
for value in customer_details.items():
if (customer_details['ip_address']) == bgp_pool:
# updating dict if mact
customer_details.update({'bgp_status':bgp_pool})
else:
customer_details.update({'bgp_status': "no_bgp"})
print(customer_details)
