1

Given the following code:

https://bpaste.net/show/dd44a1fa01dc

from ciscoconfparse import CiscoConfParse
from pprint import pprint
parse = CiscoConfParse("testconfig.txt", syntax="junos")

interfaces = {}
intfs = parse.find_objects_w_parents(r'^interface', r'^\s+ge-')
for intfobj in intfs:
    intf_name = intfobj.text.strip()
    interfaces.update({'name': intf_name})
    descr = intfobj.re_match_iter_typed(r'description\s+"(\S.+?)"$', group=1)
    interfaces.update({'description': descr})
    mode = intfobj.re_match_iter_typed(r'port-mode\s+(\S+)\s*$', group=1,
        all_children=True)
    interfaces.update({'mode': mode})


print (interfaces)

Using the following test data:

https://bpaste.net/show/df422a96aaae

interfaces {
    ge-2/0/0 {
        description "site1;;hostname1;ge-16/0/9;;;TRUST;";
        unit 0 {
            family ethernet-switching {
                port-mode trunk;
            }
        }
    }
    ge-2/0/2 {
        description "site2;;hostname2;ge-16/0/8;;;TRUST;";
        unit 0 {
            family ethernet-switching {
                port-mode trunk;
            }
        }
    }

    vstp {
        bpdu-block-on-edge;
        vlan VLAN_0005 {
            interface ge-2/0/0 {
                edge;
            }
        }
        vlan VLAN_0015 {
            interface ge-2/0/0 {
                edge;
            }
            interface ge-2/0/2 {
                edge;                   
            }
        }  
    }
}

I'm trying to understand why my interfaces = {} just contains one row:

{'name': 'ge-2/0/2', 'description': 'site2;;hostname2;ge-16/0/8;;;TRUST;', 'mode': 'trunk'}

I would expect it to contain both interfaces from the test data:

{'name': 'ge-2/0/0', 'description': 'site1;;hostname1;ge-16/0/9;;;TRUST;', 'mode': 'trunk'}
{'name': 'ge-2/0/2', 'description': 'site2;;hostname2;ge-16/0/8;;;TRUST;', 'mode': 'trunk'}
ddevalco
  • 1,209
  • 10
  • 20
  • 2
    `interfaces` is a dict. It can't contain multiple "rows". You need a list of dicts or similar. – Michael Butscher Sep 24 '18 at 22:59
  • 1
    `dict`s are mappings, not sequences. Each key is associated (or mapped) to a single value. That "single" value can be a `list` or other nested container, however. – martineau Sep 24 '18 at 23:08
  • Any chance you have an example? I'm without a doubt getting confused about things. Interfaces is the dict, within the dict each ge-x/x/x should be a new key with values of desciption and port-mode – ddevalco Sep 24 '18 at 23:11

2 Answers2

1

You are creating a dict and updating its values. So you end up with a dict with last values. You could rather use a list of dicts, as such :

from ciscoconfparse import CiscoConfParse
from pprint import pprint
parse = CiscoConfParse("testconfig.txt", syntax="junos")

interfaces = []
intfs = parse.find_objects_w_parents(r'^interface', r'^\s+ge-')
for intfobj in intfs:
    interface = {}
    interface['name'] = intfobj.text.strip()
    interface['description'] = intfobj.re_match_iter_typed(r'description\s+"(\S.+?)"$', group=1)
    interface['mode'] = intfobj.re_match_iter_typed(r'port-mode\s+(\S+)\s*$', group=1,
        all_children=True)
    interfaces.append(interface)

print (interfaces)
Loïc
  • 11,804
  • 1
  • 31
  • 49
0

Could try something like this

allInterfaces = {}
interfaces = {}
intfs = parse.find_objects_w_parents(r'^interface', r'^\s+ge-')
for intfobj in intfs:
    intf_name = intfobj.text.strip()
    interfaces.update({'name': intf_name})
    descr = intfobj.re_match_iter_typed(r'description\s+"(\S.+?)"$', group=1)
    interfaces.update({'description': descr})
    mode = intfobj.re_match_iter_typed(r'port-mode\s+(\S+)\s*$', group=1,
        all_children=True)
    interfaces.update({'mode': mode})
    allInterfaces = allInterfaces + [interfaces,]

print (allInterfaces)

Chai Ang
  • 474
  • 1
  • 6
  • 11