目标字符s='''!interfaceFastEthernet0/46!interfaceFastEthernet0/47switchportaccessvlan221switchportmodeaccessspanning-treeportfast!interfaceFastEthernet0/48switchportacces...
目标字符
s = '''!
interface FastEthernet0/46
!
interface FastEthernet0/47
switchport access vlan 221
switchport mode access
spanning-tree portfast
!
interface FastEthernet0/48
switchport access vlan 220
switchport mode access
dot1x port-control auto
spanning-tree portfast
!
interface GigabitEthernet0/1
switchport trunk encapsulation dot1q
switchport mode trunk
!'''
p = '(?:interface\s)((?:\w+)+\d+(?:/\d+)*)(?:\n\sswitchport\s)?([a|t]\w+)?(?:\s\w+\s)?(\d+)?(?:\n)?(?:.+\n)?\s?(dot1x)?'
re.findall(p,s)
输出结果是:
[('FastEthernet0/46', '', '', ''), ('FastEthernet0/47', 'access', '221', ''), ('FastEthernet0/48', 'access', '220', 'dot1x'), ('GigabitEthernet0/1', 'trunk', '', '')]
当要想再增加一个匹配项时:
p = '(?:interface\s)((?:\w+)+\d+(?:/\d+)*)(?:\n\sswitchport\s)?([a|t]\w+)?(?:\s\w+\s)?(\d+)?(?:\n)?(?:.+\n)?\s?(dot1x)?(?:.+\n)?\s?(spanning-tree)?'
输出结果是:
[('FastEthernet0/46', '', '', '', ''), ('FastEthernet0/48', 'access', '220', 'dot1x', 'spanning-tree'), ('GigabitEthernet0/1', 'trunk', '', '', '')]
少了('FastEthernet0/47', 'access', '221', '')
需要的信息:FastEthernet0/48,access|truck,VlanId,dot1x,spanning-tree这些项目,因dot1x不是每个接口都出现,故影响到spanning-tree的匹配。
求助,如何能匹配完美,谢谢。
p = '(?:interface\s)((?:\w+)+\d+(?:/\d+)*)\r\n(?:!|\sswitchport\s([at]\w+)(?:\s\w+\s)(\d+)?(?:\r\n)?(?:.+\r\n)?(?:\s)?(dot1x .+[^\r\n])?(?:\r\n)?(?:\s)?(spanning-tree portfast)?)'
('FastEthernet0/1', 'access', '211', '', 'spanning-tree portfast')
('FastEthernet0/4', 'access', '221', 'dot1x port-control auto ', 'spanning-tree portfast')
以上采用python第三方cisco包完成与交换机交互。
交换机show running-config后进行匹配,每行已\r\n结尾。
展开