Netmiko: SSH to ASA device from linux jump server

Created on 27 Aug 2019  路  6Comments  路  Source: ktbyers/netmiko

I have tried the SSH to ASA device from linux junp server.

But I am able to see the conenct handler error for the unmatch prompt error.

DEBUG:paramiko.transport:starting thread (client mode): 0x311b518L
DEBUG:paramiko.transport:Local version/idstring: SSH-2.0-paramiko_2.4.1
DEBUG:paramiko.transport:Remote version/idstring: SSH-2.0-OpenSSH_7.4
INFO:paramiko.transport:Connected (version 2.0, client OpenSSH_7.4)
DEBUG:paramiko.transport:kex algos:[u'curve25519-sha256', u'[email protected]', u'ecdh-sha2-nistp256', u'ecdh-sha2-nistp384', u'ecdh-sha2-nistp521', u'diffie-hellman-group-exchange-sha256', u'diffie-hellman-group16-sha512', u'diffie-hellman-group18-sha512', u'diffie-hellman-group-exchange-sha1', u'diffie-hellman-group14-sha256', u'diffie-hellman-group14-sha1', u'diffie-hellman-group1-sha1'] server key:[u'ssh-rsa', u'rsa-sha2-512', u'rsa-sha2-256', u'ecdsa-sha2-nistp256', u'ssh-ed25519'] client encrypt:[u'[email protected]', u'aes128-ctr', u'aes192-ctr', u'aes256-ctr', u'[email protected]', u'[email protected]', u'aes128-cbc', u'aes192-cbc', u'aes256-cbc', u'blowfish-cbc', u'cast128-cbc', u'3des-cbc'] server encrypt:[u'[email protected]', u'aes128-ctr', u'aes192-ctr', u'aes256-ctr', u'[email protected]', u'[email protected]', u'aes128-cbc', u'aes192-cbc', u'aes256-cbc', u'blowfish-cbc', u'cast128-cbc', u'3des-cbc'] client mac:[u'[email protected]', u'[email protected]', u'[email protected]', u'[email protected]', u'[email protected]', u'[email protected]', u'[email protected]', u'hmac-sha2-256', u'hmac-sha2-512', u'hmac-sha1'] server mac:[u'[email protected]', u'[email protected]', u'[email protected]', u'[email protected]', u'[email protected]', u'[email protected]', u'[email protected]', u'hmac-sha2-256', u'hmac-sha2-512', u'hmac-sha1'] client compress:[u'none', u'[email protected]'] server compress:[u'none', u'[email protected]'] client lang:[u''] server lang:[u''] kex follows?False
DEBUG:paramiko.transport:Kex agreed: ecdh-sha2-nistp256
DEBUG:paramiko.transport:HostKey agreed: ssh-ed25519
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
DEBUG:paramiko.transport:Switch to new keys ...
DEBUG:paramiko.transport:Adding ssh-ed25519 host key for 10.170.4.40: 0e31ecb59b7bb3888b140e2f0771030a
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:Received global request "[email protected]"
DEBUG:paramiko.transport:Rejecting "[email protected]" global request from server.
DEBUG:paramiko.transport:[chan 0] Max packet out: 32768 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:read_channel: Last login: Tue Aug 27 07:53:02 2019 from 159.79.224.12

from netmiko import redispatch
import re
import logging
logging.basicConfig(filename='test.log', level=logging.DEBUG)
logger = logging.getLogger("netmiko")

today = datetime.now().strftime("_%Y-%m-%d_%H_%M_%S")
time.sleep(1)
us = raw_input(" Please enter username : ")
pa = getpass()
pa1 ="Aug15Aug152487"
file1 = open("inputdeviceip.txt","r+")
file2= open("command.txt","r+")
fl =file1.readlines()
f2 =file2.readlines()
ip_addr = ("10.170.4.40")

for host in fl:
#platform = 'linux'
host=host.strip()
if not host:
break
else:
ssh_device = {
'device_type':'linux',
'ip': ip_addr,
'username': us,
'password': pa1,
'port': 22,
'verbose':True,
'global_delay_factor':10,
'secret' : pa1
}
device = ConnectHandler(**ssh_device)
time.sleep(2)
print device.find_prompt()
#print ("SSH prompt: {}".format(device.find_prompt()))
ssh ="ssh"
sshinfo =ssh+" "+host+" "+'n'
device.write_channel(sshinfo)
time.sleep(1)
output = device.read_channel()
time.sleep(1)

if 'ssword' in output:
device.write_channel(device.password + 'n')
output += device.read_channel()

Verify you logged in successfully

print(output)

mm1="-------connecting"
name =mm1+" "+host
print(name)
time.sleep(2)
redispatch(device, device_type='cisco_asa')
#print ("SSH prompt: {}".format(device.find_prompt()))
#device = ConnectHandler(device_type=platform,ip=host, username=us,password=pa,secret=pa)
file3 = open(host+today+".txt", "w")
output0 = device.send_command('show hostname')
file3.write(output0)
file3.write("n")
print("Executing the command")
for host1 in f2:
time.sleep(1)
host3=host1.strip()
output13 = device.send_command_timing(host3,strip_command=False, strip_prompt=False)
mm="Results"
commnd =mm+" "+host3+"n"
file3.write("n")
#file3.write(commnd)
file3.write("n")
file3.write(output13)
time.sleep(2)
device.disconnect()
print("Finished the command execution")
file1.close()
file2.close()
file3.close()

command prompt error:
C:\Users\murgun\Desktop\Murali\command exe--2\command execution>python Commandexe.py
Please enter username : murgun
Password:
SSH connection established to 10.170.4.40:22
Interactive SSH session established
Traceback (most recent call last):
File "Commandexe.py", line 41, in
device = ConnectHandler(*ssh_device)
File "C:\Python27\lib\site-packagesnetmiko\ssh_dispatcher.py", line 178, in ConnectHandler
return ConnectionClass(
args, **kwargs)
File "C:\Python27\lib\site-packagesnetmiko\base_connection.py", line 208, in __init__
self.session_preparation()
File "C:\Python27\lib\site-packagesnetmiko\linux\linux_ssh.py", line 17, in session_preparation
return super(LinuxSSH, self).session_preparation()
File "C:\Python27\lib\site-packagesnetmiko\base_connection.py", line 580, in session_preparation
self.set_base_prompt()
File "C:\Python27\lib\site-packagesnetmiko\linux\linux_ssh.py", line 37, in set_base_prompt
delay_factor=delay_factor)
File "C:\Python27\lib\site-packagesnetmiko\base_connection.py", line 856, in set_base_prompt
raise ValueError("Router prompt not found: {0}".format(repr(prompt)))
ValueError: Router prompt not found: u'murgun@se06mgmnnm01 ~ $\x1b[00m'

Most helpful comment

@ktbyers I am using the device_type ="autodetect" to connect the linux jump server connection.

After the successfull connection to the Linux jumphost, I did redispath like redispatch(device, device_type='autodetect').

ssh_device = {
'device_type':'autodetect',
'ip': ip_addr,
'username': us,
'password': pa1,
'port': 22,
#'verbose':True,
'global_delay_factor':10,
'secret' : pa1
}
device = ConnectHandler(**ssh_device) -------> It will connecting the linux jump server
time.sleep(2)

ssh ="ssh"
redispatch(device, device_type='autodetect') redispath the device_type
sshinfo =ssh+" "+host+" "+'n'
device.write_channel(sshinfo) ------> connecting the ASA
time.sleep(1)
output = device.read_channel()

if 'ssword' in output:
device.write_channel(pa + 'n')
output += device.read_channel()

All 6 comments

Please help on this issue

Usually for Jump Servers you would use an SSH proxy setup:

https://pynet.twb-tech.com/blog/automation/netmiko-proxy.html

Kirk

Hi, I was already dealing a lot with that and I will recommend some things, for the first connection to jumpserver use "autodetect" in device_type.
Anyway your issue is something related with the last line:

raise ValueError("Router prompt not found: {0}".format(repr(prompt)))
ValueError: Router prompt not found: u'murgun@se06mgmnnm01 ~ $\x1b[00m'

Also I will suggest to enable logging and paste here the full log to see exactly what is happening there.

import logging
logging.basicConfig(filename='test.log', level=logging.DEBUG)
logger = logging.getLogger("netmiko")

Regards
Muadiv.

Thanks for the response

@ktbyers I am not able to do SSH proxy config in this environment.

@Muadiv Thanks for the help. I have resolved my issue now. I am using device_type ="autodetect" for the first connection to jumpserver and redispath to the cisco ASA.

@Murali24872019 What was the resolution of the issue?

@ktbyers I am using the device_type ="autodetect" to connect the linux jump server connection.

After the successfull connection to the Linux jumphost, I did redispath like redispatch(device, device_type='autodetect').

ssh_device = {
'device_type':'autodetect',
'ip': ip_addr,
'username': us,
'password': pa1,
'port': 22,
#'verbose':True,
'global_delay_factor':10,
'secret' : pa1
}
device = ConnectHandler(**ssh_device) -------> It will connecting the linux jump server
time.sleep(2)

ssh ="ssh"
redispatch(device, device_type='autodetect') redispath the device_type
sshinfo =ssh+" "+host+" "+'n'
device.write_channel(sshinfo) ------> connecting the ASA
time.sleep(1)
output = device.read_channel()

if 'ssword' in output:
device.write_channel(pa + 'n')
output += device.read_channel()

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dfields186 picture dfields186  路  6Comments

yixiuGit picture yixiuGit  路  4Comments

nfordhk picture nfordhk  路  8Comments

Rooster-OC picture Rooster-OC  路  5Comments

ktbyers picture ktbyers  路  4Comments