def parse_c_map(ws): row = 1 # open all .txt files in the current directory for filename in glob.glob('*.txt'): config = CiscoConfParse(filename, factory=True, syntax='ios') hostname = config.find_objects_dna(r'Hostname')[0].hostname ios_ver = config.find_lines('IOS')[0][len('!! IOS XR Configuration '):] print('... %s (IOS XR %s)' % (hostname, ios_ver)) for be in config.find_objects_w_child(r'^interface.+Bundle[^.]+$', 'service-policy output'): for parent in be.re_search_children('service-policy'): parent_policy = parent.text[len(' service-policy output '):] p_map = config.find_all_children(r'^policy-map ' + parent_policy + '$') child_policy = [i for i in p_map if 'service-policy' in i ][0][len(' service-policy '):] for c_policy in config.find_objects('policy-map ' + child_policy + '$'): for c_map in c_policy.re_search_children('class'): # write the hostname in column A ws.write(row, 0, hostname) # write the IOS XR version in column B ws.write(row, 1, ios_ver) # write child policy in column C ws.write(row, 2, child_policy) # write class maps in column D ws.write(row, 3, c_map.text[len(' class '):]) for police_rate in c_map.re_search_children('police'): # write police rate in column E ws.write(row, 4, police_rate.text.strip()) if 'vlan' in c_map.text.lower( ) or 'default' in c_map.text: ws.write(row, 5, 'Ok') else: ws.write(row, 5, 'Incorrect class map.') row += 1 print('... Completed %s class maps.' % row) # set the autofilter ws.autofilter(0, 0, row, 5)
# Script to find what interfaces have an "ip helper-address" # Uses ciscoconfparse library, make sure its installed #Importing the necessary modules. import os from ciscoconfparse import CiscoConfParse os.chdir("c:\\configs") for filename in os.listdir(os.getcwd()): parse = CiscoConfParse(filename, factory=True, syntax='ios') obj_list = parse.find_objects_dna(r'Hostname') inf_w_help = parse.find_parents_w_child(parentspec=r"^interface", childspec=r"ip helper-address") hostn = obj_list[0].hostname print hostn for interface in inf_w_help: print interface print("Write results to file...") newconfig = CiscoConfParse([]) newconfig.append_line(hostn) for interface in inf_w_help: newconfig.append_line(interface) newconfig.append_line('ip helper-address my.new.ip.add1') newconfig.commit() newconfig.save_as(hostn + '_newconfig.txt')
def parse_ingress(ws): row = 1 # open all .txt files in the current directory for filename in glob.glob('*.txt'): config = CiscoConfParse(filename, factory=True, syntax='ios') hostname = config.find_objects_dna(r'Hostname')[0].hostname ios_ver = config.find_lines('IOS')[0][len('!! IOS XR Configuration '):] print('... %s (IOS XR %s)' % (hostname, ios_ver)) for ports in config.find_objects_w_child('^interface.+l2transport', 'service-policy input'): # write the hostname in column A ws.write(row, 0, hostname) # write the IOS XR version in column B ws.write(row, 1, ios_ver) # write the ingress port in column C port = ports.text[len('interface '):-len(' l2transport')] ws.write(row, 2, port) for line in ports.re_search_children(r'^ description '): description = line.text[len('description '):] ws.write(row, 3, description) for maps in ports.re_search_children('service-policy'): # write the ingress policy map in column D policy_map = maps.text[len(' service-policy input '):] ws.write(row, 4, policy_map) policy_maps = config.find_all_children(policy_map) policy_rate = [i for i in policy_maps if 'police rate' in i][0].strip() cos = [i for i in policy_maps if 'set cos' in i][0].strip() ws.write(row, 5, policy_rate) # write the COS setting in column F ws.write(row, 6, cos) # determine the circuit type based on the policy names if 'aovl' in policy_map.lower(): circuit_type = 'Adva Overlay' elif ('test' in policy_map.lower()) or ('mef' in policy_map.lower()): circuit_type = 'Test' elif ('nid' in policy_map.lower()) or ('dcn' in policy_map.lower()): circuit_type = 'DCN' else: circuit_type = 'Customer' # write the circuit type in column G ws.write(row, 7, circuit_type) if circuit_type == 'Test': if 'cos 4' in cos: ws.write(row, 8, 'Ok') else: ws.write( row, 8, 'Matched keyword test and/ or MEF.' 'Please confirm if this is a test policy and delete. ' 'If not, change/ add set cos 4.') elif circuit_type == 'Adva Overlay': if 'cos 6' in cos: ws.write(row, 8, 'Ok') else: ws.write(row, 8, 'Change to set cos 6.') elif circuit_type == 'DCN': if ('cos 4' in cos) or ('cos 7' in cos): ws.write(row, 8, 'Ok') else: ws.write(row, 8, 'DCN circuit - change set to cos 7.') elif circuit_type == 'Customer': if 'cos 4' in cos: ws.write(row, 8, 'Ok') else: ws.write(row, 8, 'Change/ add set cos 4.') cos = '' row += 1 print('... Completed %s ingress policies.' % row) # set the autofilter ws.autofilter(0, 0, row, 8)
def parse_egress(ws): row = 1 # open all .txt files in the current directory for filename in glob.glob('*.txt'): config = CiscoConfParse(filename, factory=True, syntax='ios') hostname = config.find_objects_dna(r'Hostname')[0].hostname ios_ver = config.find_lines('IOS')[0][len('!! IOS XR Configuration '):] print('... %s (IOS XR %s)' % (hostname, ios_ver)) # for be in config.find_objects_w_child(r'^interface.+Bundle[^.]+$', 'service-policy output'): for be in config.find_objects(r'^interface.+Bundle[^.]+$'): # write the hostname in column A ws.write(row, 0, hostname) # write the IOS XR version in column B ws.write(row, 1, ios_ver) # write the BE port in column C be_port = be.text[len('interface '):] ws.write(row, 2, be_port) # write the description in column D for line in be.re_search_children(r'^ description '): description = line.text[len('description '):] ws.write(row, 3, description) # pre-fill remarks for be ports without parent policies ws.write(row, 6, 'Missing parent and/ or child policy.') for parent in be.re_search_children('service-policy'): # write the parent policy in column E parent_policy = parent.text[len(' service-policy output '):] parent_policy1 = parent.text[len(' service-policy input '):] ws.write(row, 4, parent.text.strip()) # write the child policy in column F try: p_map = config.find_all_children(r'^policy-map ' + parent_policy + '$') child_policy = [i for i in p_map if 'service-policy' in i ][0][len(' service-policy '):] ws.write(row, 5, child_policy) except Exception: p_map = config.find_all_children(r'^policy-map ' + parent_policy1 + '$') child_policy = [i for i in p_map if 'service-policy' in i ][0][len(' service-policy '):] ws.write(row, 5, child_policy) if 'input' in parent.text: ws.write( row, 6, 'Policy incorrectly applied on ingress instead of egress.' ) elif be_port[len('Bundle-Ether'):] == parent_policy[len('policy_port_BE'):] \ and parent_policy[len('policy_port_BE'):] == child_policy[len('policy_BVID_BE'):]: ws.write(row, 6, 'Ok') else: ws.write(row, 6, 'Incorrect parent and/ or child policy.') row += 1 print('... Completed %s egress policies.' % row) # set the autofilter ws.autofilter(0, 0, row, 6)