Guest User

Untitled

a guest
Aug 2nd, 2018
593
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.84 KB | None | 0 0
  1. Test-RED# sh run vlan 158
  2.  
  3. !Command: show running-config vlan 158
  4. !Time: Mon Jul 23 08:34:49 2018
  5.  
  6. version 7.1(4)N1(1c)
  7. vlan configuration 158
  8. vlan 158
  9. name MARKETING
  10. mode fabricpath
  11.  
  12. Test-RED# sh run vlan 159
  13.  
  14. !Command: show running-config vlan 159
  15. !Time: Mon Jul 23 08:34:53 2018
  16.  
  17. version 7.1(4)N1(1c)
  18. vlan configuration 159
  19. vlan 159
  20.  
  21.  
  22. Test-RED#
  23. ------------------------------------
  24. Test-BLUE# sh run vlan 158
  25.  
  26. !Command: show running-config vlan 158
  27. !Time: Mon Jul 23 08:35:37 2018
  28.  
  29. version 7.1(4)N1(1c)
  30. vlan configuration 158
  31. vlan 158
  32. name MARKETING
  33. mode fabricpath
  34.  
  35. Test-BLUE# sh run vlan 159
  36.  
  37. !Command: show running-config vlan 159
  38. !Time: Mon Jul 23 08:35:42 2018
  39.  
  40. version 7.1(4)N1(1c)
  41. vlan configuration 159
  42. vlan 159
  43. name SALES
  44. mode fabricpath
  45.  
  46.  
  47. Test-BLUE#
  48.  
  49. import pexpect
  50. import getpass
  51. import re
  52. import sys
  53. import time
  54. import os
  55.  
  56.  
  57. def ssh(hostname, username='nadda', password='nadda', enable_pass='nadda'):
  58. count = 0
  59. hostname = hostname.lower()
  60. pass_prompt = re.compile('.*assword:')
  61. need_key = re.compile('s.*Are you sure you want to continue connecting (yes/no)?.', re.DOTALL)
  62. denied = re.compile('Permission denied, please try again.')
  63. refused = re.compile('Connection refused by remote host')
  64.  
  65. if username is 'nadda' and password is 'nadda':
  66. username = raw_input('login as: ')
  67. password = getpass.getpass('Password:')
  68. print "connecting to " + hostname
  69.  
  70. child = pexpect.spawn('ssh ' + username + '@' + hostname)
  71. print 'connected to ' + hostname
  72. i = child.expect([need_key, pass_prompt, pexpect.TIMEOUT, denied, refused, '.*>'])
  73. if i == 0:
  74. child.sendline('yes')
  75. print 'adding ssh key'
  76. child.expect(pass_prompt)
  77. else:
  78. print 'not adding ssh key'
  79.  
  80. if i == 2:
  81. print 'SSH TIMED OUT ON ' + hostname
  82. exit()
  83. elif i == 3:
  84. print 'PERMISSION DENIED ON %s. Try re-running script with proper creds' % hostname
  85. child.close()
  86. exit()
  87. elif i == 4 and count == 0:
  88. print 'CONNECTION REFUSED ON %s' % hostname
  89. print 'Will try to reconnect once in 10 seconds...'
  90. time.sleep(10)
  91. while count < 1:
  92. ssh(hostname, username, password)
  93. count += 1
  94. elif i == 4 and count > 0:
  95. print 'Sorry, %s refused your connection more than once. '
  96. 'You'll have to re-run this script for %s' % (hostname, hostname[:3])
  97. exit()
  98.  
  99. child.sendline(password)
  100. i = child.expect(['.#', '.*>'])
  101. if i == 1:
  102. child = enable(child, enable_pass)
  103.  
  104. child.sendline('term length 0')
  105. child.expect('.#')
  106. print 'n'
  107.  
  108. print 'ssh return from ' + hostname
  109.  
  110. return child, username, password
  111.  
  112.  
  113. def shvlan(devices, child):
  114. fp = open("output.txt", 'w')
  115. for i in child:
  116. with open('commands.txt', 'r') as commands:
  117. for command in commands:
  118. i.sendline(command)
  119. i.expect('.*#')
  120. switch_name = re.findall(r'(w*-REDd{0,1}#|w*-BLUEd{0,1}#)', i.after)[0]
  121. new_input = switch_name + i.after.replace(switch_name, '')
  122. fp.write(new_input)
  123. i.close()
  124. fp.close()
  125. print 'sh run vlan executed'
  126.  
  127.  
  128. def data_parsing():
  129. data_cleaned = {}
  130. current_key = ''
  131. action_flag = False
  132. data_group = []
  133. if_found_vlan = True
  134.  
  135. output = open('./output.txt','r').read()
  136.  
  137. switch_red = re.findall(r'(w*-REDd{0,1})', output)[0]
  138. switch_blue = re.findall(r'(w*-BLUEd{0,1})', output)[0]
  139. for line in open('./output.txt'):
  140. m = re.match(r'(w*-BLUEd{0,1}|w*-REDd{0,1})# sh run vlan d+', line)
  141. print m
  142. if m:
  143. if not if_found_vlan:
  144. data_cleaned[current_key].append([])
  145. #print current_key
  146. if_found_vlan = False
  147.  
  148. current_key = m.group(1)
  149. if not data_cleaned.has_key(current_key):
  150. data_cleaned[current_key] = []
  151. #print current_key
  152. continue
  153.  
  154. mm = re.match(r'vlan d+', line)
  155. if mm:
  156. if_found_vlan = True
  157. action_flag = True
  158. data_group = []
  159. #print data_group
  160. if action_flag and '' == line.strip():
  161. action_flag = False
  162. data_cleaned[current_key].append(data_group)
  163. #print current_key
  164. if action_flag:
  165. data_group.append(line.replace('r', '').replace('n', ''))
  166.  
  167. if not if_found_vlan:
  168. data_cleaned[current_key].append([])
  169. #print ("+++++++++++++++++ The missing configuration ++++++++++++++n")
  170. print switch_blue + "#" + " has below missing VLAN confign "
  171. 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]]]
  172. print('n'.join(['n'.join(item) for item in p]))
  173. print ("+++++++++++++++++++++++++++++++n")
  174. print switch_red + "#" + " has below missing VLAN confign "
  175. 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]]]
  176. print('n'.join(['n'.join(item) for item in q]))
  177.  
  178.  
  179. def main():
  180. try:
  181. device1, device2, username = sys.argv[1], sys.argv[2], sys.argv[3]
  182. except:
  183. print 'please use correct arguments'
  184. print 'EXAMPLE: python vlan-comp.py SWITCH1 SWITCH2 USERNAME'
  185. exit()
  186.  
  187. password = getpass.getpass('Password:')
  188. enable_pass = getpass.getpass('Enable Password:')
  189.  
  190.  
  191.  
  192. child1,username, password = ssh(device1, username, password, enable_pass)
  193. child2,username,password = ssh(device2, username, password, enable_pass)
  194.  
  195. shvlan([device1, device2],[child1,child2])
  196.  
  197.  
  198. print 'n' * 5
  199. data_parsing()
  200.  
  201.  
  202. if __name__ == "__main__":
  203. main()
  204.  
  205. 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]]]
  206. KeyError: 'Test-BLUE'
Add Comment
Please, Sign In to add comment