Aug-01-2018, 07:12 AM
The below script i have to compare Test-red vs Test-blue and find the difference. .Test-red and Test-blue are kept in "output.txt" . output.txt content is mentioned in the bottom.
import re data_cleaned = {} current_key = '' action_flag = False data_group = [] if_found_vlan = True output = open('./output.txt','r').read() switch_red = re.findall(r'(\w*-RED\d{0,1})', output)[0] switch_blue = re.findall(r'(\w*-BLUE\d{0,1})', output)[0] for line in open('./output.txt'): m = re.match(r'(\w*-RED\d{0,1}|\w*-BLUE\d{0,1})# sh run vlan \d+', line) if m: if not if_found_vlan: data_cleaned[current_key].append([]) if_found_vlan = False current_key = m.group(1) if not data_cleaned.has_key(current_key): data_cleaned[current_key] = [] continue mm = re.match(r'vlan \d+', line) if mm: if_found_vlan = True action_flag = True data_group = [] if action_flag and '' == line.strip(): action_flag = False data_cleaned[current_key].append(data_group) if action_flag: data_group.append(line.replace('\r', '').replace('\n', '')) if not if_found_vlan: data_cleaned[current_key].append([]) #print ("+++++++++++++++++ The missing configuration ++++++++++++++\n") print switch_blue + "#" + " has below missing VLAN config\n " p = [item for index, item in enumerate(data_cleaned[switch_blue]) if [] != [it for it in item if it not in data_cleaned[switch_red][index]]] print('\n'.join(['\n'.join(item) for item in p])) print ("+++++++++++++++++++++++++++++++\n") print switch_red + "#" + " has below missing VLAN config\n " q = [item for index, item in enumerate(data_cleaned[switch_red]) if [] != [it for it in item if it not in data_cleaned[switch_blue][index]]] print('\n'.join(['\n'.join(item) for item in q]))output.txt is :
Test-red# sh run vlan 158 !Command: show running-config vlan 158 !Time: Mon Jul 23 08:34:49 2018 version 7.1(4)N1(1c) vlan configuration 158 vlan 158 name MARKETING mode fabricpath Test-red# sh run vlan 159 !Command: show running-config vlan 159 !Time: Mon Jul 23 08:34:53 2018 version 7.1(4)N1(1c) vlan configuration 159 vlan 159 Test-red# ------------------------------------ Test-blue# sh run vlan 158 !Command: show running-config vlan 158 !Time: Mon Jul 23 08:35:37 2018 version 7.1(4)N1(1c) vlan configuration 158 vlan 158 name MARKETING mode fabricpath Test-blue# sh run vlan 159 !Command: show running-config vlan 159 !Time: Mon Jul 23 08:35:42 2018 version 7.1(4)N1(1c) vlan configuration 159 vlan 159 name SALES mode fabricpath Test-blue#i am getting below error:
p = [item for index, item in enumerate(data_cleaned[switch_blue]) if [] != [it for it in item if it not in data_cleaned[switch_red][index]]] KeyError: 'Test-blue'Any idea how to fix it up ?