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.
This commit is contained in:
YueGuobin 2026-03-12 23:38:24 +08:00
parent effea3fb26
commit 44f802ae4f
2 changed files with 44 additions and 0 deletions

View File

@ -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

View File

@ -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