fix(ping): quote the destination and source in the ping command

ping() typed its arguments into the interactive shell as they came, so a
destination such as "1.2.3.4; id" ran the command after the semicolon as
the login user. destination and source now go through shlex.quote, and
count, timeout, size and ttl are coerced with int(), which refuses
anything that is not a number before a command is sent.

The command is built in _ping.py, next to its parser, which keeps
linux.py within its recorded length.

Closes #55
This commit is contained in:
Christian Manivong committed 2026-11-02 08:37:15 +01:00
1 parent 5687f5d9a1
commit c2d7c4d1b4
5 files changed
+51 -6

No files matched your search

+13
View File
@@ -3,6 +3,7 @@
from __future__ import annotations
import re
from shlex import quote
from typing import Any
#: iputils: ``5 packets transmitted, 5 received, +2 errors, 0% packet loss, time 4005ms``;
@@ -21,6 +22,18 @@ _RTT = re.compile(
_PROBE = re.compile(r"bytes from (?:\S+ \()?([^\s()]+?)\)?: .*?\btime=([\d.]+) ?ms")
def ping_command(
destination: str, source: str, ttl: int, timeout: int, size: int, count: int
) -> str:
"""The shell line that pings *destination*: the names quoted, the numbers numbers (#55).
:raises ValueError: for a count, timeout, size or ttl that is no number.
"""
numbers = f"-c {int(count)} -W {int(timeout)} -s {int(size)} -t {int(ttl)}"
src_opt = f" -I {quote(source)}" if source else ""
return f"ping {numbers}{src_opt} {quote(destination)} 2>&1"
def parse_ping(output: str) -> dict[str, Any]:
"""``{"success": {...}}`` for *output* with a summary line, else ``{"error": output}``."""
summary = _SUMMARY.search(output)