我有数百个 cisco 配置文件,我需要(通过 python)找到在这种情况下 WIRELESS-IN 应用的具有特定服务策略的接口。我用来捕获接口的正则表达式是: pat=re.compile('(interface.*?)!$',re.DOTALL|re.M)
FastEthernet1/0/2
在这种情况下,我需要返回。
样本数据可能是这样的:
我有一个正则表达式来将界面元素匹配到一个组中,但不是如何在该组中查找。任何人都可以帮忙吗?
interface FastEthernet1/0/1
description Foo
switchport access vlan 300
switchport mode access
switchport port-security aging time 2
no logging event link-status
speed 100
duplex full
priority-queue out
mls qos trust dscp
no snmp trap link-status
no cdp enable
spanning-tree portfast
hold-queue 120 in
hold-queue 200 out
ip dhcp snooping trust
!
interface FastEthernet1/0/2
description wlap2
switchport access vlan 100
switchport mode access
switchport port-security maximum 15
switchport port-security
switchport port-security aging time 2
switchport port-security aging type inactivity
ip access-group 100 in
no logging event link-status
srr-queue bandwidth shape 0 0 0 10
queue-set 2
priority-queue out
no snmp trap link-status
storm-control broadcast level pps 100 50
storm-control multicast level pps 100 50
storm-control action trap
spanning-tree portfast
spanning-tree bpduguard enable
service-policy input WIRELESS_IN
ip dhcp snooping limit rate 15
!
interface FastEthernet1/0/3
description Test3
switchport access vlan 199
switchport mode access
switchport port-security aging time 2
no logging event link-status
queue-set 2
priority-queue out
mls qos trust dscp
no snmp trap link-status
no cdp enable
spanning-tree portfast
service-policy input VOICE-LAN
我拼凑了这段代码:
import re,string
f = open("sampleconfig.cfg")
cfgdata = f.read()
pat=re.compile('(interface.*?)!$',re.DOTALL|re.M)
pat2 = re.compile("service-policy.input.WIRELESS-IN")
data = pat.findall(cfgdata)
i=0
while i < len(data):
if pat2.findall(data[i]):
print (data[i].split("\n")[0])
i = i+1
else:
i = i+1
pass