Python Forum
match values against keys three dict
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
match values against keys three dict
#1
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)
Output:
{'ip_address': '14.98.66.130', 'prefix_name': 'Uzaina'} {'ip_address': '14.98.82.190', 'prefix_name': 'WI_LINK_12105830371'} {'ip_address': '14.98.85.202', 'prefix_name': 'MORADABAD_INTERNET_59105832429'} {'ip_address': '14.141.116.137', 'prefix_name': 'TCL-IN'} {'ip_address': '49.249.232.238', 'prefix_name': 'SIMSYS/564205818072'} {'ip_address': '49.249.232.254', 'prefix_name': 'BRAINPULSE_12005807126'} {'ip_address': '49.249.236.22', 'prefix_name': 'GUAVUS_NETWORK'} {'ip_address': '111.93.41.130', 'prefix_name': 'PROGRESSION_INFONET'} {'ip_address': '111.93.52.54', 'prefix_name': 'LEADING_EDGE_COMMUICATIONS_1105066669'} {'ip_address': '111.93.58.218', 'prefix_name': 'CHI_NETWORKS'} {'ip_address': '111.93.121.102', 'prefix_name': 'DENY_PRIVATE_IP'} {'ip_address': '111.93.125.86', 'prefix_name': 'YOU-BROADBAND_1105047237'} {'ip_address': '111.93.184.102', 'prefix_name': 'Tata_Teleservices'} {'ip_address': '111.93.184.122', 'prefix_name': 'CLICKSOFTWARE_012405300802'} {'ip_address': '111.93.184.254', 'prefix_name': 'MGRM_DIGITAL_CLOUD'} {'ip_address': '111.93.193.18', 'prefix_name': 'VERISIGN_SERVICES'} {'ip_address': '111.93.195.90', 'prefix_name': 'TOLUNA_INDIA'} {'ip_address': '111.93.249.190', 'prefix_name': 'BLUE_SKY_BROADBAND'} {'prefix-name': 'Tata_Teleservices', 'bgp-subnet': '59.161.164.0/22'} {'prefix-name': 'Tata_Teleservices', 'bgp-subnet': '115.114.244.128/25'} {'prefix-name': 'CLICKSOFTWARE_012405300802', 'bgp-subnet': '103.26.168.0/24'} {'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'} {'prefix-name': 'RATEGAIN_TECHNOLOGIES_12005796261', 'bgp-subnet': '103.211.187.0/24'} {'prefix-name': 'RATEGAIN_TECHNOLOGIES_12005796261', 'bgp-subnet': '103.211.186.0/24'} {'prefix-name': 'RATEGAIN_TECHNOLOGIES_12005796261', 'bgp-subnet': '103.242.87.0/24'} {'as_peer': '14.98.66.130', 'as_num': '138287'} {'as_peer': '14.98.82.190', 'as_num': '135778'} {'as_peer': '14.98.85.202', 'as_num': '135182'} {'as_peer': '14.141.116.137', 'as_num': '4755'} {'as_peer': '49.249.232.238', 'as_num': '135797'} {'as_peer': '49.249.232.254', 'as_num': '55711'} {'as_peer': '49.249.236.22', 'as_num': '132282'} {'as_peer': '111.93.41.130', 'as_num': '132760'} {'as_peer': '111.93.52.54', 'as_num': '64522'} {'as_peer': '111.93.58.218', 'as_num': '132542'} {'as_peer': '111.93.121.102', 'as_num': '64543'} {'as_peer': '111.93.125.86', 'as_num': '18207'} {'as_peer': '111.93.184.102', 'as_num': '65384'} {'as_peer': '111.93.184.122', 'as_num': '132871'} {'as_peer': '111.93.184.254', 'as_num': '131256'} {'as_peer': '111.93.193.18', 'as_num': '16838'} {'as_peer': '111.93.195.90', 'as_num': '24103'} {'as_peer': '111.93.249.190', 'as_num': '134945'} {'as_peer': '182.156.72.98', 'as_num': '45820'} {'as_peer': '182.156.197.254', 'as_num': '55441'} {'as_peer': '182.156.211.142', 'as_num': '134975'} {'as_peer': '2403:0:400::271', 'as_num': '4755'}
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'}
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  dict class override: how access parent values? Andrey 1 1,594 Mar-06-2022, 10:49 PM
Last Post: deanhystad
  Updating nested dict list keys tbaror 2 1,243 Feb-09-2022, 09:37 AM
Last Post: tbaror
  Loop Dict with inconsistent Keys Personne 1 1,579 Feb-05-2022, 03:19 AM
Last Post: Larz60+
  Removing nan values from a dict tomtom 8 6,871 Oct-05-2021, 06:44 PM
Last Post: tomtom
  Create Dict from multiple Lists with duplicate Keys rhat398 10 3,987 Jun-26-2021, 11:12 AM
Last Post: Larz60+
  Adding keys and values to a dictionary giladal 3 2,430 Nov-19-2020, 04:58 PM
Last Post: deanhystad
  extracting second values elements only in every keys in an OrderedDict glennford49 4 2,470 Nov-12-2020, 08:22 AM
Last Post: glennford49
  Trouble with converting list , dict to int values! faryad13 7 3,671 Sep-04-2020, 06:25 AM
Last Post: faryad13
  access dictionary with keys from another and write values to list redminote4dd 6 3,163 Jun-03-2020, 05:20 PM
Last Post: DeaD_EyE
  Sort a dict in dict cherry_cherry 4 62,804 Apr-08-2020, 12:25 PM
Last Post: perfringo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020