cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
5745
Views
5
Helpful
6
Replies

Use Cisco Router as a jump host with Python (for SSH and Telnet)

velo84
Level 1
Level 1

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. 

6 Replies 6

LanDownUnda
Spotlight
Spotlight

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!

*** Rate All Helpful Responses ***

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.

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)

*** Rate All Helpful Responses ***

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


 

 

 

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:

  • Paramiko: Python implementation of SSH client
  • Pexpect: Python module for spawning child applications (e.g., telnet/ssh) and interacting with them
  • Netmiko: multi-vendor library that simplifies Paramiko SSH connections to network devices

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:

  • Automatically learn the hostname (within reason)
  • Log connection interactions to a whole separate file
  • Connection through proxies (jump hosts)
  • RobotFramework support/keys

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

Getting Started

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: