ktbyers / netmiko

Multi-vendor library to simplify Paramiko SSH connections to network devices
MIT License
3.62k stars 1.31k forks source link

SSHDetect not working with Jumphost inbetween #3303

Open NetworkSurfer opened 1 year ago

NetworkSurfer commented 1 year ago

I have a list of IP addresses (in code only two IPs are written) and trying to figure out the device_type based on its vendor. have written the below code. we are going through jumphost. this code was just to figure out the device type, plan is to expand it for command execution based on the device vendor. maybe

could not figure out what was wrong and missing in the code and maybe encountered this scenario with Netmiko for the first time, not sure if SSHDetect can work with Jumphost Inbetween. if someone can help and point me in the right direction.

from netmiko import ConnectHandler
from netmiko.ssh_autodetect import SSHDetect
from paramiko.ssh_exception import SSHException

jumpserver = {
    "device_type": "linux",  # Correct device_type for a Linux-based jump server
    "ip": "192.168.0.129",
    "username": "root",
    "password": "root",
    "global_delay_factor": 5,
    "ssh_config_file": "C:/Users/admin/.ssh/config",
}

jump_server_channel = ConnectHandler(**jumpserver)

devices = ['172.20.20.10', '172.20.20.11']
username = "clab"
password = "clab@123"

def identify_device_type(jump_server_channel, ip, username, password):
    device = {
        'device_type': 'autodetect',
        'ip': ip,
        'username': username,
        'password': password,
        'global_delay_factor': 5,  # Adjust as needed
    }

    try:
        # Use SSHDetect to identify the device type
        guesser = jump_server_channel.write_channel(SSHDetect(**device))
        device_type = guesser.autodetect()

        return device_type

    except SSHException as e:
        print(f"Failed to connect to {ip} via the jump server: {str(e)}")
        return None

print("Jump server prompt: {}\n".format(jump_server_channel.find_prompt()))

print("=====now printing the device detail======")

if jump_server_channel:
    for ip in devices:
        device_type = identify_device_type(jump_server_channel, ip, username, password)
        if device_type:
            print("device type of {0} is {1}".format(ip, device_type))
        else:
            print("device type of {} could not be found".format(ip)) `

============output as below ================

Jump server prompt: [root@lab-server ~]#

=====now printing the device detail====== Failed to connect to 172.20.20.10 via the jump server: TCP connection to device failed.

Common causes of this problem are:

  1. Incorrect hostname or IP address.
  2. Wrong TCP port.
  3. Intermediate firewall blocking access.

Device settings: autodetect 172.20.20.10:22

device type of 172.20.20.10 could not be found Failed to connect to 172.20.20.11 via the jump server: TCP connection to device failed.

Common causes of this problem are:

  1. Incorrect hostname or IP address.
  2. Wrong TCP port.
  3. Intermediate firewall blocking access.

Device settings: autodetect 172.20.20.11:22

device type of 172.20.20.11 could not be found

ktbyers commented 1 year ago

@GV03 Yeah, this isn't going to work:

guesser = jump_server_channel.write_channel(SSHDetect(**device))

The behavior of that is that SSHDetect(**device) will execute first. It will try to connect directly from the local machine that the script is executing on`.

So in your above code, you aren't really using the SSH proxy.

I have an article on Netmiko and SSH Proxy here (though you might have some issues with it as you are using Windows):

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