match values against keys three dict - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: match values against keys three dict (/thread-16295.html) |
match values against keys three dict - anna - Feb-21-2019 Hi All, I have three dict post parsing configuration,need to match values, please guide from ciscoconfparse import CiscoConfParse from ciscoconfparse.ccp_util import IPv4Obj import os import re from netaddr import * import pprint from ipaddress import * #hostname = "DEL-CON-NE40E-X8-IAG-A5" if __name__ == "__main__": next_hops = [] conf_files_list = [x for x in os.listdir('.') if x.endswith('.conf')] for files in conf_files_list: #conf_process(files) confparse = CiscoConfParse(files) system_regex_pattern = r"^sysname" hostname = confparse.find_lines(system_regex_pattern) for line in hostname: sysname = line.strip().split(' ')[1] #print(sysname) ip_static_routes= confparse.find_blocks(r"^ip route-static ") for lines in ip_static_routes: #print(sysname,lines) routes = re.findall( r'[0-9]+(?:\.[0-9]+){3}', lines ) #print(len(routes)) if (len(routes)) == 3: route = (' '.join(routes)) lan_network = route.split(' ') lan_ip_pool = lan_network[0]+"/"+lan_network[1] ip = IPNetwork(lan_ip_pool) ip = str(ip) next_hop = lan_network[2] next_hops.append(next_hop) #bgp_peers = confparse.find_blocks " ip ip-prefix " ip_prefix = confparse.find_blocks(r"ip ip-prefix") bgp_prefix_dict = {} #dict for BGP prefixes for line in ip_prefix: if 'ip ip-prefix' in line: prefix_name = line.strip().split(' ')[2] subnet = line.strip().split(' ')[6]+"/"+line.strip().split(' ')[7] bgp_subnet = str(IPNetwork(subnet)) #update Dict bgp_prefix_dict.update({'prefix-name':prefix_name,'bgp-subnet':bgp_subnet}) print(bgp_prefix_dict) bgp_peers_dict = {} #dict for BGP peers bgp_peers = confparse.find_blocks(r" peer ") for peer in bgp_peers: if 'import' in peer: ip_addr = peer.strip().split(' ')[1] bgp_prefix_name = peer.strip().split(' ')[3] bgp_peers_dict.update({'ip_address':ip_addr, 'prefix_name':bgp_prefix_name}) print(bgp_peers_dict) bgp_as_ip = {} bgp_as_name = confparse.find_blocks(r"bgp 65000") for details in bgp_as_name: if 'as-number' in details: as_peer = details.strip().split(' ')[1] as_number = details.strip().split(' ')[3] bgp_as_ip.update({'as_peer':as_peer, 'as_num':as_number}) print(bgp_as_ip) wants to find and match values like'prefix_name': 'YOU-BROADBAND_1105047237 in first dict, get below values from second dict {'prefix-name': 'YOU-BROADBAND_1105047237', 'bgp-subnet': '103.246.212.0/22'} {'prefix-name': 'YOU-BROADBAND_1105047237', 'bgp-subnet': '123.201.0.0/16'} {'prefix-name': 'YOU-BROADBAND_1105047237', 'bgp-subnet': '175.100.128.0/19'} {'prefix-name': 'YOU-BROADBAND_1105047237', 'bgp-subnet': '203.88.128.0/19'} {'prefix-name': 'YOU-BROADBAND_1105047237', 'bgp-subnet': '203.109.64.0/18'} {'prefix-name': 'YOU-BROADBAND_1105047237', 'bgp-subnet': '203.187.192.0/18'} {'prefix-name': 'YOU-BROADBAND_1105047237', 'bgp-subnet': '219.91.128.0/17'} and match 'ip_address': '111.93.125.86' from second dict finally match {'as_peer': '111.93.125.86', 'as_num': '18207'} in third dict. and print out as {'as_peer': '111.93.125.86', 'as_num': '18207',{'prefix-name': 'YOU-BROADBAND_1105047237', 'bgp-subnet': '103.246.212.0/22'} 'bgp-subnet': '123.201.0.0/16','bgp-subnet': '175.100.128.0/19','bgp-subnet': '203.88.128.0/19','bgp-subnet': '203.109.64.0/18','bgp-subnet': '203.187.192.0/18','bgp-subnet': '219.91.128.0/17'} |