ktbyers / netmiko

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

Feature Request: ability to register new drivers in ssh_dispatcher #3273

Open alextremblay opened 1 year ago

alextremblay commented 1 year ago

It would be nice for netmiko users to be able to locally develop drivers for new hardware or modify / extend existing drivers and dog-food them for a while before submitting PRs to merge them into netmiko itself.

It would be really nice if, for example, a user could implement a new driver like so:

from netmiko.ssh_dispatcher import register_as
from netmiko.cisco_base_connection import CiscoSSHConnection

@register_as('aruba_aoscx')
class ArubaOSCXSSH(CiscoSSHConnection):
    # custom code and method overrides
    pass

Or make local modifications to netmiko's behaviour like so:

from netmiko.ssh_dispatcher import register_as
from netmiko.arista import AristaSSH

@register_as('arista_eos')
class MyAristaSSH(AristaSSH):
    # custom code and method overrides
    pass
ktbyers commented 1 year ago

@alextremblay Couldn't you just load the existing dictionary in ssh_dispatcher and add a new key and the associated class?

from netmiko.ssh_dispatcher import CLASS_MAPPER

CLASS_MAPPER["my_driver"] = MyClass
alextremblay commented 1 year ago

unfortuantely, no.

the CLASS_MAPPER global variable can be imported and manipulated, but the platforms, platforms_base, and platforms_str global variables from that same module cannot, and they also need to be modified in order for the ssh_dispatcher function to work properly

I'm working on a PR for this as we speak, and will submit it shortly

ktbyers commented 1 year ago

Good point on platforms--you don't need platforms_base nor platforms_str though.

So this works:

from netmiko.cisco.cisco_nxos_ssh import CiscoNxosSSH
from netmiko.ssh_dispatcher import CLASS_MAPPER, platforms
from netmiko import ConnectHandler

class MyClass(CiscoNxosSSH):
    pass

CLASS_MAPPER["my_driver"] = MyClass
platforms.append("my_driver")

device = {
    "device_type": "my_driver",
    "host": "nxos1.lasthop.io",
    "username": "admin",
    "password": "password",
}

with ConnectHandler(**device) as nc:
    d = nc.send_command("show lldp neighbors detail")
    print(d)

I am open to the PR though not really sure it is needed (since it is just three lines of code to do this with the existing code base).

alextremblay commented 1 year ago

you would also need to update platforms_str, for situations where a typo or misconfiguration results in ssh_dispatcher not being able to find the mistyped driver name, or the end user will be very confused why their actual driver name doesn't show up in the list of drivers in the exception raised by the ssh_dispatcher function

ktbyers commented 1 year ago

Okay, we will just disagree on whether platforms_str is needed here.

I will look at the PR.