# from scrapli.driver.core import IOSXEDriver # from scrapli.exceptions import ScrapliException # import socket # r1 = { # "host": "172.16.20.17", # "auth_username": "", # "auth_password": "", # "auth_secondary": "", # "auth_strict_key": False, # "transport": "telnet", # "port": 2022, # must be specified when connecting telnet # } # def send_show(device, show_command): # try: # with IOSXEDriver(**r1) as ssh: # reply = ssh.send_command(show_command) # return reply.result # except socket.timeout as error: # print(error) # except ScrapliException as error: # print(error, device["host"]) # if __name__ == "__main__": # output = send_show(r1, "sh ip int br") # print(output) from netmiko import ConnectHandler, NetmikoAuthenticationException, ReadTimeout, ConnectionException, NetMikoTimeoutException from getpass import getpass from pprint import pprint import sys type = "cisco_ios_telnet" host = sys.argv[2] username = "" password = "" R1 = { 'device_type':type, 'ip':host, 'username':username, 'password':password, 'port': int(sys.argv[3]), 'secret':'', } print(R1) # R1 = { # 'device_type':"cisco_ios_telnet", # 'ip': "172.16.20.1", # 'username':"", # 'password':"", # 'port': 2003, # # # 'secret':'', # } output = "" try: # print(R1) with ConnectHandler(**R1) as connect: connect.enable() output += connect.send_command_timing( "\n", strip_prompt=False, strip_command=False, delay_factor=400, last_read=5.0, read_timeout=500) if "[yes/no]" in output: output += connect.send_command_timing( "no", strip_prompt=False, strip_command=False, delay_factor=400) output += connect.send_command_timing( "erase /all non-default", strip_prompt=False, strip_command=False, delay_factor=400) connect.config_mode() output += connect.send_command_timing( "no system ignore startupconfig switch all", strip_prompt=False, strip_command=False, delay_factor=400, last_read=8.0, read_timeout=500) output += connect.send_command_timing( "end", strip_prompt=False, strip_command=False, delay_factor=400) connect.enable() check = connect.send_command_timing( "wr er", strip_prompt=False, strip_command=False, delay_factor=400) if "[confirm]" in check: check += connect.send_command_timing( "\n", strip_prompt=False, strip_command=False, delay_factor=400) output += check check = connect.send_command_timing( "er startup-config", strip_prompt=False, strip_command=False, delay_factor=400) if "[confirm]" in check: check += connect.send_command_timing( "\n", strip_prompt=False, strip_command=False, delay_factor=400) output += check check = connect.send_command_timing( "reload", strip_prompt=False, strip_command=False, delay_factor=400) output += check if "[yes/no]" in check: output += connect.send_command_timing( "no", strip_prompt=False, strip_command=False, delay_factor=400) output += connect.send_command_timing( "\n", strip_prompt=False, strip_command=False, delay_factor=400) print(output) except NetmikoAuthenticationException: print(NetmikoAuthenticationException) except NetMikoTimeoutException: print("NetMikoTimeoutException Error") except ConnectionRefusedError as err: print("Connection Refused Error") #Connection Refused except ValueError: print("Mode Error") except ReadTimeout: print("Error: Read Timeout") except ConnectionException: print("Connection Error")