fix(sudo): run the whole command as root without a sudo password

Without a root login and without a sudo password, _run_privileged typed
"sudo -n <command>" into the interactive shell. sudo covered only the
first simple command of the line, and dnf's and yum's advisory query,
which starts with a brace group, did not parse at all: no status marker
ever arrived, so netmiko waited out the 120 s read timeout and
get_available_updates() failed. This is the default path for netOrk's
own NOPASSWD account.

The command now goes to "sudo -n sh -c" as one argument, as _as_root
already does with a password. A status echo at its end stays outside
sh -c: it reports sh's status, which is the command's, and still
arrives when sudo refuses.

Closes #56
This commit is contained in:
Christian Manivong committed 2026-11-02 08:39:50 +01:00
1 parent c2d7c4d1b4
commit d3134c83b8
5 files changed
+232 -7

No files matched your search

+19
View File
@@ -4,6 +4,7 @@ from __future__ import annotations
import logging
import re
from shlex import quote
from typing import Any
logger = logging.getLogger(__name__)
@@ -17,6 +18,10 @@ PROMPT_RE = r"[#$\>]\s*$"
#: napalm-device-types.
STATUS_ECHO_RE = re.compile(r"echo\s+(__[A-Z_]+=)\$\?")
#: The same echo as the last command of a line: ``sudo_without_password`` keeps it
#: outside sudo.
_TRAILING_STATUS_ECHO_RE = re.compile(r";\s*echo\s+__[A-Z_]+=\$\?\s*$")
def expect_for(command: str) -> str:
"""The pattern that ends *command*'s output.
@@ -35,6 +40,20 @@ def expect_for(command: str) -> str:
return re.escape(markers[-1]) + r"\d+\s*\n.*" + PROMPT_RE
def sudo_without_password(command: str) -> str:
"""The line that runs all of *command* as root through ``sudo -n`` (#56).
The command goes to ``sh -c`` as one argument: ``sudo -n <command>`` covered only
its first simple command, and one that starts with a brace group did not parse.
A status echo at its end stays outside and reports sh's status, the command's:
it arrives when sudo refuses as well, where netmiko would otherwise wait for it
until the read timeout.
"""
echo = _TRAILING_STATUS_ECHO_RE.search(command)
body, tail = (command[: echo.start()], echo.group()) if echo else (command, "")
return f"sudo -n sh -c {quote(body)}{tail}"
#: Sent before anything else in the interactive session (NetOrk/netork#1039):
#: the shell writes no history file and records nothing more. The leading space
#: keeps this line itself out of a history under ``HISTCONTROL=ignorespace``;
+5 -5
View File
@@ -76,7 +76,7 @@ from napalm_linux._hostname import HostnameMixin
from napalm_linux._local_accounts import LinuxLocalAccountsMixin
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._shell import expect_for, prepare_shell, sudo_without_password
from napalm_linux._sudo_history import LinuxSudoHistoryMixin
logger = logging.getLogger("napalm_linux")
@@ -510,14 +510,14 @@ class LinuxDriver(
return self._run_privileged(command, timeout)
def _run_privileged(self, command: str, timeout: float = 100) -> str:
"""Run *command* as root: directly for a root login, through ``_sudo``
with a sudo password, and through ``sudo -n`` without one -- which fails at
once where a password prompt would hang the session until the timeout."""
"""Run all of *command* as root: directly for a root login, through ``_sudo``
with a sudo password, and through ``sudo -n sh -c`` without one -- which fails
at once where a password prompt would hang the session until the timeout."""
if self._is_root():
return self._send(command, read_timeout=timeout)
if self._sudo_password:
return self._sudo(command, read_timeout=timeout) # never typed into the shell
return self._send(f"sudo -n {command}", read_timeout=timeout)
return self._send(sudo_without_password(command), read_timeout=timeout)
# ------------------------------------------------------------------
# Command channel (napalm-device-types CommandChannelMixin)