Intro
As @Ted Lyngmo mentioned above, it's best to use a parser that is specific to this network configuration format. Once such parser is ciscoconfparse2 (full disclosure: I am the author)—despite the name, it reads more than Cisco configuration files.
The advantage of using ciscoconfparse2 over a raw regular expression is that the code is more readable and maintainable.
regex technique with ciscoconfparse2
To get the list of subnets in object-group network Cloudflare
, read the format with ciscoconfparse2 and build a dict to hold the networks. There is no limit of object-group
statements you can read with the technique below.
With ciscoconfparse2 it's pretty easy...
from pprint import pprint
from ciscoconfparse2 import CiscoConfParse, IPv4Obj
config = """
network-object 1.1.1.1 0.0.0.0
object-group network Cloudflare
network-object 173.245.48.0 255.255.240.0
network-object 103.21.244.0 255.255.252.0
network-object 103.22.200.0 255.255.252.0
network-object 103.31.4.0 255.255.252.0
network-object 141.101.64.0 255.255.192.0
network-object 108.162.192.0 255.255.192.0
network-object 190.93.240.0 255.255.240.0
network-object 188.114.96.0 255.255.240.0
network-object 197.234.240.0 255.255.252.0
network-object 198.41.128.0 255.255.128.0
network-object 162.158.0.0 255.254.0.0
network-object 104.16.0.0 255.248.0.0
network-object 104.24.0.0 255.252.0.0
network-object 172.64.0.0 255.248.0.0
network-object 131.0.72.0 255.255.252.0
access-list outside
"""
parse = CiscoConfParse(config)
# Store all object-group names in a dict to list mapping
# key the dict by the object-group name and append the networks to each
object_groups = dict()
# If there are multiple object-groups, iterate over each one...
for object_group_cmd in parse.find_objects('^object-group'):
name = object_group_cmd.split()[2]
# Grab all object-group network-object commands at once with this regex...
networks = object_group_cmd.re_list_iter_typed('network-object\s+(\d.+)')
object_groups[name] = list()
for cmd in networks:
tmp = cmd.split()
network, netmask = tmp[0], tmp[1]
# Add each network and netmask to the list...
object_groups[name].append({'network': network, 'netmask': netmask})
pprint(object_groups)
That will print:
{'Cloudflare': [{'netmask': '255.255.240.0', 'network': '173.245.48.0'},
{'netmask': '255.255.252.0', 'network': '103.21.244.0'},
{'netmask': '255.255.252.0', 'network': '103.22.200.0'},
{'netmask': '255.255.252.0', 'network': '103.31.4.0'},
{'netmask': '255.255.192.0', 'network': '141.101.64.0'},
{'netmask': '255.255.192.0', 'network': '108.162.192.0'},
{'netmask': '255.255.240.0', 'network': '190.93.240.0'},
{'netmask': '255.255.240.0', 'network': '188.114.96.0'},
{'netmask': '255.255.252.0', 'network': '197.234.240.0'},
{'netmask': '255.255.128.0', 'network': '198.41.128.0'},
{'netmask': '255.254.0.0', 'network': '162.158.0.0'},
{'netmask': '255.248.0.0', 'network': '104.16.0.0'},
{'netmask': '255.252.0.0', 'network': '104.24.0.0'},
{'netmask': '255.248.0.0', 'network': '172.64.0.0'},
{'netmask': '255.255.252.0', 'network': '131.0.72.0'}]}
It should be noted that this gets all object-group network
statements at once...
subnets
with(?P<name>object-group network Cloudflare)\n(?P<subnets>(?: network-object (?:\d+\.){3}\d+ (?:\d+\.){3}\d+\n)*)
See regex101.com/r/OloB8E/1