Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.92 KB | None | 0 0
  1. import telnetlib
  2. import os.path
  3. import subprocess
  4. import time
  5. import sys
  6.  
  7.  
  8. def telnet(command):
  9.  
  10. username = "admin01"
  11. password = "cisco12345"
  12.  
  13. port = 23
  14. connection_timeout = 5
  15. reading_timeout = 5
  16. connection = telnetlib.Telnet(ip_address, port, connection_timeout)
  17. router_output = connection.read_until(b"Username:", reading_timeout)
  18. connection.write(username.encode('ascii') + b"\n")
  19.  
  20. router_output = connection.read_until(b"Password:", reading_timeout)
  21. connection.write(password.encode('ascii') + b"\n")
  22. time.sleep(1)
  23. connection.write(b"terminal length 0\n")
  24. time.sleep(1)
  25.  
  26. connection.write(b"\n")
  27. connection.write(command.encode('ascii') + b"\n")
  28. time.sleep(5)
  29.  
  30. router_output = connection.read_very_eager()
  31. connection.close()
  32.  
  33. return router_output
  34.  
  35. ################## USER MENU #################
  36. try:
  37.  
  38. while True:
  39. print("\nPlease select your Task:\n1 - Compare Running-config with Startup-config\n2 - Exit program\n")
  40.  
  41. user_option = input("Enter your choice: ")
  42.  
  43. if user_option == "1":
  44.  
  45.  
  46. print("\nPlease wait while the config file is being compared...\n")
  47.  
  48. output_run = telnet("show running-config")
  49. output_start = telnet("show startup-config")
  50.  
  51. file_run = open("file_run.txt", "w")
  52. print(output_run.decode('ascii'), file=file_run)
  53.  
  54. file_start = open("file_start.txt", "w")
  55. print(output_start.decode('ascii'), file=file_start)
  56.  
  57. file_run.close()
  58. file_start.close()
  59.  
  60. file_run = open("file_run.txt", "r")
  61.  
  62. file_start = open("file_start.txt", "r")
  63.  
  64. list_run = file_run.readlines()
  65.  
  66. list_start = file_start.readlines()
  67.  
  68. file_run.close()
  69. file_start.close()
  70.  
  71.  
  72. for index, element in enumerate(list_run):
  73. if "version " in element and "!\r\n" == list_run[list_run.index(element) - 1]:
  74. list_run[0:list_run.index(element)] = []
  75.  
  76.  
  77. for index, element in enumerate(list_start):
  78. if "version " in element and "!\r\n" == list_start[list_start.index(element) - 1]:
  79. list_start[0:list_start.index(element)] = []
  80.  
  81.  
  82. file_diff = open("file_diff.txt", "w")
  83.  
  84. run_diff = [x for x in list_run if x not in list_start]
  85.  
  86. for line in run_diff:
  87.  
  88. print("+" + line, file=file_diff)
  89.  
  90.  
  91. start_diff = [x for x in list_start if x not in list_run]
  92.  
  93. for line in start_diff:
  94.  
  95. print("-" + line, file=file_diff)
  96.  
  97. file_diff.close()
  98.  
  99. else:
  100. print("Thanks for using the script\n\n")
  101. sys.exit()
  102.  
  103. except KeyboardInterrupt:
  104. print("\n\nProgram aborted by user. Exiting...\n")
  105. sys.exit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement