From 49b7d7fef70f01823190b6a7a5a2e385b5c36cd0 Mon Sep 17 00:00:00 2001 From: Christian Manivong Date: Sun, 1 Nov 2026 00:26:47 +0100 Subject: [PATCH 1/2] feat: open() waits out sshd's per-source penalty (NetOrk/netork#1800) OpenSSH 9.8+ closes every connection from a source it has just penalised, before its banner, for seconds to minutes. open() now wraps netmiko's ConnectHandler in napalm-device-types' retry_refused, so the connection is tried again after 1, 3, 7 and 15 s instead of failing. Needs NAPALM/napalm-device-types#63. --- CHANGELOG.md | 5 +++++ napalm_linux/linux.py | 25 +++++++++++++++---------- tests/test_ssh_host_key.py | 27 +++++++++++++++++++++++++++ 3 files changed, 47 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 407db2e..264818b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] ### Added +- `open()` tries the connection again after 1, 3, 7 and 15 s when sshd closes it before + its banner -- what OpenSSH 9.8+ does to a source it has just penalised + (`PerSourcePenalties`), e.g. after a security scanner's probes. Needs + napalm-device-types' `retry_refused` (NAPALM/napalm-device-types#63, + NetOrk/netork#1800). - The sudo password the old `_sudo` left in shell histories (NetOrk/netork#1090), through napalm-device-types' `SudoHistoryMixin`. Until NetOrk/netork#1039 every privileged command was typed into the interactive shell as `echo '' | sudo -S -p "" …`, so the diff --git a/napalm_linux/linux.py b/napalm_linux/linux.py index 6bc555d..c219a97 100644 --- a/napalm_linux/linux.py +++ b/napalm_linux/linux.py @@ -67,7 +67,7 @@ from napalm_device_types.models import ( UpdateDict, UserDict, ) -from napalm_device_types.ssh_host_keys import HostKeyCheck +from napalm_device_types.ssh_host_keys import HostKeyCheck, retry_refused from netmiko import ConnectHandler from netmiko.exceptions import NetmikoAuthenticationException, NetmikoTimeoutException @@ -391,15 +391,20 @@ class LinuxDriver( """Open the SSH connection and detect the package manager.""" try: with self._host_key.netmiko(self.netmiko_optional_args) as netmiko_args_: - self._device = ConnectHandler( - device_type=self.NETMIKO_DEVICE_TYPE, - host=self.hostname, - username=self.username, - password=self.password, - port=self.port, - secret=self._secret, - timeout=self.timeout, - **netmiko_args_, + # Again after a pause when sshd closes the connection before its + # banner: OpenSSH 9.8+ does that to a source it has just + # penalised (NetOrk/netork#1800). + self._device = retry_refused( + lambda: ConnectHandler( + device_type=self.NETMIKO_DEVICE_TYPE, + host=self.hostname, + username=self.username, + password=self.password, + port=self.port, + secret=self._secret, + timeout=self.timeout, + **netmiko_args_, + ) ) except NetmikoAuthenticationException as exc: raise ConnectionException(str(exc)) from exc diff --git a/tests/test_ssh_host_key.py b/tests/test_ssh_host_key.py index a1415f7..8758aa7 100644 --- a/tests/test_ssh_host_key.py +++ b/tests/test_ssh_host_key.py @@ -86,3 +86,30 @@ def test_a_changed_key_is_refused(): driver.open() assert driver._device is None assert seen[0]["accepted"] is False + + +def test_a_connection_sshd_turns_away_is_tried_again(monkeypatch): + """OpenSSH 9.8+ closes every connection from a source it has just penalised, + before its banner; the penalty ends after seconds (NetOrk/netork#1800).""" + from napalm_device_types import ssh_host_keys + + slept: list[float] = [] + monkeypatch.setattr(ssh_host_keys.time, "sleep", slept.append) + attempts: list[int] = [] + + def turned_away(**kwargs): + attempts.append(1) + if len(attempts) < 3: + try: + raise paramiko.SSHException("Error reading SSH protocol banner") + except paramiko.SSHException as exc: + raise NetmikoTimeoutException(f"A paramiko SSHException occurred: {exc}") from exc + return _device() + + driver = _driver([]) + with patch("napalm_linux.linux.ConnectHandler", side_effect=turned_away): + driver.open() + + assert driver._device is not None + assert len(attempts) == 3 + assert slept == list(ssh_host_keys.REFUSED_RETRY_WAITS[:2]) From bff275402efb21f4415e0cec2aabcf6076325835 Mon Sep 17 00:00:00 2001 From: Christian Manivong Date: Sun, 1 Nov 2026 00:33:32 +0100 Subject: [PATCH 2/2] fix: keep linux.py within its line count (quality ratchet) One handler for the two netmiko failures that both became a ConnectionException, and the retry's note on its own line. --- napalm_linux/linux.py | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/napalm_linux/linux.py b/napalm_linux/linux.py index c219a97..800ff01 100644 --- a/napalm_linux/linux.py +++ b/napalm_linux/linux.py @@ -391,10 +391,7 @@ class LinuxDriver( """Open the SSH connection and detect the package manager.""" try: with self._host_key.netmiko(self.netmiko_optional_args) as netmiko_args_: - # Again after a pause when sshd closes the connection before its - # banner: OpenSSH 9.8+ does that to a source it has just - # penalised (NetOrk/netork#1800). - self._device = retry_refused( + self._device = retry_refused( # waits out sshd's penalty (NetOrk/netork#1800) lambda: ConnectHandler( device_type=self.NETMIKO_DEVICE_TYPE, host=self.hostname, @@ -406,9 +403,7 @@ class LinuxDriver( **netmiko_args_, ) ) - except NetmikoAuthenticationException as exc: - raise ConnectionException(str(exc)) from exc - except NetmikoTimeoutException as exc: + except (NetmikoAuthenticationException, NetmikoTimeoutException) as exc: raise ConnectionException(str(exc)) from exc self._host_key.observe(self._device.remote_conn_pre.get_transport())