fix(ping): read BusyBox's ping and IPv6 replies

ping() only knew the iputils summary. BusyBox, the ping on Alpine,
writes "2 packets received", so every target came back as an error and
the ping sweep counted it as down. Its probe lines ("seq=") and every
IPv6 reply were dropped by an IPv4-only per-probe pattern, and its
"round-trip min/avg/max" line without a deviation was not read.

The parse moves to napalm_linux/_ping.py and accepts both summaries
(also iputils' with "+N errors"), both RTT lines, IPv6 and named
replies. The shared Unix ping parser is still open as
NAPALM/napalm-device-types#12.

Closes #15
This commit is contained in:
Christian Manivong committed 2026-11-01 21:49:28 +01:00
1 parent 024f68c543
commit 69f1701ea6
5 files changed
+149 -41

No files matched your search

+2 -39
View File
@@ -74,6 +74,7 @@ from netmiko.exceptions import NetmikoAuthenticationException, NetmikoTimeoutExc
from napalm_linux._dpkg import DPKG_QUERY_COMMAND, parse_dpkg_query
from napalm_linux._hostname import HostnameMixin
from napalm_linux._local_accounts import LinuxLocalAccountsMixin
from napalm_linux._ping import parse_ping
from napalm_linux._shell import expect_for, prepare_shell
from napalm_linux._sudo_history import LinuxSudoHistoryMixin
@@ -1122,45 +1123,7 @@ class LinuxDriver(
"""Execute ping from the remote host."""
src_opt = f"-I {source}" if source else ""
cmd = f"ping -c {count} -W {timeout} -s {size} -t {ttl} {src_opt} {destination} 2>&1"
output = self._send(cmd)
# Parse summary line: "5 packets transmitted, 5 received, 0% packet loss"
m = re.search(
r"(\d+) packets transmitted,\s*(\d+) received,\s*([\d.]+)% packet loss",
output,
)
if not m:
return {"error": output}
sent, received = int(m.group(1)), int(m.group(2))
# Parse rtt line: "rtt min/avg/max/mdev = 0.123/0.456/0.789/0.100 ms"
rtt_m = re.search(r"rtt .* = ([\d.]+)/([\d.]+)/([\d.]+)/([\d.]+) ms", output)
results = []
for line in output.splitlines():
icmp_m = re.search(
r"bytes from ([\d.]+).*icmp_seq=\d+ ttl=(\d+) time=([\d.]+) ms", line
)
if icmp_m:
results.append(
{
"ip_address": icmp_m.group(1),
"rtt": float(icmp_m.group(3)),
}
)
return {
"success": {
"probes_sent": sent,
"packet_loss": sent - received,
"rtt_min": float(rtt_m.group(1)) if rtt_m else 0.0,
"rtt_avg": float(rtt_m.group(2)) if rtt_m else 0.0,
"rtt_max": float(rtt_m.group(3)) if rtt_m else 0.0,
"rtt_stddev": float(rtt_m.group(4)) if rtt_m else 0.0,
"results": results,
}
}
return parse_ping(self._send(cmd))
# ------------------------------------------------------------------
# KernelFactsMixin – the transport for get_kernel_facts