03-10-2020 05:22 AM - edited 03-10-2020 05:25 AM
I have a scenario where I need to connect to many remote Cisco devices to run commands via Python script. These remote Cisco devices are not directly accessible from where I have Python scripts running so I am going to have to connect via an intermediate Router first (use the Cisco router it as a Jump host to connect to the remote Cisco devices) I found a python module for SSH (jumpssh: https://pypi.org/project/jumpssh/) but I need one that supports SSH as well as Telnet. Or is there a similar module for Python that I can use for Telnet?
Topology:
Python Server------------------> Cisco Router -----------------------> Remote Cisco Devices
I have had quite a detailed search for this and can't find an obvious answer to this.
I'm even having problems with the jumpssh module as it's throwing an error (ChannelException(3, 'Unknown channel type') Which makes me think it's not designed to be using a Router as a jump host, it is probably expecting a proper *nix machine.
Any help appreciated.
03-10-2020 05:43 PM - edited 03-10-2020 05:44 PM
Hi velo84,
NetMiko is a python Library that can use either SSH, Telnet or Serial Cable to connect a device. Have a look at the documentation here
I hope this helps!
03-11-2020 02:01 AM
Thanks for that. I have actually written scripts with Netmiko before and it's worked well. It's also what I have been currently using to try and get this working. This is slightly different to the normal Netmiko implementation though, I am connecting to remote devices running telnet through an existing Netmiko SSH session via a intermediate router.
03-11-2020 02:20 AM
Would you have much luck by nesting the telnet session within the SSH connection that NetMiko is giving you?
For example:
net_connect = Netmiko(**cisco1)
command = "telnet 192.168.1.1"
telnetusername = "cisco"
telnetpassword = "cisco"
print()
print(net_connect.find_prompt())
output = net_connect.send_command(command)
output1 = net_connect.send_command(telnetusername)
output2 = net_connect.send_command(telnetpassword)
03-11-2020 02:43 AM
I kind of follow what you mean, do have have more of an example?
This is what I am trying now, It's getting to the point of trying to log on to the remote telnet device but it's not logging on.
#192.168.0.120 = Cisco Router running SSH #100.60.1.1 = Far end router running telnet from __future__ import unicode_literals, print_function import time from netmiko import ConnectHandler, redispatch import logging logging.basicConfig(filename='test.log', level=logging.DEBUG) logger = logging.getLogger("netmiko") cisco = { 'device_type': 'cisco_ios', 'host': '192.168.0.120', 'username': 'admin', 'password': 'admin', } net_connect = ConnectHandler(**cisco) output = net_connect.write_channel("telnet 100.60.1.1" + '\r\n') time.sleep(.5) # Manually handle the Username and Password max_loops = 10 i = 1 while i <= max_loops: output = net_connect.read_channel() if 'Username:' in output: net_connect.write_channel(net_connect.username + '\r\n') time.sleep(1) output = net_connect.read_channel() # Search for password pattern / send password if 'Password' in output: net_connect.write_channel(net_connect.password + '\r\n') time.sleep(.5) output = net_connect.read_channel() # Did we successfully login if '>' in output or '#' in output: break
03-25-2020 02:19 AM
Please have a look at pyATS
https://github.com/CiscoTestAutomation/CL-DEVWKS-2808/blob/master/workshop.md
Automation revolves around being able to programmatically establish connection to your testbed devices. There are tools out there today that help you with this, for example:
These libraries are good at establishing low-level connectivity to your devices, and allow basic device interactions. However, what they do not provide is high-level services: stateful handling of various router/switch prompt states, and advanced mechanisms such as dialogs prompts, etc.
Here are some neat functionalities w.r.t. Unicon:
03-25-2020 05:00 AM
Thanks, I'm using netmiko already. It complicates things because I am using a Cisco router as a jump host for telnet so you have to write in every behaviour.. I'm making some progress. Thanks
Find answers to your questions by entering keywords or phrases in the Search bar above. New here? Use these resources to familiarize yourself with the community: