I have a text file that contains the following information:
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;
}
}
}
With the help of others I've been able to extract the interface id (ge-2/0/0) as well as the description.
The code for that is as follows:
from ciscoconfparse import CiscoConfParse
parse = CiscoConfParse("testconfig.txt", syntax="junos")
intfs = parse.find_objects_w_parents(r'^interfaces', r'^\s+ge-')
for intfobj in intfs:
intf_name = intfobj.text.strip()
descr = intfobj.re_match_iter_typed(r'description\s+"(\S.+?)"$', group=1)
print ('Intf: {0}, {1}'.format(intf_name, descr))
This gives me a result of:
Intf: ge-2/0/0, site1;;hostname1;ge-16/0/9;;;TRUST;
Intf: ge-2/0/2, site2;;hostname2;ge-16/0/8;;;TRUST;
So far that's been huge for me, and I really thought I was going to be able to figure out how to dig deeper into the interface to extract the "port-mode".
My attempts so far are failing me.
This is the general train of thought I had in trying to dig that info out but to no avail:
ltype = parse.find_objects_w_parents(r'^interfaces', r'^\s+ge-', r'^unit\s0', r'^family\sethernet-switching')
for ltypeobj in ltype:
pmode = intfobj.re_match_iter_typed(r'port-mode\s+"(\S.+?)"$', group=1)
print ('Port Mode: {0}'.format(pmode))
I get the following and I'm just not able to figure it out.
Traceback (most recent call last):
File "convert.py", line 11, in <module>
ltype = parse.find_objects_w_parents(r'^interfaces', r'^\s+ge-', r'^unit\s0', r'^family\sethernet-switching')
TypeError: find_objects_w_parents() takes from 3 to 4 positional arguments but 5 were given
Any advice on accomplishing this would be appreciated.
check if this example works for you
output: