Feb-20-2019, 11:04 AM
some weired results
from 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 = {} 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) #print(ip_address) 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}) print(customer_details) 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 ) #print(bgp_subnet) if len(bgp_subnet) == 2: bgp_network = (' '.join(bgp_subnet)) bgp_network = bgp_network.split(' ') bgp_annouced_subnet = bgp_network[0]+"/"+bgp_network[1] #Converted to subnet form bgp_pool = IPNetwork(bgp_annouced_subnet) bgp_pool = str(bgp_pool) #Checking BGP ip pools in Dict by comparing dict vlaue print(customer_details['ip_address']) for value in list(customer_details.items()): if customer_details['ip_address'] == bgp_pool: customer_details.update({'bgp_status': bgp_pool}) else: customer_details.update({'bgp_status': 'No'}) print(customer_details)Dict printing, no issues
Output:{'sysname': 'BAN:YES-NE40E-X8-IAG-B5', 'ip_address': '14.98.0.8/29', 'next_hop': '49.249.251.254', 'next_hop_int_ip': '49.249.251.253'}
{'sysname': 'BAN:YES-NE40E-X8-IAG-B5', 'ip_address': '14.98.0.40/29', 'next_hop': '182.156.235.250', 'next_hop_int_ip': '182.156.235.249'}
{'sysname': 'BAN:YES-NE40E-X8-IAG-B5', 'ip_address': '14.98.0.56/29', 'next_hop': '182.156.235.18', 'next_hop_int_ip': '182.156.235.17'}
{'sysname': 'BAN:YES-NE40E-X8-IAG-B5', 'ip_address': '14.98.0.88/29', 'next_hop': '49.249.251.222', 'next_hop_int_ip': '49.249.251.221'}
{'sysname': 'BAN:YES-NE40E-X8-IAG-B5', 'ip_address': '14.98.0.96/29', 'next_hop': '49.249.233.130', 'next_hop_int_ip': '49.249.233.129'}
{'sysname': 'BAN:YES-NE40E-X8-IAG-B5', 'ip_address': '14.98.0.104/29', 'next_hop': '61.12.65.150', 'next_hop_int_ip': '61.12.65.149'}
{'sysname': 'BAN:YES-NE40E-X8-IAG-B5', 'ip_address': '14.98.0.112/29', 'next_hop': '49.249.251.194', 'next_hop_int_ip': '49.249.251.193'}
{'sysname': 'BAN:YES-NE40E-X8-IAG-B5', 'ip_address': '14.98.0.128/29', 'next_hop': '49.249.251.238', 'next_hop_int_ip': '49.249.251.237'}
{'sysname': 'BAN:YES-NE40E-X8-IAG-B5', 'ip_address': '14.98.0.136/29', 'next_hop': '111.93.132.58', 'next_hop_int_ip': '111.93.132.57'}
{'sysname': 'BAN:YES-NE40E-X8-IAG-B5', 'ip_address': '14.98.0.192/29', 'next_hop': '111.93.151.50', 'next_hop_int_ip': '111.93.151.49'}
updating Dict and printing, 'ip_address' is same till loop completesOutput:14.98.0.192/29
{'sysname': 'BAN:YES-NE40E-X8-IAG-B5', 'ip_address': '14.98.0.192/29', 'next_hop': '111.93.151.50', 'next_hop_int_ip': '111.93.151.49', 'bgp_status': 'No'}
14.98.0.192/29
{'sysname': 'BAN:YES-NE40E-X8-IAG-B5', 'ip_address': '14.98.0.192/29', 'next_hop': '111.93.151.50', 'next_hop_int_ip': '111.93.151.49', 'bgp_status': 'No'}
14.98.0.192/29
{'sysname': 'BAN:YES-NE40E-X8-IAG-B5', 'ip_address': '14.98.0.192/29', 'next_hop': '111.93.151.50', 'next_hop_int_ip': '111.93.151.49', 'bgp_status': 'No'}
14.98.0.192/29
{'sysname': 'BAN:YES-NE40E-X8-IAG-B5', 'ip_address': '14.98.0.192/29', 'next_hop': '111.93.151.50', 'next_hop_int_ip': '111.93.151.49', 'bgp_status': 'No'}
14.98.0.192/29
{'sysname': 'BAN:YES-NE40E-X8-IAG-B5', 'ip_address': '14.98.0.192/29', 'next_hop': '111.93.151.50', 'next_hop_int_ip': '111.93.151.49', 'bgp_status': 'No'}
14.98.0.192/29
{'sysname': 'BAN:YES-NE40E-X8-IAG-B5', 'ip_address': '14.98.0.192/29', 'next_hop': '111.93.151.50', 'next_hop_int_ip': '111.93.151.49', 'bgp_status': 'No'}
14.98.0.192/29
{'sysname': 'BAN:YES-NE40E-X8-IAG-B5', 'ip_address': '14.98.0.192/29', 'next_hop': '111.93.151.50', 'next_hop_int_ip': '111.93.151.49', 'bgp_status': 'No'}
14.98.0.192/29
{'sysname': 'BAN:YES-NE40E-X8-IAG-B5', 'ip_address': '14.98.0.192/29', 'next_hop': '111.93.151.50', 'next_hop_int_ip': '111.93.151.49', 'bgp_status': 'No'}
14.98.0.192/29
{'sysname': 'BAN:YES-NE40E-X8-IAG-B5', 'ip_address': '14.98.0.192/29', 'next_hop': '111.93.151.50', 'next_hop_int_ip': '111.93.151.49', 'bgp_status': 'No'}
14.98.0.192/29
{'sysname': 'BAN:YES-NE40E-X8-IAG-B5', 'ip_address': '14.98.0.192/29', 'next_hop': '111.93.151.50', 'next_hop_int_ip': '111.93.151.49', 'bgp_status': 'No'}
14.98.0.192/29
{'sysname': 'BAN:YES-NE40E-X8-IAG-B5', 'ip_address': '14.98.0.192/29', 'next_hop': '111.93.151.50', 'next_hop_int_ip': '111.93.151.49', 'bgp_status': 'No'}
14.98.0.192/29
{'sysname': 'BAN:YES-NE40E-X8-IAG-B5', 'ip_address': '14.98.0.192/29', 'next_hop': '111.93.151.50', 'next_hop_int_ip': '111.93.151.49', 'bgp_status': '14.98.0.192/29'}
14.98.0.192/29
{'sysname': 'BAN:YES-NE40E-X8-IAG-B5', 'ip_address': '14.98.0.192/29', 'next_hop': '111.93.151.50', 'next_hop_int_ip': '111.93.151.49', 'bgp_status': 'No'}
14.98.0.192/29
{'sysname': 'BAN:YES-NE40E-X8-IAG-B5', 'ip_address': '14.98.0.192/29', 'next_hop': '111.93.151.50', 'next_hop_int_ip': '111.93.151.49', 'bgp_status': 'No'}
14.98.0.192/29
{'sysname': 'BAN:YES-NE40E-X8-IAG-B5', 'ip_address': '14.98.0.192/29', 'next_hop': '111.93.151.50', 'next_hop_int_ip': '111.93.151.49', 'bgp_status': 'No'}