Bottom Page

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.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  How do i append into dict? Gateux 3 84 6 hours ago
Last Post: perfringo
  Pulling & Reading Date from UDF that Compare it to Live Data firebird 4 156 Jul-20-2019, 09:30 AM
Last Post: snippsat
  call func from dict mcmxl22 3 296 Jun-21-2019, 05:20 AM
Last Post: snippsat
  compare element of a dictionary Amniote 1 183 May-23-2019, 05:21 PM
Last Post: woooee
  call dict object result key error lateublegende 2 237 May-15-2019, 01:08 PM
Last Post: lateublegende
  How can I change value of dict in list? Mike Ru 1 154 May-13-2019, 09:06 PM
Last Post: ichabod801
  read json string as dict goron 3 217 May-08-2019, 01:43 PM
Last Post: buran
  Compare two large CSV files for a match Python_Newbie9 3 409 Apr-22-2019, 08:49 PM
Last Post: ichabod801
  how to compare some string with the database columns nick123 1 186 Apr-21-2019, 05:27 PM
Last Post: ichabod801
  Fastest dict/map method when 'key' is already a hash? tasket 6 523 Apr-20-2019, 06:40 PM
Last Post: tasket

Forum Jump:


Users browsing this thread: 1 Guest(s)