Netmiko SSH from Windows PC via Jump server

Posted: April 14, 2017 in Automation, python

Netmiko is Multi-vendor library to simplify Paramiko SSH connections to network devices with the help of netmiko library we can do network automation using python
https://github.com/ktbyers/netmiko

In some situations we can not directly connect to network devices like router and switches. We can only access them via jump server
If we have Linux machine from where are initiating ssh connection and jump server is also Linux

Linux(script server)—–Linux(jump server)—–Router

Then we can use Linux ProxyCommand to access devices via jump server.
Below link provides detail for Netmiko SSH Proxy Support
https://pynet.twb-tech.com/blog/automation/netmiko-proxy.html

But we can not use proxycommand if script server is windows

windows(script server)—–Linux(jump server)—–Router

Below code can be use in this situation
In this code i tried to get device up time via jump server using redispatch(Netmiko 1.3.0 Release). Need to adjust global_delay_factor by doing trail and error method

from netmiko import ConnectHandler
import time
from netmiko import redispatch

jumpserver = {‘device_type’: ‘terminal_server’,’ip’: ‘x.x.x.x’,’username’: ‘name’,’password’: ‘pass’,’global_delay_factor’:5}

net_connect = ConnectHandler(**jumpserver)
print net_connect.find_prompt()

net_connect.write_channel(‘command to access router’)
time.sleep(1)
net_connect.read_channel()

redispatch(net_connect, device_type=’cisco_ios’)
net_connect.send_command(‘show version | i uptime’)

Advertisements
Comments
  1. toaster09 says:

    I’m very new to python or any sort of programming

    can i request you to provide a sample code entirely?, Wanted to understand where the inputs would be taken for the jump server & the network device i.e.

    I guess the below line means we should effectively write to the commands similar to how we ssh from linux prompt

    net_connect.write_channel(‘command to access router’) -> net_connect.write_channel(‘ssh )

    redispatch(net_connect, device_type=’cisco_ios’) -> effectively resets the device type under the existing connect handler? but basically is looking for a router prompt#?

    What does a global factor delay do?

    Like

    • you are right about write_channel and redispatch
      suppose you have one router which you want to access using netmiko and there is latency over the wan so by considering worst case situation you need to add delay and you have to find out suitable delay factor in your enviroment
      https://github.com/ktbyers/netmiko/issues/98

      jumpserver = {‘device_type’: ‘terminal_server’,’ip’: ‘x.x.x.x’,’username’: ‘name’,’password’: ‘pass’,’global_delay_factor’:5} here you have to give jump server ip , username and password
      then you can create the list of devices you want access and with help of for loop
      you can run below code inside for loop
      net_connect.write_channel(‘command to access router’)
      time.sleep(1)
      net_connect.read_channel()

      redispatch(net_connect, device_type=’cisco_ios’)
      net_connect.send_command(‘show version | i uptime’)

      Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s