以下示例将使用此配置,该配置取自http://pennington.net/tutorial/ciscoconfparse/ccp_tutorial.html#slide3
! filename:exampleswitch.conf
!
hostname ExampleSwitch
!
interface GigabitEthernet 1/1
switchport mode trunk
shutdown
!
interface GigabitEthernet 1/2
switchport mode access
switchport access vlan 20
switchport nonegotiate
no cdp enable
!
interface GigabitEthernet 1/3
no switchport
ip address 192.0.2.1 255.255.255.0
这是也取自 http://pennington.net/tutorial/ciscoconfparse/ccp_tutorial.html#slide7的代码
from ciscoconfparse import CiscoConfParse
parse = CiscoConfParse('exampleswitch.conf', syntax='ios')
for intf_obj in parse.find_objects_w_child('^interface', '^\s+shutdown'):
print("Shutdown: " + intf_obj.text)
输出
$ python script.py
Shutdown: interface GigabitEthernet 1/1
$
代码工作得很好。但不仅仅是显示Shutdown: interface GigabitEthernet 1/1
,是否可以interface GigabitEthernet 1/1
在输出中显示整个块,即:
interface GigabitEthernet 1/1
switchport mode trunk
shutdown