From 44f802ae4f6bc85889f28e6f1f43642aab9c135f Mon Sep 17 00:00:00 2001 From: YueGuobin Date: Thu, 12 Mar 2026 23:38:24 +0800 Subject: [PATCH] fix: update netmiko platforms after custom device registration Netmiko's `ssh_dispatcher` calculates platform lists at module import time. When custom device types (like Huawei CE) are registered dynamically, these cached lists become stale and do not include the new platforms. This change imports `netmiko.ssh_dispatcher` and recalculates the `platforms`, `platforms_base`, and `telnet_platforms` attributes to ensure Netmiko recognizes the custom device types. --- .../tools_v2/config_tools_nornir.py | 22 +++++++++++++++++++ .../tools_v2/display_tools_nornir.py | 22 +++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/gns3server/agent/gns3_copilot/tools_v2/config_tools_nornir.py b/gns3server/agent/gns3_copilot/tools_v2/config_tools_nornir.py index a925bd76a..0289aab29 100644 --- a/gns3server/agent/gns3_copilot/tools_v2/config_tools_nornir.py +++ b/gns3server/agent/gns3_copilot/tools_v2/config_tools_nornir.py @@ -61,6 +61,28 @@ try: # Re-register to ensure device types are available huawei_ce.register_custom_device_type() + + # CRITICAL: Update netmiko.ssh_dispatcher platforms lists + # The platforms variable is calculated at module import time in ssh_dispatcher + # We need to update it after registering custom device types. + # NOTE: netmiko.ssh_dispatcher is aliased to ConnectHandler function, + # so we use importlib to get the actual module + import importlib + + sd = importlib.import_module("netmiko.ssh_dispatcher") + + # Recalculate platforms lists to include custom device types + sd.platforms = list(sd.CLASS_MAPPER.keys()) + sd.platforms.sort() + + sd.platforms_base = list(sd.CLASS_MAPPER_BASE.keys()) + sd.platforms_base.sort() + + sd.telnet_platforms = [x for x in sd.platforms if "telnet" in x] + + # Update platform strings used in error messages + sd.platforms_str = "\n" + "\n".join(sd.platforms_base) + sd.telnet_platforms_str = "\n" + "\n".join(sd.telnet_platforms) except Exception: # Fail silently - the import-time registration should have worked pass diff --git a/gns3server/agent/gns3_copilot/tools_v2/display_tools_nornir.py b/gns3server/agent/gns3_copilot/tools_v2/display_tools_nornir.py index 53edb46c1..9fd0eea78 100644 --- a/gns3server/agent/gns3_copilot/tools_v2/display_tools_nornir.py +++ b/gns3server/agent/gns3_copilot/tools_v2/display_tools_nornir.py @@ -61,6 +61,28 @@ try: # Re-register to ensure device types are available huawei_ce.register_custom_device_type() + + # CRITICAL: Update netmiko.ssh_dispatcher platforms lists + # The platforms variable is calculated at module import time in ssh_dispatcher + # We need to update it after registering custom device types. + # NOTE: netmiko.ssh_dispatcher is aliased to ConnectHandler function, + # so we use importlib to get the actual module + import importlib + + sd = importlib.import_module("netmiko.ssh_dispatcher") + + # Recalculate platforms lists to include custom device types + sd.platforms = list(sd.CLASS_MAPPER.keys()) + sd.platforms.sort() + + sd.platforms_base = list(sd.CLASS_MAPPER_BASE.keys()) + sd.platforms_base.sort() + + sd.telnet_platforms = [x for x in sd.platforms if "telnet" in x] + + # Update platform strings used in error messages + sd.platforms_str = "\n" + "\n".join(sd.platforms_base) + sd.telnet_platforms_str = "\n" + "\n".join(sd.telnet_platforms) except Exception: # Fail silently - the import-time registration should have worked pass