我正在使用 cisconfparse 模块和正则表达式编写一个 python 代码来循环遍历几个配置文件并找到配置了 dot1x pae 身份验证器和身份验证端口控制行的访问端口接口。如果我从返回的对象循环遍历每个接口,我就能够提取配置语句块。有没有办法只检索接口 GigabitEtherenet 线的第一行?
interface GigabitEthernet1/1
description USER_INTERFACE
switchport access vlan 19
switchport mode access
switchport port-security aging time 1
interface GigabitEthernet1/2
description USER_INTERFACE
switchport access vlan 19
switchport mode access
switchport voice vlan 154
authentication port-control auto
mab
块引用
import os
import re
import csv
from ciscoconfparse import CiscoConfParse
file_exists = os.path.isfile(r'c:\users\lang\documents\result.csv')
if not file_exists:
with open (r'c:\users\lang\documents\result.csv', 'w', newline='') as csv_file:
Header = ['Device', 'Vul ID', 'Exception', 'Status', 'Code', 'Severity', 'Reason']
writer = csv.DictWriter(csv_file, fieldnames=Header)
writer.writeheader()
def check_services():
configs = (r'C:\Users\Lang\Documents\Tutorials\Python\Scripts\NetworkAudit\Data')
for config in os.listdir(configs):
if config.endswith(".txt"):
filename = os.path.split(config)
#print(filename[1])
parse = CiscoConfParse(config)
intfs = parse.find_blocks('^\sswitchport\smode\saccess')
for int in intfs:
print(int)
dot1x_pae = re.findall('dot1x pae authenticator', int)
if(not dot1x_pae):
with open (r'c:\users\lang\documents\result.csv',
'a', newline='') as csv_file:
Header = ['Device', 'Vul ID', 'Exception',
'Status', 'Code', 'Severity', 'Reason']
writer = csv.DictWriter(csv_file,
fieldnames=Header)
writer.writerow({'Device':filename[1], 'Vul ID':
'V-220649', 'Exception': 'dot1x not configured', \
'Status': 'Failed', 'Code': '1',
'Severity': 'High', \
'Reason': int})
else:
with open (r'c:\users\lang\documents\result.csv',
'a', newline='') as csv_file:
Header = ['Device', 'Vul ID', 'Exception',
'Status', 'Code', 'Severity', 'Reason']
writer = csv.DictWriter(csv_file,
fieldnames=Header)
writer.writerow({'Device':filename[1], 'Vul ID':
'V220649', 'Exception': 'dot1x configured', \
'Status': 'Pass', 'Code': '1',
'Severity': 'High', \
'Reason': 'dot1x configured'})
check_services()