You essentially have two options...
OPTION A: Parse port numbers yourself, using stable and supported CiscoConfParse code...
import re
from ciscoconfparse import CiscoConfParse
CONFIG_PARSED = CiscoConfParse(CONFIG)
intf_rgx = re.compile(r'interface GigabitEthernet(\d+)\/(\d+)\/(\d+)$')
for obj in CONFIG_PARSED.find_objects('^interface GigabitEthernet'):
mm = intf_rgx.search(obj.text))
card = int(mm.group(1))
port = int(mm.group(3))
if card>2:
continue
if port>16:
continue
## Do something here with obj
OPTION B: Offload port number parsing to CiscoConfParse's alpha-quality (as of version 1.1.5) port number parser...
import re
from ciscoconfparse import CiscoConfParse
CONFIG_PARSED = CiscoConfParse(CONFIG, factory=True)
for obj in CONFIG_PARSED.find_objects('^interface GigabitEthernet'):
card = obj.ordinal_list[0]
port = obj.ordinal_list[-1]
if card>2:
continue
if port>16:
continue
## Do something here with obj
FYI, obj.ordinal_list
returns a python list of integers representing the card, slot and port number of the interface. For instance ordinal_list
for "GigabitEthernet2/0/11" is [2, 0, 11]
. You must use version 1.1.5 (or higher) and parse with factory=True
to get ordinal_list
.
DON'T DO THIS:
The example you provided is truly bad from a performance perspective, because find_objects()
walks the entire Cisco IOS configuration, start to finish looking for the regex provided. In case it isn't obvious, walking the entire Cisco IOS configuration 32 different times with find_objects()
is very slow.
CONFIG_PARSED = CiscoConfParse(CONFIG)
for i in range(1,3):
for j in range(1,17):
INT = CONFIG_PARSED.find_objects('^interface GigabitEthernet'+str(i)+'/0/'+str(j)+'$')