1#!/usr/bin/env python 2""" 3This is a complicated example, but it illustrates both using a terminal server 4and bouncing through multiple devices. 5 6It also illustrates using 'redispatch()' to dynamically switch the Netmiko class 7 8The setup here is: 9 10Linux Server --(ssh)--> Small Switch --(telnet)--> Terminal Server --(serial)--> Juniper SRX 11""" 12import os 13from getpass import getpass 14from netmiko import ConnectHandler, redispatch 15 16# Hiding these IP addresses 17terminal_server_ip = os.environ["TERMINAL_SERVER_IP"] 18public_ip = os.environ["PUBLIC_IP"] 19 20s300_pass = getpass("Enter password of s300: ") 21term_serv_pass = getpass("Enter the terminal server password: ") 22srx2_pass = getpass("Enter SRX2 password: ") 23 24# For internal reasons I have to bounce through this small switch to access 25# my terminal server. 26device = { 27 "device_type": "cisco_s300", 28 "host": public_ip, 29 "username": "admin", 30 "password": s300_pass, 31 "session_log": "output.txt", 32} 33 34# Initial connection to the S300 switch 35net_connect = ConnectHandler(**device) 36print(net_connect.find_prompt()) 37 38# Update the password as the terminal server uses different credentials 39net_connect.password = term_serv_pass 40net_connect.secret = term_serv_pass 41# Telnet to the terminal server 42command = f"telnet {terminal_server_ip}\n" 43net_connect.write_channel(command) 44# Use the telnet_login() method to handle the login process 45net_connect.telnet_login() 46print(net_connect.find_prompt()) 47 48# Made it to the terminal server (this terminal server is "cisco_ios") 49# Use redispatch to re-initialize the right class. 50redispatch(net_connect, device_type="cisco_ios") 51net_connect.enable() 52print(net_connect.find_prompt()) 53 54# Now connect to the end-device via the terminal server (Juniper SRX2) 55net_connect.write_channel("srx2\n") 56# Update the credentials for SRX2 as these are different. 57net_connect.username = "pyclass" 58net_connect.password = srx2_pass 59# Use the telnet_login() method to connect to the SRX 60net_connect.telnet_login() 61redispatch(net_connect, device_type="juniper_junos") 62print(net_connect.find_prompt()) 63 64# Now we could do something on the SRX 65output = net_connect.send_command("show version") 66print(output) 67 68net_connect.disconnect() 69