Guest User

Untitled

a guest
Aug 16th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.68 KB | None | 0 0
  1. for host in hosts:
  2. host = host.strip()
  3. try:
  4. fd = open(‘/PATH/’, 'w') #capture output in file
  5. sys.stdout = fd
  6. device = ConnectHandler(device_type=platform, ip=host, username=username, password=password)
  7. time.sleep(1.5)
  8. #print("DEVICE %s\n" % (host))
  9. output= device.send_command("sh command”) #Show Comm e.g. MQC check
  10. time.sleep(1.5)
  11. print(output)
  12.  
  13. #Diff a file
  14. fn1 = ('/PATH/') #original template e.g. MQC template
  15. fn2 = ('/PATH/') #Captured via show
  16.  
  17. # Open file for reading in text mode (default mode)
  18. f1 = open(fn1)
  19. f2 = open(fn2)
  20.  
  21. fd1 = open('/PATH/', 'a') #File to output diff
  22. old_stdout = sys.stdout
  23. sys.stdout = fd1
  24. # Print confirmation
  25. print("-----------------------------------")
  26. print("Comparing files ", " > " + fn1, " < " + fn2, sep='\n')
  27. print("-----------------------------------")
  28. print("DEVICE %s\n" % (host))
  29. # Read the first line from the files
  30. f1_line = f1.readline()
  31. f2_line = f2.readline()
  32.  
  33. # Initialize counter for line number
  34. line_no = 1
  35.  
  36. # Loop if either file1 or file2 has not reached EOF
  37. while f1_line != '' or f2_line != '':
  38.  
  39. # Strip the leading whitespaces
  40. f1_line = f1_line.rstrip()
  41. f2_line = f2_line.rstrip()
  42.  
  43. # Compare the lines from both file
  44. if f1_line != f2_line:
  45.  
  46. # If a line does not exist on file2 then mark the output with + sign
  47. if f2_line == '' and f1_line != '':
  48. print("Line Not in Original Template >+", "Line-%d" % line_no, f1_line)
  49. # otherwise output the line on file1 and mark it with > sign
  50. elif f1_line != '':
  51. print(">", "Line-%d" % line_no, f1_line)
  52.  
  53. # If a line does not exist on file1 then mark the output with + sign
  54. if f1_line == '' and f2_line != '':
  55. print("Line in Current Device NOT in Template <+", "Line-%d" % line_no, f2_line)
  56. # otherwise output the line on file2 and mark it with < sign
  57. elif f2_line != '':
  58. print("<", "Line-%d" % line_no, f2_line)
  59.  
  60. # Print a blank line
  61. print()
  62.  
  63. # Read the next line from the file
  64. f1_line = f1.readline()
  65. f2_line = f2.readline()
  66.  
  67. # Increment line counter
  68. line_no += 1
  69.  
  70. # Close the files
  71.  
  72. except (NoValidConnectionsError, SSHException, NetMikoTimeoutException):
  73. print("SSH is not enabled for %s\n" % (host))
  74. continue
Add Comment
Please, Sign In to add comment