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

How can I connect to router through another router using netmiko ?

I want to CONNECT cisco router R2 through another router R1( connected to it via ssh from netmiko server with connecthandler) using Netmiko, , and run commands on R2

 

 

79d959d6-d68c-11e5-8737-ad3d6839df9d.png

6 Replies 6

balaji.bandi
Hall of Fame
Hall of Fame

As Long as IP reachable you can connect to any device in the network.

 

Server able to reach R1 and R2 ? so you can connect.

 

BB

***** Rate All Helpful Responses *****

How to Ask The Cisco Community for Help

Hello Balaji, thank you for your response.
the server cant reach R2, I have to login first to R1 and then ssh to R2, which cause a problem for me, I connected to R1 via Netmiko and then I used send_command_timing to establish SSH cnx to R2. but the problem is that I cant run commands in R2. 

        net_connect = ConnectHandler(**device_info)
        output = net_connect.send_cmd_timing('telnet'+' '+IP)

Thank you for the clarification i was in impress IP can be reachable, please follow other suggested method and see if you can make it work.

 

BB

***** Rate All Helpful Responses *****

How to Ask The Cisco Community for Help

Netmiko supports SSH proxies. By this I mean you can 'bounce' through an intermediate server while connecting to a remote network device. https://pynet.twb-tech.com/blog/automation/netmiko-proxy.html

Please mark this as helpful or solution accepted to help others
Connect with me https://bigevilbeard.github.io

the jump host in my case is a router.

 

After generating the ssh key from the netmiko server , is it possible to copy the ssh key to the jump router ?

Please check below solution,

 

VM:
sudo route add default gw <router 1 ip> <interface>

Router 1:
line vty 0 15
transport input ssh
username cisco password cisco123
exit
conf t
enable secret cisco123
line vty 0 15
login local
exit
ip domain-name cisco
crypto key generate rsa
800
exit
ip dhcp pool NMR1
network 192.168.2.0 255.255.255.0
default-router 192.168.2.1
exit
ip dhcp excluded-address 192.168.2.1 192.168.2.5
int fa1/0
ip add 192.168.2.1 255.255.255.0
no sh
exit

Router 2:
line vty 0 15
transport input ssh
username cisco password cisco123
exit
conf t
enable secret cisco123
line vty 0 15
login local
exit
ip domain-name cisco
crypto key generate rsa
800
exit

 

Python Program:

#! /usr/bin/env python3
#SHEBANG
from netmiko import ConnectHandler
from time import sleep
#import modules with methods in this space

def network():
details = {
'device_type':'cisco_ios',
'ip': '<router 1 ip>',
'username': 'cisco',
'password': 'cisco123',
'secret': 'cisco123',
}
vty = ConnectHandler(**details)
vty.enable()
ping = f'show ipv6 neighbors fastEthernet 0/0'
output = vty.send_command(ping)
print(output)
config_commands = ['int fa0/0','ip add 192.168.2.1 255.255.255.0','no sh','ip dhcp pool NM','default-router 192.168.2.1','network 192.168.2.0 255.255.255.0','ip dhcp excluded-address 192.168.2.1 192.168.2.5'];
vty.enable()
output1=vty.send_config_set(config_commands, delay_factor=5)
sleep(5)
ping = f'sh ip dhcp binding'
print("\n")
print("DHCP client bindings")
outputip = vty.send_command(ping)
vty.disconnect()
print(outputip)
with open('dhcpclients','w') as dh:
dh.write(outputip);
with open('dhcpclients') as of:
cv=of.readlines();
print(cv[4][0:13])
vty.disconnect()
h=cv[4][0:13]
ipl = h.strip()
details = {
'device_type':'cisco_ios',
'ip': ipl,
'username': 'cisco',
'password': 'cisco123',
'secret': 'cisco123',
}

vty = ConnectHandler(**details)
vty.enable()
pinge = f'ping <router 1 ip>'
outputz = vty.send_command(pinge)
print(outputz)
network()

 

Thanks. Hope it is helpful.

Note: <router 1 ip> and <interface> need to be changed. You need a csv file with name 'dhcpclients.csv' with login details.