pythonpython-3.xnetwork-programmingciscoconfparse

Can I use two ciscoconfparse objects in a single Cisco IOS config file to get specific interfaces


Trying to use two of the conf objects find_objects_w_child & find_objects_wo_child in a single file.

I need to find out "interfaces" from a Cisco config file which have a specific QoS "service-policy" command configured.

At the same time should not be a part of any Etherchannel.

Is it even possible to use these 2 objects on a same config file?


Solution

  • CiscoConfParse objects do not offer a method that allows you to find objects with specific children, but without other specific children. However, we can utilize a list comprehension to accomplish the same task with the IOSCfgLine object's re_search_children() method, as shown below:

    from ciscoconfparse import CiscoConfParse
    
    parse = CiscoConfParse("ios_cfg.txt")
    phys_intfs_w_qos = [obj for obj in parse.find_objects_wo_child(r"^interface", "channel-group") if obj.re_search_children(r"service-policy")]
    

    Because regex objects are truthy, the above list comprehension will only return IOSCfgLine objects representing interfaces that do not have channel-group configured, but does have service-policy configured.