From f51585947293507b90c63e9a7efc200c3bd4e41b Mon Sep 17 00:00:00 2001 From: Christian Manivong Date: Sun, 1 Nov 2026 21:52:30 +0100 Subject: [PATCH 1/3] fix(ping): list replies only, not timed-out requests Each "Request timed out" line went into results with the timeout as its RTT. NAPALM's results hold replies, and a sweep treats a non-empty results list as a live host, so dead hosts showed up in LAN scans. Closes #2 --- CHANGELOG.md | 2 + napalm_tplink_jetstream/tplink_jetstream.py | 4 +- tests/quality_baseline.json | 4 +- tests/unit/test_ping.py | 51 +++++++++++++++++++++ 4 files changed, 56 insertions(+), 5 deletions(-) create mode 100644 tests/unit/test_ping.py diff --git a/CHANGELOG.md b/CHANGELOG.md index 388da34..c7873f4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,8 @@ All notable changes to this driver. The format follows 10-second `conn_timeout`, so every other connection failed and the switch flapped between online and offline. `diffie-hellman-group14-sha256` takes 1.5 seconds. A `disabled_algorithms` in `optional_args` still takes precedence. +- `ping()` lists only replies in `results`. A "Request timed out" line was listed too, + so a ping sweep reported hosts that never answered as alive. ### Changed - Python 3.12 is the floor (`requires-python = ">=3.12"`): netOrk runs every driver diff --git a/napalm_tplink_jetstream/tplink_jetstream.py b/napalm_tplink_jetstream/tplink_jetstream.py index a285552..b056ef1 100644 --- a/napalm_tplink_jetstream/tplink_jetstream.py +++ b/napalm_tplink_jetstream/tplink_jetstream.py @@ -1835,7 +1835,7 @@ class TPLinkJetstreamDriver(ConfigLifecycleMixin, SnmpAgentMixin, SwitchDriver): rtt_max = float(m.group(2)) rtt_avg = float(m.group(3)) - # Parse individual reply RTTs (time<16ms → 0ms threshold; treat as 1ms) + # Replies only: a sweep reads any result as a live host (#2). results = [] for reply_line in output.splitlines(): m = re.match(r"Reply from ([\d.]+)\s*:.*?time[<=](\d+)ms", reply_line) @@ -1843,8 +1843,6 @@ class TPLinkJetstreamDriver(ConfigLifecycleMixin, SnmpAgentMixin, SwitchDriver): rtt_val = float(m.group(2)) # "time<16ms" means it rounded down; at least 1ms results.append({"ip_address": m.group(1), "rtt": rtt_val if rtt_val > 0 else 1.0}) - elif re.search(r"Request timed out", reply_line, re.I): - results.append({"ip_address": destination, "rtt": timeout * 1000.0}) return { "success": { diff --git a/tests/quality_baseline.json b/tests/quality_baseline.json index 6354393..9f261a3 100644 --- a/tests/quality_baseline.json +++ b/tests/quality_baseline.json @@ -20,14 +20,14 @@ "napalm_tplink_jetstream/tplink_jetstream.py::TPLinkJetstreamDriver.get_mac_address_table": 60, "napalm_tplink_jetstream/tplink_jetstream.py::TPLinkJetstreamDriver.get_route_to": 68, "napalm_tplink_jetstream/tplink_jetstream.py::TPLinkJetstreamDriver.get_vlans_detail": 52, - "napalm_tplink_jetstream/tplink_jetstream.py::TPLinkJetstreamDriver.ping": 64, + "napalm_tplink_jetstream/tplink_jetstream.py::TPLinkJetstreamDriver.ping": 62, "napalm_tplink_jetstream/tplink_jetstream.py::TPLinkJetstreamDriver.traceroute": 55 }, "mypy_relaxed": [ "napalm_tplink_jetstream.tplink_jetstream" ], "python_file_lines": { - "napalm_tplink_jetstream/tplink_jetstream.py": 2029 + "napalm_tplink_jetstream/tplink_jetstream.py": 2027 }, "untyped_public": [ "napalm_tplink_jetstream/tplink_jetstream.py::TPLinkJetstreamDriver.get_snmp_config" diff --git a/tests/unit/test_ping.py b/tests/unit/test_ping.py new file mode 100644 index 0000000..9df9e77 --- /dev/null +++ b/tests/unit/test_ping.py @@ -0,0 +1,51 @@ +"""ping's results hold replies only (#2). + +A "Request timed out" line was added to ``results`` with the timeout as its +RTT, and a sweep treats a non-empty ``results`` as a live host, so hosts that +never answered appeared in LAN scans. +""" + +from unittest.mock import MagicMock + +from napalm_tplink_jetstream.tplink_jetstream import TPLinkJetstreamDriver + +DEAD = """\ +Pinging 10.0.0.99 with 64 bytes of data: +Request timed out. +Request timed out. +Request timed out. + +Ping statistics for 10.0.0.99: +Packets: Sent = 3 , Received = 0 , Lost = 3 (100% loss). +""" + +HALF = """\ +Pinging 10.0.0.1 with 64 bytes of data: +Reply from 10.0.0.1: bytes=64 time<16ms TTL=64 +Request timed out. + +Ping statistics for 10.0.0.1: +Packets: Sent = 2 , Received = 1 , Lost = 1 (50% loss). +Approximate round trip times in milli-seconds: +Minimum = 0ms , Maximum = 0ms , Average = 0ms +""" + + +def _ping(output: str, destination: str, count: int) -> dict: + driver = TPLinkJetstreamDriver("10.0.0.30", "admin", "pw") + driver._send_command = MagicMock(return_value=output) + return driver.ping(destination, count=count) + + +def test_a_host_that_only_times_out_has_no_results(): + result = _ping(DEAD, "10.0.0.99", 3)["success"] + + assert result["results"] == [] + assert result["packet_loss"] == 3 + + +def test_only_the_replies_are_results(): + result = _ping(HALF, "10.0.0.1", 2)["success"] + + assert result["results"] == [{"ip_address": "10.0.0.1", "rtt": 16.0}] + assert result["packet_loss"] == 1 -- 2.54.0 From 34f00f9f3aea699dfd5d85cfaf36d0e811ef8b57 Mon Sep 17 00:00:00 2001 From: Christian Manivong Date: Sun, 1 Nov 2026 21:52:55 +0100 Subject: [PATCH 2/3] fix(snmp): report no community instead of inventing "public" get_snmp_config started from community "public" and returned it with running=True when no row of the community table parsed. Without a listed community it now reports the agent as not running, as it already did for "no community entry exists". The issue's other two points were already fixed on main: the management VLAN is matched exactly since c2f0f7c (_management_vlan, with the caller's listen address), and the floor on napalm-device-types is 4.21.1 with no requirements.txt since 4e171bf. Closes #3 --- CHANGELOG.md | 2 + napalm_tplink_jetstream/tplink_jetstream.py | 21 +++++------ tests/quality_baseline.json | 6 +-- tests/unit/test_snmp_config.py | 42 +++++++++++++++++++++ 4 files changed, 55 insertions(+), 16 deletions(-) create mode 100644 tests/unit/test_snmp_config.py diff --git a/CHANGELOG.md b/CHANGELOG.md index c7873f4..d1145c5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,8 @@ All notable changes to this driver. The format follows `disabled_algorithms` in `optional_args` still takes precedence. - `ping()` lists only replies in `results`. A "Request timed out" line was listed too, so a ping sweep reported hosts that never answered as alive. +- `get_snmp_config()` no longer reports the community `"public"` when the switch lists + none: without a listed community it reports the agent as not running. ### Changed - Python 3.12 is the floor (`requires-python = ">=3.12"`): netOrk runs every driver diff --git a/napalm_tplink_jetstream/tplink_jetstream.py b/napalm_tplink_jetstream/tplink_jetstream.py index b056ef1..b1a6bab 100644 --- a/napalm_tplink_jetstream/tplink_jetstream.py +++ b/napalm_tplink_jetstream/tplink_jetstream.py @@ -33,7 +33,7 @@ from napalm.base.exceptions import ( ReplaceConfigException, ) from napalm_device_types import ConfigLifecycleMixin, FingerprintRule, SnmpAgentMixin, SwitchDriver -from napalm_device_types.models import InterfaceConfigDict, VlanConfigDict +from napalm_device_types.models import InterfaceConfigDict, SNMPConfigDict, VlanConfigDict from napalm_device_types.ssh_host_keys import HostKeyCheck, SshHostKeyMismatch from netmiko import ConnectHandler from netmiko.exceptions import NetmikoAuthenticationException, NetmikoTimeoutException @@ -1927,23 +1927,19 @@ class TPLinkJetstreamDriver(ConfigLifecycleMixin, SnmpAgentMixin, SwitchDriver): # ── SNMP / Health ────────────────────────────────────────────────────────── - def get_snmp_config(self): + def get_snmp_config(self) -> SNMPConfigDict: """Return SNMP agent config if SNMP is enabled on the switch. TP-Link JetStream switches have SNMP built-in — no daemon to install. - Reads community from 'show snmp community'. + Reads community from 'show snmp community'; without a listed one the agent + does not run (#3). """ - try: - from napalm_device_types.models import SNMPConfigDict - except ImportError: - return None - + no_agent = SNMPConfigDict(running=False, community="", port=161, version="2c") out = self._send_command("show snmp community") if not out or "disabled" in out.lower() or "no community entry exists" in out.lower(): - return SNMPConfigDict(running=False, community="", port=161, version="2c") + return no_agent - # Parse first read-only community - community = "public" + community = None in_table = False for line in out.splitlines(): line_s = line.strip() @@ -1956,7 +1952,8 @@ class TPLinkJetstreamDriver(ConfigLifecycleMixin, SnmpAgentMixin, SwitchDriver): if len(parts) >= 2: community = parts[0] break - + if community is None: + return no_agent return SNMPConfigDict(running=True, community=community, port=161, version="2c") def get_device_warnings(self) -> list: diff --git a/tests/quality_baseline.json b/tests/quality_baseline.json index 9f261a3..7094dfe 100644 --- a/tests/quality_baseline.json +++ b/tests/quality_baseline.json @@ -27,9 +27,7 @@ "napalm_tplink_jetstream.tplink_jetstream" ], "python_file_lines": { - "napalm_tplink_jetstream/tplink_jetstream.py": 2027 + "napalm_tplink_jetstream/tplink_jetstream.py": 2024 }, - "untyped_public": [ - "napalm_tplink_jetstream/tplink_jetstream.py::TPLinkJetstreamDriver.get_snmp_config" - ] + "untyped_public": [] } diff --git a/tests/unit/test_snmp_config.py b/tests/unit/test_snmp_config.py new file mode 100644 index 0000000..40ad090 --- /dev/null +++ b/tests/unit/test_snmp_config.py @@ -0,0 +1,42 @@ +"""get_snmp_config reports the community the switch lists, never one it made up (#3). + +It started from "public" and kept it when no table row parsed, so a switch +whose community table could not be read reported a running agent with the +community "public". +""" + +from unittest.mock import MagicMock + +import pytest + +from napalm_tplink_jetstream.tplink_jetstream import TPLinkJetstreamDriver + + +def _driver(output: str) -> TPLinkJetstreamDriver: + driver = TPLinkJetstreamDriver("10.0.0.30", "admin", "pw") + driver._send_command = MagicMock(return_value=output) + return driver + + +def test_the_listed_community_is_reported(): + out = "Index Community-Name Access\n----- -------------- ------\nc0mm read-only x\n" + + config = _driver(out).get_snmp_config() + + assert config == {"running": True, "community": "c0mm", "port": 161, "version": "2c"} + + +@pytest.mark.parametrize( + "output", + [ + "Index Community-Name Access\n----- -------------- ------\n", + "Index Community-Name Access\n", + "something the parser does not know\n", + ], +) +def test_without_a_listed_community_none_is_invented(output): + config = _driver(output).get_snmp_config() + + assert config is not None + assert config["community"] != "public" + assert config == {"running": False, "community": "", "port": 161, "version": "2c"} -- 2.54.0 From 8e7be0e47e2038480452d7717be2eef07faf3e73 Mon Sep 17 00:00:00 2001 From: Christian Manivong Date: Sun, 1 Nov 2026 22:19:33 +0100 Subject: [PATCH 3/3] fix(snmp): read the community from the Name column The switch's community table starts with an Index column, and get_snmp_config took a row's first word, so it reported the community as "1". The name now comes from the column headed Name, found in the header line above the dashes; the tests read the recorded table. Found while recording fixtures for #3. --- CHANGELOG.md | 2 ++ napalm_tplink_jetstream/parsers.py | 29 +++++++++++++++++++ napalm_tplink_jetstream/tplink_jetstream.py | 19 +++--------- .../jetstream-show-snmp-server-community.txt | 3 ++ tests/quality_baseline.json | 2 +- tests/unit/test_snmp_config.py | 22 ++++++++++---- 6 files changed, 55 insertions(+), 22 deletions(-) create mode 100644 napalm_tplink_jetstream/parsers.py create mode 100644 tests/fixtures/jetstream-show-snmp-server-community.txt diff --git a/CHANGELOG.md b/CHANGELOG.md index d1145c5..6d5b4f2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,8 @@ All notable changes to this driver. The format follows ## [Unreleased] ### Fixed +- `get_snmp_config()` reads the community from the table's `Name` column. It took the + first column, which is `Index` on the switch, and reported the community as `1`. - The SSH key exchange no longer uses `diffie-hellman-group16-sha512`. On an SG2210P with firmware 5.30 its 4096-bit exchange took 9 to 16 seconds, against netmiko's 10-second `conn_timeout`, so every other connection failed and the switch flapped diff --git a/napalm_tplink_jetstream/parsers.py b/napalm_tplink_jetstream/parsers.py new file mode 100644 index 0000000..017a21c --- /dev/null +++ b/napalm_tplink_jetstream/parsers.py @@ -0,0 +1,29 @@ +"""Pure parsers: the switch's CLI output in, plain values out.""" + +from __future__ import annotations + +import re + +_DASHES = re.compile(r"^-{4,}$") + + +def parse_snmp_communities(output: str) -> list[str]: + """The community names in ``show snmp-server community``, in the switch's order. + + The table starts with an ``Index`` column: the name is the column headed + ``Name``, found in the header line above the dashes. Without one, no name. + """ + names: list[str] = [] + header: list[str] = [] + column: int | None = None + for line in output.splitlines(): + words = line.split() + if not words: + continue + if column is None: + if _DASHES.match(words[0]): + column = next((i for i, w in enumerate(header) if "name" in w.lower()), -1) + header = words + elif 0 <= column < len(words): + names.append(words[column]) + return names diff --git a/napalm_tplink_jetstream/tplink_jetstream.py b/napalm_tplink_jetstream/tplink_jetstream.py index b1a6bab..0c512d8 100644 --- a/napalm_tplink_jetstream/tplink_jetstream.py +++ b/napalm_tplink_jetstream/tplink_jetstream.py @@ -39,6 +39,7 @@ from netmiko import ConnectHandler from netmiko.exceptions import NetmikoAuthenticationException, NetmikoTimeoutException from napalm_tplink_jetstream._ssh import netmiko_connect_args +from napalm_tplink_jetstream.parsers import parse_snmp_communities logger = logging.getLogger(__name__) @@ -1939,22 +1940,10 @@ class TPLinkJetstreamDriver(ConfigLifecycleMixin, SnmpAgentMixin, SwitchDriver): if not out or "disabled" in out.lower() or "no community entry exists" in out.lower(): return no_agent - community = None - in_table = False - for line in out.splitlines(): - line_s = line.strip() - if re.match(r"^-{4,}", line_s): - in_table = True - continue - if not in_table or not line_s: - continue - parts = line_s.split() - if len(parts) >= 2: - community = parts[0] - break - if community is None: + communities = parse_snmp_communities(out) + if not communities: return no_agent - return SNMPConfigDict(running=True, community=community, port=161, version="2c") + return SNMPConfigDict(running=True, community=communities[0], port=161, version="2c") def get_device_warnings(self) -> list: """Return device warnings. Currently delegates to SNMP detection only.""" diff --git a/tests/fixtures/jetstream-show-snmp-server-community.txt b/tests/fixtures/jetstream-show-snmp-server-community.txt new file mode 100644 index 0000000..ff62915 --- /dev/null +++ b/tests/fixtures/jetstream-show-snmp-server-community.txt @@ -0,0 +1,3 @@ +Index Name Type MIB-View +----- ---- ---- -------- +1 public read-only viewDefault diff --git a/tests/quality_baseline.json b/tests/quality_baseline.json index 7094dfe..db2f48d 100644 --- a/tests/quality_baseline.json +++ b/tests/quality_baseline.json @@ -27,7 +27,7 @@ "napalm_tplink_jetstream.tplink_jetstream" ], "python_file_lines": { - "napalm_tplink_jetstream/tplink_jetstream.py": 2024 + "napalm_tplink_jetstream/tplink_jetstream.py": 2013 }, "untyped_public": [] } diff --git a/tests/unit/test_snmp_config.py b/tests/unit/test_snmp_config.py index 40ad090..5a7644a 100644 --- a/tests/unit/test_snmp_config.py +++ b/tests/unit/test_snmp_config.py @@ -3,14 +3,24 @@ It started from "public" and kept it when no table row parsed, so a switch whose community table could not be read reported a running agent with the community "public". + +The fixture is ``show snmp-server community`` recorded from a JetStream switch: its +table starts with an ``Index`` column, which was read as the community. """ +from pathlib import Path from unittest.mock import MagicMock import pytest from napalm_tplink_jetstream.tplink_jetstream import TPLinkJetstreamDriver +FIXTURES = Path(__file__).parent.parent / "fixtures" + +SHOW_SNMP_COMMUNITY = (FIXTURES / "jetstream-show-snmp-server-community.txt").read_text() +#: The recording without its only row. +NO_COMMUNITY = "".join(SHOW_SNMP_COMMUNITY.splitlines(keepends=True)[:2]) + def _driver(output: str) -> TPLinkJetstreamDriver: driver = TPLinkJetstreamDriver("10.0.0.30", "admin", "pw") @@ -19,18 +29,18 @@ def _driver(output: str) -> TPLinkJetstreamDriver: def test_the_listed_community_is_reported(): - out = "Index Community-Name Access\n----- -------------- ------\nc0mm read-only x\n" + config = _driver(SHOW_SNMP_COMMUNITY).get_snmp_config() - config = _driver(out).get_snmp_config() - - assert config == {"running": True, "community": "c0mm", "port": 161, "version": "2c"} + assert config == {"running": True, "community": "public", "port": 161, "version": "2c"} @pytest.mark.parametrize( "output", [ - "Index Community-Name Access\n----- -------------- ------\n", - "Index Community-Name Access\n", + NO_COMMUNITY, + SHOW_SNMP_COMMUNITY.splitlines(keepends=True)[0], + # A table without a column headed Name: its rows name no community. + SHOW_SNMP_COMMUNITY.replace("Name ", "Comm ", 1), "something the parser does not know\n", ], ) -- 2.54.0