Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import telnetlib
- import os.path
- import subprocess
- import time
- import sys
- def telnet(command):
- username = "admin01"
- password = "cisco12345"
- port = 23
- connection_timeout = 5
- reading_timeout = 5
- connection = telnetlib.Telnet(ip_address, port, connection_timeout)
- router_output = connection.read_until(b"Username:", reading_timeout)
- connection.write(username.encode('ascii') + b"\n")
- router_output = connection.read_until(b"Password:", reading_timeout)
- connection.write(password.encode('ascii') + b"\n")
- time.sleep(1)
- connection.write(b"terminal length 0\n")
- time.sleep(1)
- connection.write(b"\n")
- connection.write(command.encode('ascii') + b"\n")
- time.sleep(5)
- router_output = connection.read_very_eager()
- connection.close()
- return router_output
- ################## USER MENU #################
- try:
- while True:
- print("\nPlease select your Task:\n1 - Compare Running-config with Startup-config\n2 - Exit program\n")
- user_option = input("Enter your choice: ")
- if user_option == "1":
- print("\nPlease wait while the config file is being compared...\n")
- output_run = telnet("show running-config")
- output_start = telnet("show startup-config")
- file_run = open("file_run.txt", "w")
- print(output_run.decode('ascii'), file=file_run)
- file_start = open("file_start.txt", "w")
- print(output_start.decode('ascii'), file=file_start)
- file_run.close()
- file_start.close()
- file_run = open("file_run.txt", "r")
- file_start = open("file_start.txt", "r")
- list_run = file_run.readlines()
- list_start = file_start.readlines()
- file_run.close()
- file_start.close()
- for index, element in enumerate(list_run):
- if "version " in element and "!\r\n" == list_run[list_run.index(element) - 1]:
- list_run[0:list_run.index(element)] = []
- for index, element in enumerate(list_start):
- if "version " in element and "!\r\n" == list_start[list_start.index(element) - 1]:
- list_start[0:list_start.index(element)] = []
- file_diff = open("file_diff.txt", "w")
- run_diff = [x for x in list_run if x not in list_start]
- for line in run_diff:
- print("+" + line, file=file_diff)
- start_diff = [x for x in list_start if x not in list_run]
- for line in start_diff:
- print("-" + line, file=file_diff)
- file_diff.close()
- else:
- print("Thanks for using the script\n\n")
- sys.exit()
- except KeyboardInterrupt:
- print("\n\nProgram aborted by user. Exiting...\n")
- sys.exit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement