cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
2586
Views
5
Helpful
1
Replies

Could not establish an ssh connection on router using NETMIKO

I am trying to develop a python script that connect to cisco router through another router using netmiko( ConnectHandler()). ssh to R1 then SSH to R2 from R1.

 


I established the first connexion to R1 , and I used Netmiko to ssh to the router R2 But I could not establish the cnx.
**This is the dictionary creation**

def sh_route(self,alias):
global verif
self.device = self._cache.switch(alias)
device_info = {
'device_type': 'cisco_ios',
'ip': self.device.hostname,
'username': self.device.username,
'password': self.device.password,}


**Connexion to R1 using ConnectHandler and the dictionary created**

#connect to device1 from linux
net_connect = ConnectHandler(**device_info)
ldp= self.get_ldp_neighbors(alias)
for x in ldp.keys():
addr_list = ldp[x]['addresses'] #List of ldp ip adresses

src_ip_addr= ldp[x]['SrcIPaddr'] #source ip addr

**Writing ssh 'src_ip_addr using write_channel() function'** (src_ip_addr is the address that I want to ssh)

net_connect.write_channel('ssh'+src_ip_addr+'\n')
time.sleep(1)
output = net_connect.read_channel()

**use a loop for the above username and password handling**
The password seems like it was not written , so the cnx failed.

