mpenning / ciscoconfparse

Parse, Audit, Query, Build, and Modify Arista / Cisco / Juniper / Palo Alto / F5 configurations.
http://www.pennington.net/py/ciscoconfparse/
GNU General Public License v3.0
788 stars 219 forks source link

NXOS configure profile parsing #197

Closed haught closed 3 years ago

haught commented 3 years ago

Having a bit of trouble parsing my cisco nexus devices that are using configure profiles

configure profile Example_net
  vlan 810
    vn-segment 810
    name Example_net-Priv
  interface Vlan810
    description My example
    vrf member example-1
    no ip redirects
    no ipv6 redirects
    ip address 10.9.8.1/24 tag 12345
    fabric forwarding mode anycast-gateway
    no shutdown
  interface nve1
    member vni 810
      mcast-group 239.1.1.0
  evpn
    vni 810 l2
      rd auto
      route-target import auto
      route-target export auto

I am trying to get the vlan interface data but it appears the it is overwritten by the nve1 interface that comes after. Doing a simple:

test = confparse.find_children_w_parents(r"^configure profile", r"interface")
print(test)

just gives me the nve1 interfaces:

['  interface nve1', '  interface nve1' ....]
mpenning commented 3 years ago

Hello @haught ,

I can't reproduce this problem yet... here's what I tried:

from ciscoconfparse import CiscoConfParse

config = """
configure profile Example_net
  vlan 810
    vn-segment 810
    name Example_net-Priv
  interface Vlan810
    description My example
    vrf member example-1
    no ip redirects
    no ipv6 redirects
    ip address 10.9.8.1/24 tag 12345
    fabric forwarding mode anycast-gateway
    no shutdown
  interface nve1
    member vni 810
      mcast-group 239.1.1.0
  evpn
    vni 810 l2
      rd auto
      route-target import auto
      route-target export auto
"""

parse = CiscoConfParse(config.splitlines())
results = parse.find_children_w_parents(r"^configure profile", r"interface")
print(results)

When I run this under ciscoconfparse==1.5.29 in linux, I get the following output:

$ python issue.py
['  interface Vlan810', '  interface nve1']
mpenning commented 3 years ago

If it was me, I would use find_objects_w_parents(). That will get the vlan interface object for you... at this point, just call all_children on that vlan inteface object to get the children.


# as_text_list() is only available in version 1.5.30+
from ciscoconfparse.ccp_util import as_text_list
from ciscoconfparse import CiscoConfParse

config = """
configure profile Example_net
  vlan 810
    vn-segment 810
    name Example_net-Priv
  interface Vlan810
    description My example
    vrf member example-1
    no ip redirects
    no ipv6 redirects
    ip address 10.9.8.1/24 tag 12345
    fabric forwarding mode anycast-gateway
    no shutdown
  interface nve1
    member vni 810
      mcast-group 239.1.1.0
  evpn
    vni 810 l2
      rd auto
      route-target import auto
      route-target export auto
"""

parse = CiscoConfParse(config.splitlines())
vlan_intf_obj = parse.find_objects_w_parents(r"configure\s+profile", r"interface\s+Vlan\s*\d+")[0]
#
# Use this prior to version 1.5.30...
#results = list(map(attrgetter("text"), vlan_intf_obj.all_children))
#
# Use this in version 1.5.30 and afterwards...
results =as_text_list(vlan_intf_obj.all_children)
print(results)

This prints out the following...

]$ python issue.py
['    description My example', '    vrf member example-1', '    no ip redirects', '    no ipv6 redirects', '    ip address 10.9.8.1/24 tag 12345', '    fabric forwarding mode anycast-gateway', '    no shutdown']
haught commented 3 years ago

Thank you so much! I don't know what I was doing for it to act so oddly. Your guidance was extremely helpful. Everything is working for me now, sorry I should have banged at it a bit more.

Also thank you for your work on writing this project, it has been a life saver for my project to map subnets to vlans in routing domains across our campus. --Matt