Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- for host in hosts:
- host = host.strip()
- try:
- fd = open(‘/PATH/’, 'w') #capture output in file
- sys.stdout = fd
- device = ConnectHandler(device_type=platform, ip=host, username=username, password=password)
- time.sleep(1.5)
- #print("DEVICE %s\n" % (host))
- output= device.send_command("sh command”) #Show Comm e.g. MQC check
- time.sleep(1.5)
- print(output)
- #Diff a file
- fn1 = ('/PATH/') #original template e.g. MQC template
- fn2 = ('/PATH/') #Captured via show
- # Open file for reading in text mode (default mode)
- f1 = open(fn1)
- f2 = open(fn2)
- fd1 = open('/PATH/', 'a') #File to output diff
- old_stdout = sys.stdout
- sys.stdout = fd1
- # Print confirmation
- print("-----------------------------------")
- print("Comparing files ", " > " + fn1, " < " + fn2, sep='\n')
- print("-----------------------------------")
- print("DEVICE %s\n" % (host))
- # Read the first line from the files
- f1_line = f1.readline()
- f2_line = f2.readline()
- # Initialize counter for line number
- line_no = 1
- # Loop if either file1 or file2 has not reached EOF
- while f1_line != '' or f2_line != '':
- # Strip the leading whitespaces
- f1_line = f1_line.rstrip()
- f2_line = f2_line.rstrip()
- # Compare the lines from both file
- if f1_line != f2_line:
- # If a line does not exist on file2 then mark the output with + sign
- if f2_line == '' and f1_line != '':
- print("Line Not in Original Template >+", "Line-%d" % line_no, f1_line)
- # otherwise output the line on file1 and mark it with > sign
- elif f1_line != '':
- print(">", "Line-%d" % line_no, f1_line)
- # If a line does not exist on file1 then mark the output with + sign
- if f1_line == '' and f2_line != '':
- print("Line in Current Device NOT in Template <+", "Line-%d" % line_no, f2_line)
- # otherwise output the line on file2 and mark it with < sign
- elif f2_line != '':
- print("<", "Line-%d" % line_no, f2_line)
- # Print a blank line
- print()
- # Read the next line from the file
- f1_line = f1.readline()
- f2_line = f2.readline()
- # Increment line counter
- line_no += 1
- # Close the files
- except (NoValidConnectionsError, SSHException, NetMikoTimeoutException):
- print("SSH is not enabled for %s\n" % (host))
- continue
Add Comment
Please, Sign In to add comment