Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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#
- import pexpect
- import getpass
- import re
- import sys
- import time
- import os
- def ssh(hostname, username='nadda', password='nadda', enable_pass='nadda'):
- count = 0
- hostname = hostname.lower()
- pass_prompt = re.compile('.*assword:')
- need_key = re.compile('s.*Are you sure you want to continue connecting (yes/no)?.', re.DOTALL)
- denied = re.compile('Permission denied, please try again.')
- refused = re.compile('Connection refused by remote host')
- if username is 'nadda' and password is 'nadda':
- username = raw_input('login as: ')
- password = getpass.getpass('Password:')
- print "connecting to " + hostname
- child = pexpect.spawn('ssh ' + username + '@' + hostname)
- print 'connected to ' + hostname
- i = child.expect([need_key, pass_prompt, pexpect.TIMEOUT, denied, refused, '.*>'])
- if i == 0:
- child.sendline('yes')
- print 'adding ssh key'
- child.expect(pass_prompt)
- else:
- print 'not adding ssh key'
- if i == 2:
- print 'SSH TIMED OUT ON ' + hostname
- exit()
- elif i == 3:
- print 'PERMISSION DENIED ON %s. Try re-running script with proper creds' % hostname
- child.close()
- exit()
- elif i == 4 and count == 0:
- print 'CONNECTION REFUSED ON %s' % hostname
- print 'Will try to reconnect once in 10 seconds...'
- time.sleep(10)
- while count < 1:
- ssh(hostname, username, password)
- count += 1
- elif i == 4 and count > 0:
- print 'Sorry, %s refused your connection more than once. '
- 'You'll have to re-run this script for %s' % (hostname, hostname[:3])
- exit()
- child.sendline(password)
- i = child.expect(['.#', '.*>'])
- if i == 1:
- child = enable(child, enable_pass)
- child.sendline('term length 0')
- child.expect('.#')
- print 'n'
- print 'ssh return from ' + hostname
- return child, username, password
- def shvlan(devices, child):
- fp = open("output.txt", 'w')
- for i in child:
- with open('commands.txt', 'r') as commands:
- for command in commands:
- i.sendline(command)
- i.expect('.*#')
- switch_name = re.findall(r'(w*-REDd{0,1}#|w*-BLUEd{0,1}#)', i.after)[0]
- new_input = switch_name + i.after.replace(switch_name, '')
- fp.write(new_input)
- i.close()
- fp.close()
- print 'sh run vlan executed'
- def data_parsing():
- 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*-REDd{0,1})', output)[0]
- switch_blue = re.findall(r'(w*-BLUEd{0,1})', output)[0]
- for line in open('./output.txt'):
- m = re.match(r'(w*-BLUEd{0,1}|w*-REDd{0,1})# sh run vlan d+', line)
- print m
- if m:
- if not if_found_vlan:
- data_cleaned[current_key].append([])
- #print current_key
- if_found_vlan = False
- current_key = m.group(1)
- if not data_cleaned.has_key(current_key):
- data_cleaned[current_key] = []
- #print current_key
- continue
- mm = re.match(r'vlan d+', line)
- if mm:
- if_found_vlan = True
- action_flag = True
- data_group = []
- #print data_group
- if action_flag and '' == line.strip():
- action_flag = False
- data_cleaned[current_key].append(data_group)
- #print current_key
- 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 confign "
- 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 confign "
- 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]))
- def main():
- try:
- device1, device2, username = sys.argv[1], sys.argv[2], sys.argv[3]
- except:
- print 'please use correct arguments'
- print 'EXAMPLE: python vlan-comp.py SWITCH1 SWITCH2 USERNAME'
- exit()
- password = getpass.getpass('Password:')
- enable_pass = getpass.getpass('Enable Password:')
- child1,username, password = ssh(device1, username, password, enable_pass)
- child2,username,password = ssh(device2, username, password, enable_pass)
- shvlan([device1, device2],[child1,child2])
- print 'n' * 5
- data_parsing()
- if __name__ == "__main__":
- main()
- 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'
Add Comment
Please, Sign In to add comment