i = 1
while i <= 20:
try:
if 'Username' in output:
net_connect.write_channel(net_connect.username + '\r\n')
time.sleep(1)
output = net_connect.read_channel()
if 'password' in output:
net_connect.write_channel(net_connect.password + '\n')
time.sleep(1)
output = net_connect.read_channel()
i += 1
except EOFError:
pass
print(output=

**Verify that the cnx was establisged**

print ("SSH prompt: {}".format(net_connect.find_prompt()))

**then use the dispatch feature and send command to the destination router R2**

redispatch(net_connect, device_type='cisco_ios')
net_connect.enable()
new_output = net_connect.send_command('show version', use_textfsm=True)

**Then I tried to run the command sh ip route connected and sh route local**

version= new_output[0]['rommon']
if version == 'IOS-XE':
new_output = net_connect.send_command('show ip route connected')
return new_output
if version == 'IOS-XR':
new_output = net_connect.send_command('show route local')
return new_output


THE problem is in the username and password handling section.

THis is the output on terminal

ssh 192.168.247.2
Password:
ssh 192.168.247.2
Password:
ssh 192.168.247.2
Password:
ssh 192.168.247.2
Password:
ssh 192.168.247.2
Password:
ssh 192.168.247.2
Password:
ssh 192.168.247.2
Password:
ssh 192.168.247.2
Password:
ssh 192.168.247.2
Password:
ssh 192.168.247.2
Password:
ssh 192.168.247.2
Password:
ssh 192.168.247.2
Password:
ssh 192.168.247.2
Password:
ssh 192.168.247.2
Password:
ssh 192.168.247.2
Password:
ssh 192.168.247.2
Password:
ssh 192.168.247.2
Password:
ssh 192.168.247.2
Password:
ssh 192.168.247.2
Password:
ssh 192.168.247.2
Password:
SSH prompt: Password:


this is the full log:

THIS is the log

> DEBUG:paramiko.transport:starting thread (client mode): 0x31b464e0
> DEBUG:paramiko.transport:Local version/idstring: SSH-2.0-paramiko_2.7.2
> DEBUG:paramiko.transport:Remote version/idstring: SSH-2.0-Cisco-1.25
> INFO:paramiko.transport:Connected (version 2.0, client Cisco-1.25)
> DEBUG:paramiko.transport:kex algos:['ecdh-sha2-nistp256', 'ecdh-sha2-nistp384', 'ecdh-sha2-nistp521', 'diffie-hellman-group14-sha1'] server key:['rsa-sha2-512', 'rsa-sha2-256', 'ssh-rsa'] client encrypt:['aes128-gcm', 'aes256-gcm', 'aes128-ctr', 'aes192-ctr', 'aes256-ctr'] server encrypt:['aes128-gcm', 'aes256-gcm', 'aes128-ctr', 'aes192-ctr', 'aes256-ctr'] client mac:['hmac-sha2-256-etm@openssh.com', 'hmac-sha2-512-etm@openssh.com', 'hmac-sha2-256', 'hmac-sha2-512'] server mac:['hmac-sha2-256-etm@openssh.com', 'hmac-sha2-512-etm@openssh.com', 'hmac-sha2-256', 'hmac-sha2-512'] client compress:['none'] server compress:['none'] client lang:[''] server lang:[''] kex follows?False
> DEBUG:paramiko.transport:Kex agreed: ecdh-sha2-nistp256
> DEBUG:paramiko.transport:HostKey agreed: ssh-rsa
> DEBUG:paramiko.transport:Cipher agreed: aes128-ctr
> DEBUG:paramiko.transport:MAC agreed: hmac-sha2-256
> DEBUG:paramiko.transport:Compression agreed: none
> DEBUG:paramiko.transport:kex engine KexNistp256 specified hash_algo <built-in function openssl_sha256>
> DEBUG:paramiko.transport:Switch to new keys ...
> DEBUG:paramiko.transport:Adding ssh-rsa host key for Bnet-A101: b'a51ea5a576822573a37ce25441bd0241'
> DEBUG:paramiko.transport:userauth is OK
> INFO:paramiko.transport:Authentication (password) successful!
> DEBUG:paramiko.transport:[chan 0] Max packet in: 32768 bytes
> DEBUG:paramiko.transport:[chan 0] Max packet out: 4096 bytes
> DEBUG:paramiko.transport:Secsh channel 0 opened.
> DEBUG:paramiko.transport:[chan 0] Sesch channel 0 request ok
> DEBUG:paramiko.transport:[chan 0] Sesch channel 0 request ok
> DEBUG:netmiko:write_channel: b'terminal width 511\n'
> DEBUG:netmiko:Pattern is: terminal width 511
> DEBUG:netmiko:_read_channel_expect read_data: CCCCCCCC

CC
C
_____________________________________________________________________________
| |
| BNET-A101 at BRATISLAVA vLAB |
| |
| THIS IS A PRIVATE SYSTEM MANAGED BY ORANGE BUSINESS SERVICES |
| |
| ACCESS TO THIS DEVICE REQUIRES AUTHENTICATION AGAINST THE |
| |
| >> LAB INFRASTRUCTURE TACACS SERVICE << |
| |
| Your "saturne" login should be used here |
| |
| |
|_____________________________________________________________________________|


> DEBUG:netmiko:_read_channel_expect read_data: CCCCCCCC

CCCCCCCCCC
CC
C
________________________________________________________________
| |
| Activity on this device is recorded and possibly monitored |
|________________________________________________________________|

Bnet-A101#terminal width 5

> DEBUG:netmiko:_read_channel_expect read_data: 11
>
> DEBUG:netmiko:Pattern found: terminal width 511 CCCCCCCC

CC
C
_____________________________________________________________________________
| |
| BNET-A101 at BRATISLAVA vLAB |
| |
| THIS IS A PRIVATE SYSTEM MANAGED BY ORANGE BUSINESS SERVICES |
| |
| ACCESS TO THIS DEVICE REQUIRES AUTHENTICATION AGAINST THE |
| |
| >> LAB INFRASTRUCTURE TACACS SERVICE << |
| |
| Your "saturne" login should be used here |
| |
| |
|_____________________________________________________________________________|
CCCCCCCC
CCCCCCCCCC
CC
C
________________________________________________________________
| |
| Activity on this device is recorded and possibly monitored |
|________________________________________________________________|

Bnet-A101#terminal width 511

> DEBUG:netmiko:In disable_paging
> DEBUG:netmiko:Command: terminal length 0
>
> DEBUG:netmiko:write_channel: b'terminal length 0\n'
> DEBUG:netmiko:Pattern is: terminal\ length\ 0
> DEBUG:netmiko:_read_channel_expect read_data: Bnet-A101#
> DEBUG:netmiko:_read_channel_expect read_data: terminal len
> DEBUG:netmiko:_read_channel_expect read_data: gth 0
>
> DEBUG:netmiko:Pattern found: terminal\ length\ 0 Bnet-A101#terminal length 0
>
> DEBUG:netmiko:Bnet-A101#terminal length 0
>
> DEBUG:netmiko:Exiting disable_paging
> DEBUG:netmiko:read_channel:
> DEBUG:netmiko:write_channel: b'\n'
> DEBUG:netmiko:read_channel:
> DEBUG:netmiko:read_channel:
> DEBUG:netmiko:write_channel: b'\n'
> DEBUG:netmiko:read_channel:
> DEBUG:netmiko:write_channel: b'\n'
> DEBUG:netmiko:read_channel:
> DEBUG:netmiko:write_channel: b'\n'
> DEBUG:netmiko:read_channel:
> DEBUG:netmiko:write_channel: b'\n'
> DEBUG:netmiko:read_channel:
> DEBUG:netmiko:write_channel: b'\n'
> DEBUG:netmiko:read_channel: Bnet-A101#

Bnet-A101#
Bnet-A101#
Bnet-A101#
Bnet-A101#
Bnet-A101#
Bnet-A101#


> DEBUG:netmiko:read_channel:
> DEBUG:netmiko:[find_prompt()]: prompt is Bnet-A101#
> DEBUG:netmiko:write_channel: b'ssh 192.168.247.2\n'
> DEBUG:netmiko:read_channel: ssh 192.168.247.2

Password:

> DEBUG:netmiko:read_channel:
> DEBUG:netmiko:write_channel: b'\n'
> DEBUG:netmiko:read_channel:
>
> DEBUG:netmiko:read_channel:
> DEBUG:netmiko:write_channel: b'\n'
> DEBUG:netmiko:read_channel:
> DEBUG:netmiko:write_channel: b'\n'
> DEBUG:netmiko:read_channel:
> DEBUG:netmiko:write_channel: b'\n'
> DEBUG:netmiko:read_channel:
> DEBUG:netmiko:write_channel: b'\n'
> DEBUG:netmiko:read_channel:
> DEBUG:netmiko:write_channel: b'\n'
> DEBUG:netmiko:read_channel:
> DEBUG:netmiko:write_channel: b'\n'
> DEBUG:netmiko:read_channel:
> DEBUG:netmiko:write_channel: b'\n'
> DEBUG:netmiko:read_channel:
> DEBUG:netmiko:write_channel: b'\n'
> DEBUG:netmiko:read_channel: CCCCAuthentication failed, please try again!

Password:

>
> DEBUG:netmiko:read_channel:
> DEBUG:netmiko:[find_prompt()]: prompt is Password:
> DEBUG:netmiko:write_channel: b'\n'
> DEBUG:netmiko:Pattern is: Bnet\-A101
> DEBUG:netmiko:_read_channel_expect read_data: CCCCAuthentication failed, please try again!

Password:

DEBUG:netmiko:_read_channel_expect read_data: CCCCAuthentication failed, please try again!
Password:

> DEBUG:netmiko:_read_channel_expect read_data:
>
> DEBUG:netmiko:_read_channel_expect read_data:

[Connection to 192.168.247.2 closed by foreign host]
Bnet-A101#
Bnet-A101#

> DEBUG:netmiko:Pattern found: Bnet\-A101 CCCCAuthentication failed, please try again!

Password:
CCCCAuthentication failed, please try again!
Password:

[Connection to 192.168.247.2 closed by foreign host]
Bnet-A101#
Bnet-A101#

> DEBUG:netmiko:read_channel:

Bnet-A101#
Bnet-A101#
Bnet-A101#
Bnet-A101#
Bnet-A101#

> DEBUG:netmiko:Clear buffer detects data in the channel
> DEBUG:netmiko:read_channel:
> DEBUG:netmiko:write_channel: b'\n'
> DEBUG:netmiko:read_channel:

Bnet-A101#

> DEBUG:netmiko:read_channel:
> DEBUG:netmiko:[find_prompt()]: prompt is Bnet-A101#
> DEBUG:netmiko:read_channel:
> DEBUG:netmiko:write_channel: b'show version\n'
> DEBUG:netmiko:Pattern is: show\ version
> DEBUG:netmiko:_read_channel_expect read_data: s
> DEBUG:netmiko:_read_channel_expect read_data: how version
>
> DEBUG:netmiko:Pattern found: show\ version show version
>
> DEBUG:netmiko:read_channel:
> DEBUG:netmiko:read_channel:
> DEBUG:netmiko:read_channel:
> DEBUG:netmiko:read_channel:
> DEBUG:netmiko:read_channel:
> DEBUG:netmiko:read_channel:
> DEBUG:netmiko:read_channel:
> DEBUG:netmiko:read_channel:
> DEBUG:netmiko:read_channel:
> DEBUG:netmiko:read_channel:

**output of sh ip route connected executed under R1 not R2**

1 Reply 1

I would suggest opening an issue here https://github.com/ktbyers/netmiko/issues or check the NTC Netmiko Slack channel https://networktocode.slack.com/?redir=%2Fmessages%2Fnetmiko%2F

 

Hope this helps!

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