Python Module CiscoConfParse returns only the 1st IPv6 address on an interface

536 views Asked by At

I'm trying to use CiscoConfParse on Cisco IOS config where the Interfaces have more than IPv6 address and I'm only getting the 1st IP address. Code, input file and output below What am I doing wrong here ? Any guidance appreciated.

    confparse = CiscoConfParse("ipv6_ints.txt")

    # extract the interface name and description
    # first, we get all interface commands from the configuration
    interface_cmds = confparse.find_objects(r"^interface ")

    # iterate over the resulting IOSCfgLine objects
    for interface_cmd in interface_cmds:
        # get the interface name (remove the interface command from the configuration line)
        intf_name = interface_cmd.text[len("interface "):]
        result["interfaces"][intf_name] = {}

        IPv6_REGEX = (r"ipv6\saddress\s(\S+)")
        for cmd in interface_cmd.re_search_children(IPv6_REGEX):
           ipv6_addr = interface_cmd.re_match_iter_typed(IPv6_REGEX, result_type=IPv6Obj)
           result["interfaces"][intf_name].update({
              "ipv6": {
              "ipv6 address": ipv6_addr.compressed,
              }
            })

    print("\nEXTRACTED PARAMETERS\n")
    print(json.dumps(result, indent=4))

Input file

1

There are 1 answers

0
Mike Pennington On

You are right that re_match_iter_typed() only returns the first match, so it's not a good fit for this application.

I suggest something along these lines:

  • Find the interface object as usual, using find_objects()
  • Iterate over all the child objects of the interface object using the .children attribute
  • Use re_match_typed() on each child object (with a default value so you can easily detect whether you got a IPv6 address match or not).

Example code below...


import re

from ciscoconfparse.ccp_util import IPv6Obj
from ciscoconfparse import CiscoConfParse

CONFIG = """!
interface Vlan150
 no ip proxy-arp
 ipv6 address FE80:150::2 link-local
 ipv6 address 2A01:860:FE:1::1/64
 ipv6 enable
!
interface Vlan160
 no ip proxy-arp
 ipv6 address FE80:160::2 link-local
 ipv6 address 2A01:870:FE:1::1/64
 ipv6 enable
!"""

parse = CiscoConfParse(CONFIG.splitlines())

result = dict()
result['interfaces'] = dict()
for intf_obj in parse.find_objects(r'^interface'):
    intf_name = re.split(r'\s+', intf_obj.text)[-1]
    result['interfaces'][intf_name] = dict()

    IPV6_REGEX = r'ipv6\s+address\s+(\S+)'
    for val_obj in intf_obj.children:

        val = val_obj.re_match_typed(IPV6_REGEX, result_type=IPv6Obj,
            untyped_default=True, default='__not_addr__')
        if val!='__not_addr__':
            # Do whatever you like here...
            print("{} {}".format(intf_name, val.compressed))

Running this code results in:

$ python try.py
Vlan150 fe80:150::2/128
Vlan150 2a01:860:fe:1::1/64
Vlan160 fe80:160::2/128
Vlan160 2a01:870:fe:1::1/64
$

Of course, you can pack these results into json using whatever packing scheme you want.

This technique is explained in the docs, under Get Config Values