Skip to content

Network Automation

My journey with Network & Cloud Automation

Menu
  • Beginner
  • DevOps-NetDevOps
  • Network Automation
    • Docker
    • Python Libraries
      • NAPALM
      • Netmiko
      • Jinja2
      • Scrapli
      • Yang
  • Cloud Automation
    • Terraform
  • Python 🐍 Tips and Tricks
Menu
Netmiko TEXTFSM

Netmiko SSH Proxy via JumpHost

Posted on November 22, 2021January 30, 2022 by Gurpreet Kochar

Off late I have been required to access the network devices through a JumpHost. One of the easiest methods to do this was using python’s netmiko module. My setup looks like this

Windows Machine ——- SSH tunnel ——- Linux JumpHost ——-SSH to ——- Network Devices

Here are the basic steps that I followed to get this working.

  1. Create public private key pair on windows machine. There are 2 ways to go about this. I did it using putty but either of the two methods are perfectly fine.
    • OpenSSH Client on windows
    • Using Putty

PUTTY METHOD

  1. Launch PuTTYgen and click on generate. Move your mouse in the white space to create randomness
  2. You could know save the public and private key seperately.
  3. However, while working with netmiko, you need to export the key as OpenSSH Key.

OpenSSH Method

  1. Install OpenSSH Client on windows
    • https://docs.microsoft.com/en-us/windows-server/administration/openssh/openssh_install_firstuse
  2. Launch powershell/cmd prompt and type “ssh-keygen”
  3. You could choose a custom location but if not, the default is already chosen for you.
  4. You can now chose a passphrase if you want and 2 new keys with id_rsa and id_rsa.pub shall be created under C:\Users\<username>\.ssh directory. As the name suggests, .pub is the public key and the other one is the private key.

The next step is to copy the public key you just generated to the authorized_hosts directory of the jump server. You can find the authorized_hosts file at this location “~/.ssh/authorized_keys“

Now is the time to start playing with netmiko to access your network devices

jumpserver = {
    "device_type": "terminal_server",
    "ip": "<jumphost_ip>",
    "username": "<username>",
    "global_delay_factor": 5,
    "use_keys": True,
    "key_file": "<path to openssh key file>",
}
logging.info("Trying to connect Term Server [%s] with user [%s]" % (jumpserver['ip'], jumpserver['username']))
net_connect = ConnectHandler(**jumpserver)
net_connect.write_channel(f"ssh router_username@router_ip\r\n")
time.sleep(5) # enough time for allow login to jumpserver.. It's super slow in my case.
output = net_connect.read_channel()
if 'password' in output.lower(): # 'password' is the prompt i get. check what it is in your case.
    net_connect.write_channel(f"router_pass\r\n")
    net_connect.secret = router_secret
    time.sleep(1) # just enough for login banner to appear
    net_connect.enable(cmd='enable') # optional if your device needs enable

redispatch(net_connect, device_type='cisco_ios') # or a suitable device_type to suit your needs.
# find device prompt
# from here on its usual netmiko IOS interaction that you would use.
dev_prompt = net_connect.find_prompt()

You could find a lot more information about netmiko and ideas on what you can do past the above step here

https://networkautomationlane.in/category/network-automation/python-libraries-for-network-automation/network-automation-using-python-netmiko/

Know someone who may benefit? Share this:

  • Tweet
  • Click to share on Telegram (Opens in new window) Telegram
  • Click to share on WhatsApp (Opens in new window) WhatsApp
  • Click to email a link to a friend (Opens in new window) Email
  • More
  • Click to print (Opens in new window) Print
  • Click to share on Reddit (Opens in new window) Reddit
  • Share on Tumblr
  • Pocket

Like this:

Like Loading...

Related

Leave a ReplyCancel reply

All Blog Posts
My Resume

Upcoming Posts

Sorry - nothing planned yet!

Recent Posts

  • How to backup configuration to TFTP Server using Ansible – Part II
  • How to backup network devices using Ansible – Part I
  • Netmiko SSH Proxy/JumpServer
  • A short note on SASE
  • Understanding Ansible

Recent Comments

  1. Jack on Multithreading with Python for Network Engineers
  2. LifeCanvas on [Theory] Multithreading vs Multiprocessing vs AsyncIO
  3. Jasper Horng on Netmiko SSH Proxy/JumpServer
  4. asdfasdf on Python API Using FASTAPI – UPDATE – PUT – PATCH – Part V
  5. Gurpreet Kochar on Python Scrapli AsyncIO Usage

Archives

  • September 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
Topic Request / Suggestion
Loading
© 2025 Network Automation | Powered by Minimalist Blog WordPress Theme
 

Loading Comments...
 

    %d