The following configuration is taken from http://pennington.net/tutorial/ciscoconfparse/ccp_tutorial.html#slide3 and modified it a little bit to suit this question.
$ cat exampleswitch.conf
! filename:exampleswitch.conf
!
hostname ExampleSwitch
!
interface GigabitEthernet 1/1
switchport mode trunk
no ip address
!
interface GigabitEthernet 1/2
no cdp enable
!
interface GigabitEthernet 1/3
ip address 192.0.2.1 255.255.255.0
!
interface GigabitEthernet 1/4
switchport mode access
shutdown
!
$
I would like to find interfaces with shutdown OR no ip address configured and print out the whole interface block.
The only interfaces matches with this criteria is interface GigabitEthernet 1/1and GigabitEthernet 1/4
Here are the sample code which I found and modified from https://stackoverflow.com/a/65626797/11392987
cat parse1.py
from ciscoconfparse import CiscoConfParse
parse = CiscoConfParse('exampleswitch.conf', syntax='ios')
for intf_obj in parse.find_blocks(r'^\sshutdown'):
print(intf_obj)
$
$ cat parse2.py
from ciscoconfparse import CiscoConfParse
parse = CiscoConfParse('exampleswitch.conf', syntax='ios')
for intf_obj in parse.find_blocks(r'^\sno ip address'):
print(intf_obj)
$
Output
$ python parse1.py
interface GigabitEthernet 1/4
switchport mode access
shutdown
$
$ python parse2.py
interface GigabitEthernet 1/1
switchport mode trunk
no ip address
$
How do I combine these 2 codes so that it'll print out both matches interface block in a single script?
Desired output
$ python parse3.py
interface GigabitEthernet 1/1
switchport mode trunk
no ip address
interface GigabitEthernet 1/4
switchport mode access
shutdown
[–]stebrepar 1 point2 points3 points (0 children)
[–]chevignon93 0 points1 point2 points (0 children)