Reputation: 134
I am learning Python module ciscoconfparse to loop thru each interface in the config files and find all interfaces that are configured with switchport mode access. I parsed the config files with find_blocks method and it outputs the result as expected below. I also want to loop thru each returned interfaces and search for the dot1x pae authenticator line. If it finds, then return the interface name that was configured with dot1x pae authenticator. I tried the following codes but it didn't work yet. Please help. thanks
interface GigabitEthernet1/1
switchport mode access
switchport access vlan 10
dot1x pae authenticator
interface GigabitEthernet1/2
switchport mode access
switchport voice vlan 154
dot1x pae authenticator
The code is below:
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)
all_intfs = parse.find_blocks('switchport mode access')
for intf in all_intfs:
print(intf)
dot1x = re.search("^\sdot1x\spae\sauthenticator", all_intfs)
print(dot1x)
check_services()
Upvotes: 0
Views: 132
Reputation: 1016
You can get filtered result using following:
parse = CiscoConfParse(config)
# Return a list of all interfaces with switchport mode access
mode_access_intfs = parse.find_objects_w_child(r"^interf", r"switchport mode access")
# For each interface above, print out relevant information...
for obj in mode_access_intfs:
# Find dot1x mode
has_dotx = obj.re_match_iter_typed(r'^\s*(dot1x pae authenticator).*$', default='')
if has_dotx:
# Print out what we found...
print("-----")
print("Object: {0}".format(obj))
print(" Interface config line: {0}".format(obj.text))
print(" has dotx mode: {0}".format(has_dotx))
Check on replit: https://replit.com/@arvindDhakad/QuickwittedOldlaceSet#main.py
examples: https://github.com/mpenning/ciscoconfparse/tree/master/examples
Upvotes: 1