compare output with 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: compare output with dict (/thread-20530.html) |
compare output with dict - anna - Aug-16-2019 Hi All, I am parsing router configuration, however how can i match dict values with output and print/add matched line in dict. from ciscoconfparse import CiscoConfParse from ciscoconfparse.ccp_util import IPv4Obj import netaddr if __name__ == "__main__": # the result dictionary result = { "interfaces": {} } sr_no = 0 conf_file = "TTML-E320.conf" confparse = CiscoConfParse(conf_file) global_obj = confparse.find_objects(r'^hostname')[0] sysname = global_obj.re_match_typed(r'^hostname\s+(\S+)', default='').strip('"') interface_cmds = confparse.find_objects(r"^interface ") for interface_cmd in interface_cmds: intf_name = interface_cmd.text[len("interface "):] result["interfaces"][intf_name] = {} result["interfaces"][intf_name]["svlan"] = "not_set" for cmd in interface_cmd.re_search_children(r"^ svlan "): result["interfaces"][intf_name]["svlan"] = cmd.text.strip()[len(" svlan id"):].split(' ')[0] result["interfaces"][intf_name]["cvlan"] = cmd.text.strip()[len(" svlan id"):].split(' ')[1] result["interfaces"][intf_name]["status"] = "Active" for cmd in interface_cmd.re_search_children(r"^ shutdown"): result["interfaces"][intf_name]["status"] = "Shutdown" ip_static_routes_conf= confparse.find_blocks(r"^ip route ") for items in ip_static_routes_conf: if 'GigabitEthernet' in items: ip_addr = items.split(' ')[2] g_interface = items.split(' ')[4].strip()[len("GigabitEthernet"):] cidr_match = netaddr.all_matching_cidrs(ip_addr,["10.117.1.0/23","10.117.3.0/24"]) if cidr_match: print('{} {}'.format(ip_addr,g_interface)) for interface in result["interfaces"].items(): if 'Shutdown' in interface[1].values() or 'lag' in interface[0]: continue else: print(sr_no,sysname+";",interface[0]+";",";".join(interface[1].values())) sr_no +=1interfaces dict output seperated by ; sample sr.No Device Name Interface svlan cvlan status ip route outputsr.No IP interface here I want check eg. .. 2/0/4.101173989 in interfaces dict, post match add ip_addr in interfaces dict.
|