04-30-2021 04:38 AM
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
04-30-2021 04:42 AM
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.
04-30-2021 04:53 AM
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.
04-30-2021 05:19 AM
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.
04-30-2021 05:02 AM
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
04-30-2021 05:25 AM
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 ?
03-03-2022 07:40 PM - edited 03-24-2022 11:14 PM
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.
Discover and save your favorite ideas. Come back to expert answers, step-by-step guides, recent topics, and more.
New here? Get started with these tips. How to use Community New member guide