From f51585947293507b90c63e9a7efc200c3bd4e41b Mon Sep 17 00:00:00 2001 From: Christian Manivong Date: Sun, 1 Nov 2026 21:52:30 +0100 Subject: [PATCH] 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