fix: quote ping arguments, sudo -n runs the whole command, ip route types (#55, #56, #57) #61

Merged
christianmanivong merged 6 commits from fix/bugs-2026-11-02 into main 2026-11-02 08:15:56 +00:00
5 changed files with 51 additions and 6 deletions
Showing only changes of commit c2d7c4d1b4 - Show all commits

No files matched your search

+4
View File
@@ -45,6 +45,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
on 3.12, so 3.10 and 3.11 are no longer tested (NAPALM/driver-standard#6).
### Fixed
- `ping()` passes `destination` and `source` to the host's shell as one quoted word
each (#55). A destination such as `192.0.2.1; id` ran the command after the `;` as
the login user. `count`, `timeout`, `size` and `ttl` must be numbers; anything else
raises `ValueError` before a command is sent.
- dnf and yum hosts read pending updates and security advisories from the cached
metadata only (`--cacheonly`), as apt reads its local index (#59). With an expired
cache `check-update` downloaded the repositories' metadata first, and on an Oracle
+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)
+3 -5
View File
@@ -74,7 +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._ping import parse_ping, ping_command
from napalm_linux._routes import IP_ROUTE_COMMANDS, parse_ip_route
from napalm_linux._shell import expect_for, prepare_shell
from napalm_linux._sudo_history import LinuxSudoHistoryMixin
@@ -1086,10 +1086,8 @@ class LinuxDriver(
count: int = 5,
vrf: str = "",
) -> dict[str, Any]:
"""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"
return parse_ping(self._send(cmd))
"""Ping from the remote host; ValueError for a count, timeout, size or ttl not a number."""
return parse_ping(self._send(ping_command(destination, source, ttl, timeout, size, count)))
# ------------------------------------------------------------------
# KernelFactsMixin – the transport for get_kernel_facts
+1 -1
View File
@@ -21,7 +21,7 @@
"napalm_linux.linux"
],
"python_file_lines": {
"napalm_linux/linux.py": 2142
"napalm_linux/linux.py": 2140
},
"untyped_public": []
}
+30
View File
@@ -516,6 +516,36 @@ class TestPingFormats:
assert driver.ping("nosuch") == {"error": "ping: unknown host nosuch"}
class TestPingArguments:
"""``ping`` types its arguments into a shell: each one stays one word (#55)."""
def _sent(self, driver, *args, **kwargs):
with patch.object(driver, "_send", return_value=PING_OUTPUT) as send:
driver.ping(*args, **kwargs)
return send.call_args[0][0]
def test_the_destination_is_one_quoted_word(self, driver):
sent = self._sent(driver, "1.2.3.4; id")
assert sent == "ping -c 5 -W 2 -s 100 -t 255 '1.2.3.4; id' 2>&1"
def test_the_source_is_one_quoted_word(self, driver):
sent = self._sent(driver, "192.0.2.1", source="$(id)")
assert sent == "ping -c 5 -W 2 -s 100 -t 255 -I '$(id)' 192.0.2.1 2>&1"
def test_the_numbers_are_numbers(self, driver):
sent = self._sent(driver, "192.0.2.1", count="3", timeout=1.0, size="56", ttl=64)
assert sent == "ping -c 3 -W 1 -s 56 -t 64 192.0.2.1 2>&1"
def test_a_number_that_is_not_one_is_refused_before_anything_is_sent(self, driver):
with patch.object(driver, "_send") as send, pytest.raises(ValueError):
driver.ping("192.0.2.1", count="5; id")
send.assert_not_called()
# ---------------------------------------------------------------------------
# apply_updates – input validation
# ---------------------------------------------------------------------------