fix: avoid duplicate 'port' kwarg when netmiko_optional_args already contains it

Extracted _netmiko_kwargs() helper that strips port/timeout/disabled_algorithms
from netmiko_optional_args before spreading, so explicit values always win.
Used in both _try_ssh and _action_fix_snmp SSH fallback loop.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Christian Manivong
2026-06-09 23:47:33 +02:00
co-authored by Claude Sonnet 4.6
parent 0cab237b1d
commit 22b83cf174
+19 -21
View File
@@ -244,13 +244,15 @@ class ProcurveDriver(SwitchDriver):
logger.warning("REST API connect failed for %s — falling back to CLI", self.hostname) logger.warning("REST API connect failed for %s — falling back to CLI", self.hostname)
return False return False
def _try_ssh(self, legacy: bool = False) -> bool: def _netmiko_kwargs(self, legacy: bool = False) -> dict:
"""Probe and connect via SSH. Returns True on success.""" """Return kwargs for ConnectHandler, merging explicit fields with netmiko_optional_args.
label = "SSH-legacy" if legacy else "SSH"
logger.debug("Trying %s for %s", label, self.hostname) Explicit fields (port, timeout, disabled_algorithms) take precedence and are
disabled = _SSH_DISABLED_LEGACY if legacy else _SSH_DISABLED_STANDARD excluded from the spread to avoid 'multiple values for keyword argument' errors.
try: """
conn = ConnectHandler( extra = {k: v for k, v in self.netmiko_optional_args.items()
if k not in ("port", "timeout", "disabled_algorithms")}
return dict(
device_type=self.NETMIKO_DEVICE_TYPE, device_type=self.NETMIKO_DEVICE_TYPE,
host=self.hostname, host=self.hostname,
username=self.username, username=self.username,
@@ -258,9 +260,16 @@ class ProcurveDriver(SwitchDriver):
secret=self._secret, secret=self._secret,
port=self.port, port=self.port,
timeout=self.timeout, timeout=self.timeout,
disabled_algorithms=disabled, disabled_algorithms=_SSH_DISABLED_LEGACY if legacy else _SSH_DISABLED_STANDARD,
**self.netmiko_optional_args, **extra,
) )
def _try_ssh(self, legacy: bool = False) -> bool:
"""Probe and connect via SSH. Returns True on success."""
label = "SSH-legacy" if legacy else "SSH"
logger.debug("Trying %s for %s", label, self.hostname)
try:
conn = ConnectHandler(**self._netmiko_kwargs(legacy))
self._device = conn self._device = conn
self._transport = "ssh_legacy" if legacy else "ssh" self._transport = "ssh_legacy" if legacy else "ssh"
logger.info("Connected to %s via %s", self.hostname, label) logger.info("Connected to %s via %s", self.hostname, label)
@@ -1114,19 +1123,8 @@ class ProcurveDriver(SwitchDriver):
ssh_error: str = "" ssh_error: str = ""
for legacy in (False, True): for legacy in (False, True):
disabled = _SSH_DISABLED_LEGACY if legacy else _SSH_DISABLED_STANDARD
try: try:
conn = ConnectHandler( conn = ConnectHandler(**self._netmiko_kwargs(legacy))
device_type=self.NETMIKO_DEVICE_TYPE,
host=self.hostname,
username=self.username,
password=self.password,
secret=self._secret,
port=self.port,
timeout=self.timeout,
disabled_algorithms=disabled,
**self.netmiko_optional_args,
)
self._device = conn self._device = conn
self._transport = "ssh_legacy" if legacy else "ssh" self._transport = "ssh_legacy" if legacy else "ssh"
break break