Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
compare output with dict
#1
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 +=1
interfaces dict output seperated by ;
sample
sr.No Device Name Interface svlan cvlan status
Output:
3893 TTML-E320; gigabitEthernet 4/0/4.741286; 74;1286;Active 3894 TTML-E320; gigabitEthernet 4/0/4.741893; 74;1893;Active 3895 TTML-E320; gigabitEthernet 4/0/4.742350; 74;2350;Active 3896 TTML-E320; gigabitEthernet 4/0/4.742369; 74;2369;Active 3897 TTML-E320; gigabitEthernet 4/0/4.742806; 74;2806;Active 3898 TTML-E320; gigabitEthernet 4/0/4.742819; 74;2819;Active 3899 TTML-E320; gigabitEthernet 4/0/4.742862; 74;2862;Active 3900 TTML-E320; gigabitEthernet 4/0/4.742887; 74;2887;Active 3901 TTML-E320; gigabitEthernet 4/0/4.743078; 74;3078;Active 3902 TTML-E320; gigabitEthernet 4/0/4.743479; 74;3479;Active 3903 TTML-E320; gigabitEthernet 4/0/4.751523; 75;1523;Active 3904 TTML-E320; gigabitEthernet 4/0/4.751531; 75;1531;Active 3905 TTML-E320; gigabitEthernet 4/0/4.753137; 75;3137;Active
ip route output
sr.No IP interface

Output:
293 10.117.3.70 2/0/5.1011745 294 10.117.3.71 2/0/5.1011716 295 10.117.3.72 2/0/5.1011709 296 10.117.3.73 4/0/5.1171775 297 10.117.3.74 4/0/5.1171775 298 10.117.3.76 2/0/5.1011712 299 10.117.3.77 2/0/5.1011707 300 10.117.3.78 2/0/5.1011701 301 10.117.3.79 2/0/4.101173990 302 10.117.3.80 2/0/5.101173985 303 10.117.3.81 2/0/5.1011708 304 10.117.3.82 2/0/5.1011711 305 10.117.3.84 2/0/5.1011704 306 10.117.3.85 2/0/5.101173964 307 10.117.3.86 4/0/5.10117643 308 10.117.3.87 2/0/4.101173989
here I want check eg. .. 2/0/4.101173989 in interfaces dict, post match add ip_addr in interfaces dict.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Sort a dict in dict cherry_cherry 4 62,808 Apr-08-2020, 12:25 PM
Last Post: perfringo
  Compare Two Values in the Same Dict malonn 6 4,343 Aug-01-2018, 03:54 PM
Last Post: malonn
  Compare several strings and output average TotallyCoolGuy 2 2,848 Jan-28-2018, 05:07 PM
Last Post: TotallyCoolGuy
  Keyword compare in two files and output the results kotigasp 2 2,814 Jan-23-2018, 08:13 AM
Last Post: buran

Forum Jump:

User Panel Messages

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