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
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_objects()
.children
attributere_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...
Running this code results in:
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