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

+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)