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:
1 parent
024f68c543
commit
69f1701ea6
5 files changed
+149
-41
No files matched your search
@@ -0,0 +1,47 @@
|
||||
"""NAPALM's ``ping()`` reply from what ``ping(8)`` prints: iputils and BusyBox, IPv4 and IPv6."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import re
|
||||
from typing import Any
|
||||
|
||||
#: iputils: ``5 packets transmitted, 5 received, +2 errors, 0% packet loss, time 4005ms``;
|
||||
#: BusyBox: ``2 packets transmitted, 2 packets received, 0% packet loss`` (#15).
|
||||
_SUMMARY = re.compile(
|
||||
r"(\d+) packets transmitted, (\d+) (?:packets )?received,(?: [^,]*,)*? ([\d.]+)% packet loss"
|
||||
)
|
||||
#: iputils: ``rtt min/avg/max/mdev = 0.1/0.2/0.3/0.05 ms``;
|
||||
#: BusyBox: ``round-trip min/avg/max = 0.1/0.2/0.3 ms``, without a deviation.
|
||||
_RTT = re.compile(
|
||||
r"(?:rtt|round-trip) min/avg/max(?:/\w+)? = ([\d.]+)/([\d.]+)/([\d.]+)(?:/([\d.]+))? ms"
|
||||
)
|
||||
#: One reply: ``bytes from <address>:`` or, for a name, ``bytes from <name> (<address>):``.
|
||||
#: The address may be IPv6 (``2001:db8::1``, ``fe80::1%eth0``); ``icmp_seq=`` (iputils)
|
||||
#: or ``seq=`` (BusyBox), and a ``ttl=`` that is not always there, come before the time.
|
||||
_PROBE = re.compile(r"bytes from (?:\S+ \()?([^\s()]+?)\)?: .*?\btime=([\d.]+) ?ms")
|
||||
|
||||
|
||||
def parse_ping(output: str) -> dict[str, Any]:
|
||||
"""``{"success": {...}}`` for *output* with a summary line, else ``{"error": output}``."""
|
||||
summary = _SUMMARY.search(output)
|
||||
if not summary:
|
||||
return {"error": output}
|
||||
sent, received = int(summary.group(1)), int(summary.group(2))
|
||||
rtt = _RTT.search(output)
|
||||
rtts = [float(v) if v else 0.0 for v in rtt.groups()] if rtt else [0.0] * 4
|
||||
results = [
|
||||
{"ip_address": probe.group(1), "rtt": float(probe.group(2))}
|
||||
for probe in map(_PROBE.search, output.splitlines())
|
||||
if probe
|
||||
]
|
||||
return {
|
||||
"success": {
|
||||
"probes_sent": sent,
|
||||
"packet_loss": sent - received,
|
||||
"rtt_min": rtts[0],
|
||||
"rtt_avg": rtts[1],
|
||||
"rtt_max": rtts[2],
|
||||
"rtt_stddev": rtts[3],
|
||||
"results": results,
|
||||
}
|
||||
}
|
||||
+2
-39
@@ -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
|
||||
|
||||
Reference in new issue
Block a user