net_connect.write_channel("\r") time.sleep(0.5) net_connect.write_channel("\r") time.sleep(0.5) output = net_connect.read_channel() print(output) # Now login to the end device try: # If "sername" isn't present assume we are already logged in from a previous session if "sername" in output: net_connect.username = "******" net_connect.password = end_device_pwd net_connect.std_login() net_connect.secret = end_device_pwd net_connect.set_base_prompt() print(net_connect.find_prompt()) # We are fully logged into the end device; we now must switch the Netmiko class redispatch(net_connect, device_type="cisco_ios_telnet") print(net_connect) print(net_connect.send_command("show ip int brief")) net_connect.enable() print(net_connect.send_config_set("logging buffered 30000")) net_connect.write_channel("exit\r") time.sleep(0.5) finally: net_connect.disconnect()
from netmiko import ConnectHandler from datetime import datetime from getpass import getpass password = getpass() cisco_ios = { 'host': 'cisco4.lasthop.io', 'username': '******', 'password': password, 'secret': password, 'device_type': 'cisco_ios', 'session_log': 'my_output.txt', } net_connect = ConnectHandler(**cisco_ios) #print prompt net_connect.enable() print(net_connect.find_prompt()) #write channel 'disable' net_connect.write_channel('disable\n') print(net_connect.find_prompt()) #read channel output = net_connect.read_channel() print(output) net_connect.secret = getpass() net_connect.enable() print(net_connect.enable()) net_connect.find_prompt() print(net_connect.find_prompt()) net_connect.disconnect()
# my terminal server. device = { "device_type": "cisco_s300", "host": public_ip, "username": "******", "password": s300_pass, "session_log": "output.txt", } # Initial connection to the S300 switch net_connect = ConnectHandler(**device) print(net_connect.find_prompt()) # Update the password as the terminal server uses different credentials net_connect.password = term_serv_pass net_connect.secret = term_serv_pass # Telnet to the terminal server command = f"telnet {terminal_server_ip}\n" net_connect.write_channel(command) # Use the telnet_login() method to handle the login process net_connect.telnet_login() print(net_connect.find_prompt()) # Made it to the terminal server (this terminal server is "cisco_ios") # Use redispatch to re-initialize the right class. redispatch(net_connect, device_type="cisco_ios") net_connect.enable() print(net_connect.find_prompt()) # Now connect to the end-device via the terminal server (Juniper SRX2) net_connect.write_channel("srx2\n")