Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
650379680f | ||
|
|
04a34ca6f5 | ||
|
|
d5870d9813 | ||
|
|
a30d1f6fe5 | ||
|
|
ca32b89686 | ||
|
|
cb0e308a78 | ||
|
|
c7317925ed | ||
|
|
9ac6c7cf04 | ||
|
|
d3189806d3 | ||
|
|
a3d95d72b9 | ||
|
|
ca65f007c1 | ||
|
|
9089429c2d | ||
|
|
a1e8ec78c3 | ||
|
|
3d2de27988 | ||
|
|
e4c65945b9 | ||
|
|
d3134c83b8 | ||
|
|
c2d7c4d1b4 | ||
|
|
5687f5d9a1 | ||
|
|
b891acd29c | ||
|
|
d226ebf764 | ||
|
|
60ff9d9700 | ||
|
|
897892b923 | ||
|
|
69f1701ea6 | ||
|
|
024f68c543 | ||
|
|
54f6844d84 | ||
|
|
df4f4b0e4d | ||
|
|
eaa08ae7e3 | ||
|
|
a729b35d8b | ||
|
|
e9a1fc7ff9 | ||
|
|
f49d1ca7f5 | ||
|
|
dd351f1077 | ||
|
|
b702fa5125 | ||
|
|
448eeec0fa | ||
|
|
9ce915a8b9 | ||
|
|
812a162eef | ||
|
|
bff275402e | ||
|
|
49b7d7fef7 | ||
|
|
c5be71adac | ||
|
|
7550b884b0 | ||
|
|
fbaded5913 | ||
|
|
4cd1710e19 | ||
|
|
05cef25e9b | ||
|
|
2ffad23bf0 | ||
|
|
4108eb83dd | ||
|
|
b6677c9fc5 | ||
|
|
2afdf33d4d | ||
|
|
661f6da3f7 | ||
|
|
42e93f02dd | ||
|
|
4159612a33 |
No files matched your search
+138
@@ -7,6 +7,144 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|||||||
|
|
||||||
## [Unreleased]
|
## [Unreleased]
|
||||||
|
|
||||||
|
## [0.16.0] - 2026-11-03
|
||||||
|
|
||||||
|
### Added
|
||||||
|
- Scheduled jobs (NetOrk/netork#1933, #67), through napalm-device-types' `PosixScheduledJobsMixin`:
|
||||||
|
`get_scheduled_jobs()` reads `/etc/crontab`, `/etc/cron.d`, the user crontabs (Debian,
|
||||||
|
RHEL, Alpine), the run-parts directories, `/etc/anacrontab` and every systemd timer in one
|
||||||
|
round trip as root; `manage_scheduled_job()` enables, disables or runs one now;
|
||||||
|
`put_scheduled_job()` and `delete_scheduled_job()` write and remove an application's own.
|
||||||
|
OpenMediaVault and QNAP QTS inherit it and decide per verb in their own packages.
|
||||||
|
- netOrk's own account with a fixed uid (NetOrk/netork#1817): `_account_create_with_uid`
|
||||||
|
makes it with `useradd -m -u <uid>`. Below `UID_MIN` useradd warns and goes on, as a
|
||||||
|
system's account.
|
||||||
|
- Personal accounts (NetOrk/netork#1793), through napalm-device-types' `LocalAccountsMixin`:
|
||||||
|
`get_local_accounts()`, `add_local_account()` and `lock_local_account()` with
|
||||||
|
shadow-utils' commands (`useradd -m -s /bin/bash`, `usermod`, `gpasswd -d`). A locked
|
||||||
|
account is an expired one (`usermod -L -e 1`): PAM refuses it a key login too, where a
|
||||||
|
`!` in front of the password stops only the password. Opening it again lifts the expiry
|
||||||
|
and gives the old password back. Checked on Debian 13 and Rocky Linux 9 with sshd and
|
||||||
|
sudo. OpenMediaVault (Debian) inherits it; QNAP QTS opts out in napalm-qnap-qts.
|
||||||
|
Requires the napalm-device-types release with `LocalAccountsMixin`.
|
||||||
|
- `open()` tries the connection again after 1, 3, 7 and 15 s when sshd closes it before
|
||||||
|
its banner -- what OpenSSH 9.8+ does to a source it has just penalised
|
||||||
|
(`PerSourcePenalties`), e.g. after a security scanner's probes. Needs
|
||||||
|
napalm-device-types' `retry_refused` (NAPALM/napalm-device-types#63,
|
||||||
|
NetOrk/netork#1800).
|
||||||
|
- The sudo password the old `_sudo` left in shell histories (NetOrk/netork#1090), through
|
||||||
|
napalm-device-types' `SudoHistoryMixin`. Until NetOrk/netork#1039 every privileged command
|
||||||
|
was typed into the interactive shell as `echo '<password>' | sudo -S -p "" …`, so the
|
||||||
|
login user's history kept the password once per command. `get_history_sudo_lines()`
|
||||||
|
counts those lines with `grep -c`, without reading them; `remove_history_sudo_lines()`
|
||||||
|
deletes exactly them and gives `/etc/snmp/snmpd.conf` and `/etc/apt/apt.conf.d/00proxy`
|
||||||
|
back to `root:root` where the old partial sudo left them to the login user (a regular
|
||||||
|
file, never a symbolic link). OpenMediaVault and QNAP inherit both. Requires the
|
||||||
|
napalm-device-types release with `SudoHistoryMixin`.
|
||||||
|
- `set_hostname()` (#45): netOrk could save a Linux host's new name but not apply it
|
||||||
|
("The driver linux does not support set_hostname()"). The host now takes the short
|
||||||
|
name through `hostnamectl`, or `/etc/hostname` and `hostname(1)` without systemd;
|
||||||
|
`/etc/hosts` follows (the lines naming the old name, else the `127.0.1.1` line), and a
|
||||||
|
cloud-init host gets `preserve_hostname: true` so its next boot keeps the name. Names
|
||||||
|
that are not valid hostnames are refused before anything runs on the host.
|
||||||
|
|
||||||
|
### Changed
|
||||||
|
- Python 3.12 is the floor (`requires-python = ">=3.12"`): netOrk runs every driver
|
||||||
|
on 3.12, so 3.10 and 3.11 are no longer tested (NAPALM/driver-standard#6).
|
||||||
|
- The floor on napalm-device-types is 4.24.0, for its shared `lldpctl` parser.
|
||||||
|
|
||||||
|
### Fixed
|
||||||
|
- `get_lldp_neighbors()` raises when lldpd runs but lldpctl fails (its socket out of
|
||||||
|
the login's reach, a daemon restarting), with lldpctl's message, instead of reporting
|
||||||
|
no neighbours (#66). A stopped lldpd is recognised as stopped: systemd's `inactive`
|
||||||
|
used to pass the running check, as it contains the word "active". A host without
|
||||||
|
lldpd, or with lldpd stopped, still reports no neighbours.
|
||||||
|
- `get_lldp_neighbors()` lists every neighbour of a port (#58): a phone and the PC
|
||||||
|
behind it, or the devices behind an unmanaged switch, used to merge into one entry
|
||||||
|
and only the last one was left. A neighbour whose chassis id is a MAC address now
|
||||||
|
carries it as `mac` (it never did), a port given as a MAC address or local id is
|
||||||
|
reported instead of coming back empty, and an interface with a dot in its name
|
||||||
|
(`eth0.100`) keeps its whole name.
|
||||||
|
- `get_route_to()` reads unreachable, blackhole, prohibit and throw routes and
|
||||||
|
multipath routes (#57). Their destinations came back as `unreachable/32`,
|
||||||
|
`blackhole/32` or `nexthop/32`, and a multipath route as one entry without a next
|
||||||
|
hop. A typed route now has its real prefix and its type in
|
||||||
|
`protocol_attributes` (`{"type": "blackhole"}`), with no next hop and no outgoing
|
||||||
|
interface, since it sends nothing out. A multipath route has one entry per next hop,
|
||||||
|
each with its gateway and interface. A host route that FRR reports without a prefix
|
||||||
|
length is a `/128` for IPv6 instead of a `/32` (#64), so it meets the kernel's route
|
||||||
|
for the same host and gets FRR's protocol.
|
||||||
|
- `get_route_to()` reads the next hop of an IPv4 route through an IPv6 gateway (RFC
|
||||||
|
8950, as BGP unnumbered sets up), which `ip route` prints as `via inet6 fe80::1`
|
||||||
|
(#65). The next hop came back as `inet6`; it is now the address, for multipath
|
||||||
|
routes too. Such a route stays `family: ipv4`: the family is the destination's,
|
||||||
|
never the next hop's.
|
||||||
|
- `get_available_updates()` works on dnf and yum hosts whose login user has
|
||||||
|
passwordless sudo (NOPASSWD), as netOrk's own account does, and marks the security
|
||||||
|
fixes there (#56). The security-advisory query did not parse under `sudo -n`, so no
|
||||||
|
answer came back and the read ran into its 120 s timeout. Without a sudo password a
|
||||||
|
privileged command now runs as root as a whole, in one `sh -c`, as it already did
|
||||||
|
with a password; before, only its first command did. A refused sudo still reports
|
||||||
|
its exit status at once.
|
||||||
|
- `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. A destination or source that starts
|
||||||
|
with `-` is refused the same way (#63): `ping` would read `-f` as its flood option,
|
||||||
|
and no address or hostname starts with `-`.
|
||||||
|
- 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
|
||||||
|
Linux host on OCI that outlasted netOrk's whole poll, every time. Refreshing stays
|
||||||
|
`refresh_available_updates()` (`dnf makecache`). Without a cache the read fails at
|
||||||
|
once and says to refresh the index. Requires the napalm-device-types release with
|
||||||
|
NAPALM/napalm-device-types#76 for dnf's security query.
|
||||||
|
- `get_arp_table()` reports an unknown age as `-1.0` (napalm-device-types'
|
||||||
|
`ARP_AGE_UNKNOWN`) instead of `0.0`, which means "confirmed just now": `ip neigh`
|
||||||
|
has no age, and a STALE entry may be hours old (NetOrk/netork#1327).
|
||||||
|
- `install_package()` reports success by the package manager's exit status, as
|
||||||
|
`uninstall_package()` already does (#4). It used to search the output for words like
|
||||||
|
"failed": an install whose postinst merely mentioned a failure read as failed, and a
|
||||||
|
failure that used none of the words read as success.
|
||||||
|
- `get_vpn_tunnels()` makes the public-key prefix in a WireGuard peer's key URL-safe
|
||||||
|
(`+` becomes `-`, `/` becomes `_`), as napalm-opnsense does (#43). A key with `/`
|
||||||
|
could not be used as a URL path segment, so such a tunnel could not be edited in
|
||||||
|
netOrk. The key of a peer whose first eight key characters hold `+` or `/` changes
|
||||||
|
once; `public_key` is reported unchanged.
|
||||||
|
- `get_processes()` reports a process's whole start time in `started`
|
||||||
|
(`Tue Oct 6 08:47:15 2026`) and only the command line in `command` (#21). `started`
|
||||||
|
held the weekday alone and `command` began with the rest of the date. The start time
|
||||||
|
is read in the C locale, so its words are English on every host.
|
||||||
|
- `get_packages()` on apt hosts no longer lists removed packages whose configuration
|
||||||
|
dpkg still keeps (state `rc`) (#17). They were reported as installed: old kernels, or
|
||||||
|
an agent that had been removed. A package that is held, unpacked or half-configured
|
||||||
|
still counts, since its files are on disk.
|
||||||
|
- `ping()` reads BusyBox's output, the `ping` on Alpine (#15). Its summary says
|
||||||
|
"2 packets received", which did not match, so every target came back as an error and
|
||||||
|
a LAN scan through an Alpine host found nothing. Replies to IPv6 targets, BusyBox's
|
||||||
|
`seq=` replies and replies from a name (`bytes from host (address)`) now appear in
|
||||||
|
`results`, BusyBox's `round-trip` line fills the RTT fields, and an iputils summary
|
||||||
|
that counts errors is read as well.
|
||||||
|
- `get_route_to()` reports a dual-stack host's IPv6 default route as `::/0` (#6). It
|
||||||
|
came back as a second `0.0.0.0/0` with `family: ipv6`, because `ip -6 route show`
|
||||||
|
calls its default route `default` as well. An IPv6 host route is now a `/128`
|
||||||
|
instead of a `/32`.
|
||||||
|
|
||||||
|
### Removed
|
||||||
|
- `get_cron_jobs()`: it read without root, missed other users' crontabs, `/etc/crontab`,
|
||||||
|
run-parts, anacron and every systemd timer, and nothing called it. `get_scheduled_jobs()`
|
||||||
|
replaces it.
|
||||||
|
|
||||||
|
## [0.15.1] - 2026-10-23
|
||||||
|
|
||||||
|
### Fixed
|
||||||
|
- Ubuntu 26.04 hosts could not be polled (#41). systemd 258's
|
||||||
|
`/etc/profile.d/80-systemd-osc-context.sh` wraps every command of an interactive bash
|
||||||
|
in OSC 3008 sequences, so they ended up in every value read through the shell, the
|
||||||
|
prompt line with them: `os_version` grew past 255 characters. The session now unsets
|
||||||
|
`PS0` and `PROMPT_COMMAND` right after switching the history off and reads its prompt
|
||||||
|
anew, and `_send()` strips terminal codes from what it returns.
|
||||||
|
|
||||||
## [0.15.0] - 2026-10-23
|
## [0.15.0] - 2026-10-23
|
||||||
|
|
||||||
The first release on PyPI. It collects everything since 0.4.0, which was the last version recorded here.
|
The first release on PyPI. It collects everything since 0.4.0, which was the last version recorded here.
|
||||||
|
|||||||
@@ -62,7 +62,7 @@ with Driver(
|
|||||||
services = dev.get_services()
|
services = dev.get_services()
|
||||||
users = dev.get_os_users()
|
users = dev.get_os_users()
|
||||||
procs = dev.get_processes()
|
procs = dev.get_processes()
|
||||||
jobs = dev.get_cron_jobs()
|
jobs = dev.get_scheduled_jobs()["jobs"]
|
||||||
|
|
||||||
# Upgrade specific packages
|
# Upgrade specific packages
|
||||||
result = dev.apply_updates(["openssh-server", "curl"])
|
result = dev.apply_updates(["openssh-server", "curl"])
|
||||||
@@ -108,7 +108,9 @@ with Driver(
|
|||||||
| `manage_service(name, action)` | ✅ | systemd: start, stop, restart, enable, disable |
|
| `manage_service(name, action)` | ✅ | systemd: start, stop, restart, enable, disable |
|
||||||
| `get_os_users()` | ✅ | `/etc/passwd` + `/etc/group` |
|
| `get_os_users()` | ✅ | `/etc/passwd` + `/etc/group` |
|
||||||
| `get_processes()` | ✅ | `ps axo` |
|
| `get_processes()` | ✅ | `ps axo` |
|
||||||
| `get_cron_jobs()` | ✅ | user crontabs + `/etc/cron.d/` |
|
| `get_scheduled_jobs()` | ✅ | `/etc/crontab`, `/etc/cron.d`, user crontabs, run-parts, anacron, systemd timers (napalm-device-types' `PosixScheduledJobsMixin`) |
|
||||||
|
| `manage_scheduled_job(key, action, digest=)` | ✅ | enable, disable, run now |
|
||||||
|
| `put_scheduled_job(spec)` / `delete_scheduled_job(key, digest=)` | ✅ | an application's own job: a systemd timer, else `/etc/cron.d`, else the user's crontab |
|
||||||
|
|
||||||
## Package manager auto-detection
|
## Package manager auto-detection
|
||||||
|
|
||||||
@@ -136,7 +138,7 @@ The SSH user needs read access to:
|
|||||||
| `systemctl list-unit-files`, `systemctl show` | unprivileged |
|
| `systemctl list-unit-files`, `systemctl show` | unprivileged |
|
||||||
| `systemctl start/stop/restart/enable/disable` | `root`, or `sudo` (with `sudo_password`, or passwordless) |
|
| `systemctl start/stop/restart/enable/disable` | `root`, or `sudo` (with `sudo_password`, or passwordless) |
|
||||||
| `apt list --upgradable` | may require `apt-get update` (root) |
|
| `apt list --upgradable` | may require `apt-get update` (root) |
|
||||||
| `dnf check-update` / `yum check-update` | unprivileged, but slower without cache |
|
| `dnf --cacheonly check-update` / `yum --cacheonly check-update` | `root` or `sudo`; reads the cache that `dnf makecache` (root) fills |
|
||||||
|
|
||||||
For full functionality it is recommended to run as `root` or grant passwordless `sudo` for
|
For full functionality it is recommended to run as `root` or grant passwordless `sudo` for
|
||||||
the above commands.
|
the above commands.
|
||||||
@@ -146,6 +148,14 @@ shell: the password reaches `sudo -S` on stdin, never a command line, so it show
|
|||||||
the process list nor in a shell history. The interactive session switches its history off
|
the process list nor in a shell history. The interactive session switches its history off
|
||||||
before its first command.
|
before its first command.
|
||||||
|
|
||||||
|
Versions before that typed `echo '<password>' | sudo -S -p "" <command>` into the interactive
|
||||||
|
shell, so the login user's history kept the password once per command.
|
||||||
|
`get_history_sudo_lines()` counts those lines per history file without reading them, and
|
||||||
|
`remove_history_sudo_lines()` deletes exactly them and gives `/etc/snmp/snmpd.conf` and
|
||||||
|
`/etc/apt/apt.conf.d/00proxy` back to root where they were left to the login user
|
||||||
|
(napalm-device-types' `SudoHistoryMixin`). Rotate the sudo password afterwards: backups,
|
||||||
|
snapshots and audit logs may still hold it.
|
||||||
|
|
||||||
`get_services()` and `manage_service()` come from napalm-device-types'
|
`get_services()` and `manage_service()` come from napalm-device-types'
|
||||||
`SystemdServicesMixin`; this driver supplies only the transport. An action runs as
|
`SystemdServicesMixin`; this driver supplies only the transport. An action runs as
|
||||||
`timeout 45 systemctl --no-ask-password <action> -- <unit>.service`, so a unit that hangs
|
`timeout 45 systemctl --no-ask-password <action> -- <unit>.service`, so a unit that hangs
|
||||||
|
|||||||
@@ -0,0 +1,73 @@
|
|||||||
|
"""The packages dpkg has on a Debian-family host: the query and its parse."""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from napalm_device_types.models import PackageDict
|
||||||
|
|
||||||
|
#: One line per package dpkg knows, its state first (``ii``, ``rc``, ``hi``, …):
|
||||||
|
#: dpkg also lists removed packages whose configuration it still keeps (#17).
|
||||||
|
DPKG_QUERY_COMMAND = (
|
||||||
|
"dpkg-query -W -f='${db:Status-Abbrev}\\t${Package}\\t${Version}\\t${Installed-Size}"
|
||||||
|
"\\t${source:Package}\\t${source:Version}\\t${binary:Summary}\\n' 2>/dev/null"
|
||||||
|
)
|
||||||
|
#: The second letter of ``db:Status-Abbrev`` for a package whose files are gone:
|
||||||
|
#: ``n`` not installed, ``c`` only its configuration left (``rc``). Every other
|
||||||
|
#: state -- half-installed, unpacked, half-configured, awaiting triggers -- has
|
||||||
|
#: its files on disk.
|
||||||
|
_NOT_ON_DISK = frozenset("nc")
|
||||||
|
|
||||||
|
|
||||||
|
class DpkgPackageDict(PackageDict):
|
||||||
|
"""A dpkg package with the source package it was built from."""
|
||||||
|
|
||||||
|
source_package: str
|
||||||
|
source_version: str
|
||||||
|
|
||||||
|
|
||||||
|
def parse_dpkg_query(out: str) -> list[PackageDict]:
|
||||||
|
"""The packages in ``DPKG_QUERY_COMMAND``'s output whose files are on disk."""
|
||||||
|
packages: list[PackageDict] = []
|
||||||
|
for line in out.splitlines():
|
||||||
|
# Summary stays last and keeps whatever it contains: maxsplit must
|
||||||
|
# equal the number of tabs the format writes, not the field count.
|
||||||
|
status, _, line = line.partition("\t")
|
||||||
|
if status[1:2] in _NOT_ON_DISK:
|
||||||
|
continue
|
||||||
|
package = _package(line.split("\t", 5))
|
||||||
|
if package is not None:
|
||||||
|
packages.append(package)
|
||||||
|
return packages
|
||||||
|
|
||||||
|
|
||||||
|
def _package(parts: list[str]) -> DpkgPackageDict | None:
|
||||||
|
if len(parts) < 2:
|
||||||
|
return None
|
||||||
|
name = parts[0].strip()
|
||||||
|
version = parts[1].strip()
|
||||||
|
size = int(parts[2].strip()) * 1024 if len(parts) > 2 and parts[2].strip().isdigit() else 0
|
||||||
|
# Debian source package (e.g. openssh-server → openssh) for OSV matching.
|
||||||
|
source_package = parts[3].strip() if len(parts) > 3 and parts[3].strip() else name
|
||||||
|
# And its version, which is a different number from this package's.
|
||||||
|
#
|
||||||
|
# OSV states Debian ranges in *source* versions. A source package
|
||||||
|
# that ships several binaries gives each its own upstream version:
|
||||||
|
# libldb2 is 2:2.11.0+samba4.22.11+dfsg-… while its source, samba,
|
||||||
|
# is 2:4.22.11+dfsg-…. A consumer matching on source_package and
|
||||||
|
# comparing `version` compares two unrelated numbers — dpkg reads
|
||||||
|
# ldb's 2.11.0 as older than the 2:4.17.4+dfsg-1 that fixed
|
||||||
|
# CVE-2022-44640, and a host five releases past the fix was reported
|
||||||
|
# vulnerable on four packages at once.
|
||||||
|
#
|
||||||
|
# dpkg leaves this empty when it equals `Version`; so does an older
|
||||||
|
# dpkg that does not know the field at all.
|
||||||
|
source_version = parts[4].strip() if len(parts) > 4 and parts[4].strip() else version
|
||||||
|
return {
|
||||||
|
"name": name,
|
||||||
|
"version": version,
|
||||||
|
"installed": True,
|
||||||
|
"description": parts[5].strip() if len(parts) > 5 else "",
|
||||||
|
"size": size,
|
||||||
|
"source": "apt",
|
||||||
|
"source_package": source_package,
|
||||||
|
"source_version": source_version,
|
||||||
|
}
|
||||||
@@ -0,0 +1,145 @@
|
|||||||
|
"""Renaming the host: static and runtime hostname, ``/etc/hosts``, cloud-init (#45)."""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import re
|
||||||
|
from shlex import quote as _shlex_quote
|
||||||
|
from typing import TYPE_CHECKING
|
||||||
|
|
||||||
|
if TYPE_CHECKING:
|
||||||
|
from napalm_device_types import CommandResult
|
||||||
|
from napalm_device_types.models import FixStepOutcomeDict
|
||||||
|
|
||||||
|
#: One label of a hostname (RFC 1123): letters, digits and inner hyphens. It
|
||||||
|
#: also keeps every name ``set_hostname`` accepts safe in a root shell.
|
||||||
|
_LABEL_RE = re.compile(r"^(?!-)[A-Za-z0-9-]{1,63}(?<!-)$")
|
||||||
|
#: Names ``/etc/hosts`` keeps whatever the host was called.
|
||||||
|
_LOOPBACK_NAMES = frozenset({"localhost", "localhost.localdomain"})
|
||||||
|
#: What the host is called before the rename: its short name and its FQDN.
|
||||||
|
_OLD_NAME_COMMANDS = ("hostname", "hostname -f 2>/dev/null")
|
||||||
|
#: Without this, cloud-init's ``update_hostname`` puts the image's name back on
|
||||||
|
#: the next boot.
|
||||||
|
_CLOUD_INIT_PRESERVE = "/etc/cloud/cloud.cfg.d/99-preserve-hostname.cfg"
|
||||||
|
_CLOUD_INIT_PRESERVE_CONTENT = (
|
||||||
|
"# The hostname was set on purpose: keep it across reboots.\npreserve_hostname: true\n"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def _split_hostname(name: str) -> tuple[str, str | None]:
|
||||||
|
"""``(short name, FQDN or None)`` of *name*.
|
||||||
|
|
||||||
|
:raises ValueError: when *name* is not a valid hostname or FQDN.
|
||||||
|
"""
|
||||||
|
labels = name.split(".")
|
||||||
|
if len(name) > 253 or not all(_LABEL_RE.match(label) for label in labels):
|
||||||
|
raise ValueError(f"not a valid hostname: {name!r}")
|
||||||
|
return labels[0], (name if len(labels) > 1 else None)
|
||||||
|
|
||||||
|
|
||||||
|
def _renamed_line(line: str, targets: set[str], new: list[str]) -> str | None:
|
||||||
|
"""*line* of ``/etc/hosts`` with *new* where the first of *targets* stood.
|
||||||
|
|
||||||
|
The other names on it stay; ``None`` when the line names none of *targets*.
|
||||||
|
"""
|
||||||
|
entry, hash_, comment = line.partition("#")
|
||||||
|
address, *names = entry.split() or [""]
|
||||||
|
if not any(n.lower() in targets for n in names):
|
||||||
|
return None
|
||||||
|
result: list[str] = []
|
||||||
|
inserted = False
|
||||||
|
for name in names:
|
||||||
|
if name.lower() not in targets:
|
||||||
|
result.append(name)
|
||||||
|
elif not inserted:
|
||||||
|
result += new
|
||||||
|
inserted = True
|
||||||
|
tail = f" {hash_}{comment}" if hash_ else ""
|
||||||
|
return f"{address}\t{' '.join(dict.fromkeys(result))}{tail}"
|
||||||
|
|
||||||
|
|
||||||
|
def _rename_in_hosts(content: str, old_names: set[str], short: str, fqdn: str | None) -> str:
|
||||||
|
"""*content* of ``/etc/hosts`` with the host's old names replaced by the new.
|
||||||
|
|
||||||
|
Every line naming the host gets the new names. When no line names it, the
|
||||||
|
Debian ``127.0.1.1`` line takes them; a file with neither is returned
|
||||||
|
unchanged -- nothing is appended to a host that never listed itself.
|
||||||
|
"""
|
||||||
|
old = {n.lower() for n in old_names} - _LOOPBACK_NAMES
|
||||||
|
new = [fqdn, short] if fqdn else [short]
|
||||||
|
lines = content.split("\n")
|
||||||
|
renamed = [_renamed_line(line, old, new) for line in lines]
|
||||||
|
if all(r is None for r in renamed):
|
||||||
|
renamed = _renamed_self_line(lines, new)
|
||||||
|
return "\n".join(r or line for line, r in zip(lines, renamed, strict=True))
|
||||||
|
|
||||||
|
|
||||||
|
def _renamed_self_line(lines: list[str], new: list[str]) -> list[str | None]:
|
||||||
|
"""The first ``127.0.1.1`` line of *lines* naming *new* instead, the rest ``None``."""
|
||||||
|
result: list[str | None] = [None] * len(lines)
|
||||||
|
for i, line in enumerate(lines):
|
||||||
|
address, *names = line.partition("#")[0].split() or [""]
|
||||||
|
if address == "127.0.1.1" and names:
|
||||||
|
result[i] = _renamed_line(line, {n.lower() for n in names}, new)
|
||||||
|
break
|
||||||
|
return result
|
||||||
|
|
||||||
|
|
||||||
|
class HostnameMixin:
|
||||||
|
"""``set_hostname`` for a Linux host, over the driver's command channel."""
|
||||||
|
|
||||||
|
if TYPE_CHECKING:
|
||||||
|
|
||||||
|
def run_command(
|
||||||
|
self,
|
||||||
|
command: str,
|
||||||
|
*,
|
||||||
|
privileged: bool = False,
|
||||||
|
timeout: float = 60,
|
||||||
|
stdin: bytes | None = None,
|
||||||
|
) -> CommandResult: ...
|
||||||
|
|
||||||
|
def _install_file_as_root(self, path: str, content: str) -> CommandResult: ...
|
||||||
|
|
||||||
|
def _root_outcome(self, command: str, timeout: float, done: str) -> FixStepOutcomeDict: ...
|
||||||
|
|
||||||
|
def set_hostname(self, new_hostname: str) -> None:
|
||||||
|
"""Rename the host: static and runtime hostname, ``/etc/hosts``, cloud-init.
|
||||||
|
|
||||||
|
Takes a bare name or an FQDN; the hostname itself is the short name, and
|
||||||
|
an FQDN goes to ``/etc/hosts`` in front of it. ``hostnamectl`` sets it
|
||||||
|
where there is one, otherwise ``/etc/hostname`` and ``hostname(1)``.
|
||||||
|
Files are installed from a staged copy (``_install_file_as_root``).
|
||||||
|
|
||||||
|
:raises ValueError: for a name that is not a valid hostname, before
|
||||||
|
anything runs on the host.
|
||||||
|
:raises RuntimeError: when the host refuses a step; the steps before it
|
||||||
|
stay done.
|
||||||
|
"""
|
||||||
|
short, fqdn = _split_hostname(new_hostname)
|
||||||
|
old_names = {self.run_command(c, timeout=30).stdout.strip() for c in _OLD_NAME_COMMANDS}
|
||||||
|
hosts = self.run_command("cat /etc/hosts", timeout=30)
|
||||||
|
|
||||||
|
name = _shlex_quote(short)
|
||||||
|
outcome = self._root_outcome(
|
||||||
|
f"if command -v hostnamectl >/dev/null 2>&1 && hostnamectl set-hostname {name} "
|
||||||
|
f"2>/dev/null; then :; else printf '%s\\n' {name} > /etc/hostname "
|
||||||
|
f"&& hostname {name}; fi",
|
||||||
|
30,
|
||||||
|
"",
|
||||||
|
)
|
||||||
|
if not outcome["ok"]:
|
||||||
|
raise RuntimeError(outcome["output"] or "the host refused the new hostname")
|
||||||
|
|
||||||
|
if hosts.exit_code == 0:
|
||||||
|
renamed = _rename_in_hosts(hosts.stdout, old_names - {""}, short, fqdn)
|
||||||
|
if renamed != hosts.stdout:
|
||||||
|
self._install_or_raise("/etc/hosts", renamed)
|
||||||
|
if self.run_command("test -d /etc/cloud/cloud.cfg.d", timeout=30).exit_code == 0:
|
||||||
|
self._install_or_raise(_CLOUD_INIT_PRESERVE, _CLOUD_INIT_PRESERVE_CONTENT)
|
||||||
|
|
||||||
|
def _install_or_raise(self, path: str, content: str) -> None:
|
||||||
|
"""``_install_file_as_root``, raising its output when it failed."""
|
||||||
|
result = self._install_file_as_root(path, content)
|
||||||
|
if result.exit_code != 0:
|
||||||
|
reason = (result.stderr or result.stdout).strip() or f"status {result.exit_code}"
|
||||||
|
raise RuntimeError(f"hostname set, but {path} could not be written: {reason}")
|
||||||
@@ -0,0 +1,68 @@
|
|||||||
|
"""Personal accounts on a Linux host, with shadow-utils (NetOrk/netork#1793).
|
||||||
|
|
||||||
|
napalm-device-types' ``LocalAccountsMixin`` runs the steps; this names the
|
||||||
|
commands. A locked account is an expired one (``usermod -L -e 1``): PAM's
|
||||||
|
account check refuses it every login, a key's too, where a ``!`` in front of
|
||||||
|
the password -- ``usermod -L`` alone -- stops only the password. Opening it
|
||||||
|
again lifts the expiry and gives the old password back.
|
||||||
|
"""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import shlex
|
||||||
|
import time
|
||||||
|
|
||||||
|
from napalm_device_types import LocalAccountsMixin
|
||||||
|
|
||||||
|
#: What the password field holds when the account never had a password.
|
||||||
|
_NO_PASSWORD = frozenset({"", "!", "!!", "!*", "*"})
|
||||||
|
|
||||||
|
|
||||||
|
class LinuxLocalAccountsMixin(LocalAccountsMixin):
|
||||||
|
ACCOUNT_SHELL = "/bin/bash"
|
||||||
|
#: Debian and Ubuntu grant ``%sudo``, the Red Hat family ``%wheel``, old Ubuntu ``%admin``.
|
||||||
|
ACCOUNT_ADMIN_GROUPS = ("sudo", "wheel", "admin")
|
||||||
|
SUDOERS_DIR = "/etc/sudoers.d"
|
||||||
|
ROOT_GROUP = "root"
|
||||||
|
|
||||||
|
def _account_create(self, name: str, password_hash: str | None) -> list[list[str]]:
|
||||||
|
password = ["-p", password_hash] if password_hash else []
|
||||||
|
return [["useradd", "-m", "-s", self.ACCOUNT_SHELL, *password, name]]
|
||||||
|
|
||||||
|
def _account_create_with_uid(self, name: str, uid: int) -> list[list[str]]:
|
||||||
|
# Below UID_MIN useradd warns and goes on: netOrk's own account is a system's.
|
||||||
|
return [["useradd", "-m", "-u", str(uid), "-s", self.ACCOUNT_SHELL, name]]
|
||||||
|
|
||||||
|
def _account_open(
|
||||||
|
self, name: str, password_hash: str | None, *, locked: bool
|
||||||
|
) -> list[list[str]]:
|
||||||
|
if password_hash:
|
||||||
|
return [["usermod", "-e", "", "-p", password_hash, name]]
|
||||||
|
# usermod -U refuses to leave an account without any password.
|
||||||
|
had_one = self._shadow(name)[1].lstrip("!") not in _NO_PASSWORD
|
||||||
|
return [["usermod", *(["-U"] if had_one else []), "-e", "", name]]
|
||||||
|
|
||||||
|
def _account_lock(self, name: str, *, locked: bool) -> list[list[str]]:
|
||||||
|
return [] if locked else [["usermod", "-L", "-e", "1", name]]
|
||||||
|
|
||||||
|
def _account_locked(self, name: str, passwd: list[str]) -> bool:
|
||||||
|
expire = self._shadow(name)[7]
|
||||||
|
# pam_unix: expired from the day sp_expire names on.
|
||||||
|
return bool(expire) and int(expire) <= int(time.time() // 86400)
|
||||||
|
|
||||||
|
def _account_leave_group(self, name: str, group: str, groups: list[str]) -> list[str]:
|
||||||
|
return ["gpasswd", "-d", name, group]
|
||||||
|
|
||||||
|
def _shadow(self, name: str) -> list[str]:
|
||||||
|
"""*name*'s shadow entry, split; only the lock and the expiry are looked at.
|
||||||
|
|
||||||
|
:raises RuntimeError: when it cannot be read.
|
||||||
|
"""
|
||||||
|
result = self.run_command(
|
||||||
|
shlex.join(["getent", "shadow", name]), privileged=True, timeout=30
|
||||||
|
)
|
||||||
|
fields = result.stdout.strip().split(":")
|
||||||
|
if result.exit_code != 0 or len(fields) < 9 or fields[0] != name:
|
||||||
|
reason = (result.stderr or result.stdout).strip() or f"exit {result.exit_code}"
|
||||||
|
raise RuntimeError(f"cannot read the shadow entry of {name}: {reason}")
|
||||||
|
return fields
|
||||||
@@ -0,0 +1,65 @@
|
|||||||
|
"""NAPALM's ``ping()`` reply from what ``ping(8)`` prints: iputils and BusyBox, IPv4 and IPv6."""
|
||||||
|
|
||||||
|
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``;
|
||||||
|
#: 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 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, and for a
|
||||||
|
destination or source that ping would read as an option (#63): no address or
|
||||||
|
hostname starts with ``-``.
|
||||||
|
"""
|
||||||
|
for name in (destination, source):
|
||||||
|
if name.startswith("-"):
|
||||||
|
raise ValueError(f"{name!r} would be read as a ping option, not an address")
|
||||||
|
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)
|
||||||
|
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,
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,106 @@
|
|||||||
|
"""Routes from ``ip route show``, one address family at a time."""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import re
|
||||||
|
from collections.abc import Iterator
|
||||||
|
from typing import NamedTuple
|
||||||
|
|
||||||
|
#: The command per family. ``ip -6 route show`` names its default route ``default``
|
||||||
|
#: as well, so only the command a line came from says which family it is (#6).
|
||||||
|
IP_ROUTE_COMMANDS = {"ipv4": "ip -4 route show", "ipv6": "ip -6 route show"}
|
||||||
|
_DEFAULT = {"ipv4": "0.0.0.0/0", "ipv6": "::/0"}
|
||||||
|
_HOST_PREFIX = {"ipv4": "/32", "ipv6": "/128"}
|
||||||
|
|
||||||
|
#: The route types ``ip route`` prints in front of the destination; a unicast route
|
||||||
|
#: carries none (#57).
|
||||||
|
_ROUTE_TYPES = frozenset(
|
||||||
|
{
|
||||||
|
"unicast",
|
||||||
|
"local",
|
||||||
|
"broadcast",
|
||||||
|
"anycast",
|
||||||
|
"multicast",
|
||||||
|
"blackhole",
|
||||||
|
"unreachable",
|
||||||
|
"prohibit",
|
||||||
|
"throw",
|
||||||
|
"nat",
|
||||||
|
"xresolve",
|
||||||
|
}
|
||||||
|
)
|
||||||
|
#: Route types that send nothing out of an interface: no next hop, and no outgoing
|
||||||
|
#: interface either, though IPv6 names ``lo`` for them.
|
||||||
|
_NO_FORWARDING = frozenset({"blackhole", "unreachable", "prohibit", "throw"})
|
||||||
|
#: The line a multipath route's next hops start with, one each, after the route.
|
||||||
|
_NEXTHOP = "nexthop"
|
||||||
|
|
||||||
|
#: ``via 192.0.2.1``, or with the gateway's family in front: ``via inet6 fe80::1`` for an
|
||||||
|
#: IPv4 route with an IPv6 next hop (RFC 8950, #65).
|
||||||
|
_VIA = re.compile(r"via\s+(?:inet6?\s+)?(\S+)")
|
||||||
|
_DEV = re.compile(r"dev\s+(\S+)")
|
||||||
|
_PROTO = re.compile(r"proto\s+(\S+)")
|
||||||
|
_METRIC = re.compile(r"metric\s+(\d+)")
|
||||||
|
|
||||||
|
|
||||||
|
class IpRoute(NamedTuple):
|
||||||
|
"""One route of ``ip route show``; a multipath route is one per next hop."""
|
||||||
|
|
||||||
|
network: str
|
||||||
|
proto: str
|
||||||
|
nexthop: str
|
||||||
|
iface: str
|
||||||
|
metric: int
|
||||||
|
route_type: str = "unicast"
|
||||||
|
|
||||||
|
@property
|
||||||
|
def attributes(self) -> dict[str, str]:
|
||||||
|
"""NAPALM's ``protocol_attributes`` for this route: its type, unless unicast."""
|
||||||
|
return {} if self.route_type == "unicast" else {"type": self.route_type}
|
||||||
|
|
||||||
|
|
||||||
|
def parse_ip_route(out: str, family: str) -> Iterator[IpRoute]:
|
||||||
|
"""The routes in *out*, the output of ``IP_ROUTE_COMMANDS[family]``."""
|
||||||
|
route = ""
|
||||||
|
hops: list[str] = []
|
||||||
|
for line in out.splitlines():
|
||||||
|
line = line.strip()
|
||||||
|
if not line or line.startswith("#"):
|
||||||
|
continue
|
||||||
|
if line.split(None, 1)[0] == _NEXTHOP:
|
||||||
|
hops.append(line)
|
||||||
|
continue
|
||||||
|
if route:
|
||||||
|
yield from _routes(route, hops, family)
|
||||||
|
route, hops = line, []
|
||||||
|
if route:
|
||||||
|
yield from _routes(route, hops, family)
|
||||||
|
|
||||||
|
|
||||||
|
def with_length(prefix: str) -> str:
|
||||||
|
"""*prefix* with a prefix length: a host route without one is ``/32`` or ``/128``
|
||||||
|
by the address's family (#64)."""
|
||||||
|
if "/" in prefix:
|
||||||
|
return prefix
|
||||||
|
return prefix + _HOST_PREFIX["ipv6" if ":" in prefix else "ipv4"]
|
||||||
|
|
||||||
|
|
||||||
|
def _routes(line: str, hops: list[str], family: str) -> Iterator[IpRoute]:
|
||||||
|
"""The route *line* with its multipath next-hop lines *hops*: one per next hop."""
|
||||||
|
words = line.split()
|
||||||
|
route_type = words.pop(0) if words[0] in _ROUTE_TYPES and len(words) > 1 else "unicast"
|
||||||
|
network = _DEFAULT[family] if words[0] == "default" else words[0]
|
||||||
|
if "/" not in network:
|
||||||
|
network += _HOST_PREFIX[family]
|
||||||
|
proto = _first(_PROTO, line) or "kernel"
|
||||||
|
metric = int(_first(_METRIC, line) or 0)
|
||||||
|
if route_type in _NO_FORWARDING:
|
||||||
|
yield IpRoute(network, proto, "", "", metric, route_type)
|
||||||
|
return
|
||||||
|
for hop in hops or [line]:
|
||||||
|
yield IpRoute(network, proto, _first(_VIA, hop), _first(_DEV, hop), metric, route_type)
|
||||||
|
|
||||||
|
|
||||||
|
def _first(pattern: re.Pattern[str], line: str) -> str:
|
||||||
|
match = pattern.search(line)
|
||||||
|
return match.group(1) if match else ""
|
||||||
@@ -0,0 +1,97 @@
|
|||||||
|
"""The interactive shell netmiko opens, made fit to be read before the first command."""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import logging
|
||||||
|
import re
|
||||||
|
from shlex import quote
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
#: The end of a command's output when nothing better is known: a line that looks
|
||||||
|
#: like a shell prompt.
|
||||||
|
PROMPT_RE = r"[#$\>]\s*$"
|
||||||
|
|
||||||
|
#: A command ending in ``echo __NAME=$?`` reports its exit status on a line of its
|
||||||
|
#: own: the driver's ``_RC_MARKER``, ``__APT_RC=`` and ``__SVC_RC=`` in
|
||||||
|
#: 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.
|
||||||
|
|
||||||
|
netmiko stops reading as soon as the pattern matches what it has read so far.
|
||||||
|
A line the command prints can end in ``#``, ``$`` or ``>`` -- apt's
|
||||||
|
``<ftpmaster@ubuntu.com>`` after a bad signature -- and was taken for the
|
||||||
|
prompt: half the output came back, and the rest started the next command's
|
||||||
|
(#615). A command that echoes its exit status is read until that marker, with
|
||||||
|
a number, and the prompt line after it. The echoed command line carries a
|
||||||
|
literal ``$?`` and cannot match.
|
||||||
|
"""
|
||||||
|
markers: list[str] = STATUS_ECHO_RE.findall(command)
|
||||||
|
if not markers:
|
||||||
|
return PROMPT_RE
|
||||||
|
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``;
|
||||||
|
#: a shell without ``set -o history`` (dash) only complains, to /dev/null.
|
||||||
|
HISTORY_OFF = " unset HISTFILE; set +o history 2>/dev/null; true"
|
||||||
|
|
||||||
|
#: Sent right after ``HISTORY_OFF`` (#41): systemd 258's
|
||||||
|
#: ``/etc/profile.d/80-systemd-osc-context.sh`` wraps every command in OSC 3008
|
||||||
|
#: sequences from ``PS0`` and ``PROMPT_COMMAND``, each with a fresh UUID. It appends
|
||||||
|
#: to ``PROMPT_COMMAND`` as an array, so only ``unset`` removes its hook.
|
||||||
|
PROMPT_HOOKS_OFF = " unset PS0 PROMPT_COMMAND; true"
|
||||||
|
|
||||||
|
#: Keeps the PTY from wrapping long output lines (e.g. docker JSON).
|
||||||
|
NO_LINE_WRAP = "stty cols 10000 2>/dev/null || true"
|
||||||
|
|
||||||
|
|
||||||
|
def prepare_shell(device: Any, hostname: str) -> None:
|
||||||
|
"""Make netmiko's fresh shell *device* fit to read, one step at a time.
|
||||||
|
|
||||||
|
A step the shell refuses is logged and skipped: none of them is worth
|
||||||
|
failing the connection for.
|
||||||
|
"""
|
||||||
|
# No history before anything else is typed (NetOrk/netork#1039).
|
||||||
|
try:
|
||||||
|
device.send_command(HISTORY_OFF, expect_string=PROMPT_RE)
|
||||||
|
except Exception:
|
||||||
|
logger.debug("Could not switch the shell history off on %s", hostname, exc_info=True)
|
||||||
|
|
||||||
|
# No systemd OSC 3008 context around every command (#41). netmiko learned
|
||||||
|
# the prompt with those sequences in front, and a prompt carrying a fresh
|
||||||
|
# UUID never matches again, so it reads the prompt anew once they are gone.
|
||||||
|
try:
|
||||||
|
device.send_command(PROMPT_HOOKS_OFF, expect_string=PROMPT_RE)
|
||||||
|
device.set_base_prompt()
|
||||||
|
except Exception:
|
||||||
|
logger.debug("Could not switch the prompt hooks off on %s", hostname, exc_info=True)
|
||||||
|
|
||||||
|
try:
|
||||||
|
device.send_command(NO_LINE_WRAP, expect_string=PROMPT_RE)
|
||||||
|
except Exception:
|
||||||
|
logger.debug("%s: ignored", hostname, exc_info=True)
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
"""What the old ``_sudo`` left on every host it reached (NetOrk/netork#1090).
|
||||||
|
|
||||||
|
Until NetOrk/netork#1039, ``_sudo`` typed ``echo '<password>' | sudo -S -p ""
|
||||||
|
<command>`` into netmiko's interactive shell, and only the part before the first
|
||||||
|
``&&`` ran as root. napalm-device-types' ``SudoHistoryMixin`` counts and deletes
|
||||||
|
those lines and gives the files back to root; this says what to look for.
|
||||||
|
"""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from napalm_device_types import AptProxyMixin, SudoHistoryMixin
|
||||||
|
|
||||||
|
|
||||||
|
class LinuxSudoHistoryMixin(SudoHistoryMixin):
|
||||||
|
#: Every line the old ``_sudo`` typed carries it, with the sudo password before it.
|
||||||
|
SUDO_HISTORY_MARKER = "| sudo -S -p "
|
||||||
|
#: Written as the login user where the part that should have been root's came
|
||||||
|
#: after the first ``&&``: snmpd.conf (``configure_snmp_agent``) and apt's proxy.
|
||||||
|
SUDO_ROOT_FILES = ("/etc/snmp/snmpd.conf", AptProxyMixin.APT_PROXY_FILE)
|
||||||
+110
-369
@@ -28,6 +28,7 @@ from napalm.base.netmiko_helpers import netmiko_args
|
|||||||
from napalm_device_types import (
|
from napalm_device_types import (
|
||||||
APT_UPGRADABLE_COMMAND,
|
APT_UPGRADABLE_COMMAND,
|
||||||
DNF_SECURITY_COMMAND,
|
DNF_SECURITY_COMMAND,
|
||||||
|
LLDPCTL_KEYVALUE_COMMAND,
|
||||||
AptProxyMixin,
|
AptProxyMixin,
|
||||||
ByteStream,
|
ByteStream,
|
||||||
CommandResult,
|
CommandResult,
|
||||||
@@ -38,8 +39,10 @@ from napalm_device_types import (
|
|||||||
HostStatusMixin,
|
HostStatusMixin,
|
||||||
KernelFactsMixin,
|
KernelFactsMixin,
|
||||||
ListeningSocketsMixin,
|
ListeningSocketsMixin,
|
||||||
|
LldpNeighborDict,
|
||||||
OpenSshServerMixin,
|
OpenSshServerMixin,
|
||||||
OSDriver,
|
OSDriver,
|
||||||
|
PosixScheduledJobsMixin,
|
||||||
ServiceClustersMixin,
|
ServiceClustersMixin,
|
||||||
ServicePackagesMixin,
|
ServicePackagesMixin,
|
||||||
SnmpAgentMixin,
|
SnmpAgentMixin,
|
||||||
@@ -47,16 +50,17 @@ from napalm_device_types import (
|
|||||||
SystemdUnavailable,
|
SystemdUnavailable,
|
||||||
SystemIdentityMixin,
|
SystemIdentityMixin,
|
||||||
VirtualIpsMixin,
|
VirtualIpsMixin,
|
||||||
|
lldp_neighbors,
|
||||||
open_stream_on_transport,
|
open_stream_on_transport,
|
||||||
parse_apt_upgradable,
|
parse_apt_upgradable,
|
||||||
parse_dnf_security,
|
parse_dnf_security,
|
||||||
|
parse_lldpctl_keyvalue,
|
||||||
run_fix_steps,
|
run_fix_steps,
|
||||||
run_on_transport,
|
run_on_transport,
|
||||||
strip_terminal_codes,
|
strip_terminal_codes,
|
||||||
)
|
)
|
||||||
from napalm_device_types.models import (
|
from napalm_device_types.models import (
|
||||||
ApplyUpdatesResultDict,
|
ApplyUpdatesResultDict,
|
||||||
CronJobDict,
|
|
||||||
FixResultDict,
|
FixResultDict,
|
||||||
FixStepOutcomeDict,
|
FixStepOutcomeDict,
|
||||||
PackageDict,
|
PackageDict,
|
||||||
@@ -67,12 +71,17 @@ from napalm_device_types.models import (
|
|||||||
UpdateDict,
|
UpdateDict,
|
||||||
UserDict,
|
UserDict,
|
||||||
)
|
)
|
||||||
from napalm_device_types.ssh_host_keys import HostKeyCheck
|
from napalm_device_types.ssh_host_keys import HostKeyCheck, retry_refused
|
||||||
from netmiko import ConnectHandler
|
from netmiko import ConnectHandler
|
||||||
from netmiko.exceptions import (
|
from netmiko.exceptions import NetmikoAuthenticationException, NetmikoTimeoutException
|
||||||
NetmikoAuthenticationException,
|
|
||||||
NetmikoTimeoutException,
|
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, ping_command
|
||||||
|
from napalm_linux._routes import IP_ROUTE_COMMANDS, IpRoute, parse_ip_route, with_length
|
||||||
|
from napalm_linux._shell import expect_for, prepare_shell, sudo_without_password
|
||||||
|
from napalm_linux._sudo_history import LinuxSudoHistoryMixin
|
||||||
|
|
||||||
logger = logging.getLogger("napalm_linux")
|
logger = logging.getLogger("napalm_linux")
|
||||||
|
|
||||||
@@ -86,31 +95,6 @@ _RC_MARKER = "__NETORK_RC="
|
|||||||
_RC_MARKER_RE = re.compile(rf"^{_RC_MARKER}(\d+)\s*$", re.MULTILINE)
|
_RC_MARKER_RE = re.compile(rf"^{_RC_MARKER}(\d+)\s*$", re.MULTILINE)
|
||||||
|
|
||||||
|
|
||||||
#: The end of a command's output when nothing better is known: a line that looks
|
|
||||||
#: like a shell prompt.
|
|
||||||
_PROMPT_RE = r"[#$\>]\s*$"
|
|
||||||
#: A command ending in ``echo __NAME=$?`` reports its exit status on a line of its
|
|
||||||
#: own: ``_RC_MARKER`` here, ``__APT_RC=`` and ``__SVC_RC=`` in napalm-device-types.
|
|
||||||
_STATUS_ECHO_RE = re.compile(r"echo\s+(__[A-Z_]+=)\$\?")
|
|
||||||
|
|
||||||
|
|
||||||
def _expect_for(command: str) -> str:
|
|
||||||
"""The pattern that ends *command*'s output.
|
|
||||||
|
|
||||||
netmiko stops reading as soon as the pattern matches what it has read so far.
|
|
||||||
A line the command prints can end in ``#``, ``$`` or ``>`` -- apt's
|
|
||||||
``<ftpmaster@ubuntu.com>`` after a bad signature -- and was taken for the
|
|
||||||
prompt: half the output came back, and the rest started the next command's
|
|
||||||
(#615). A command that echoes its exit status is read until that marker, with
|
|
||||||
a number, and the prompt line after it. The echoed command line carries a
|
|
||||||
literal ``$?`` and cannot match.
|
|
||||||
"""
|
|
||||||
markers = _STATUS_ECHO_RE.findall(command)
|
|
||||||
if not markers:
|
|
||||||
return _PROMPT_RE
|
|
||||||
return re.escape(markers[-1]) + r"\d+\s*\n.*" + _PROMPT_RE
|
|
||||||
|
|
||||||
|
|
||||||
def _split_status(raw: str) -> tuple[str, int | None]:
|
def _split_status(raw: str) -> tuple[str, int | None]:
|
||||||
"""``(output, exit_status)`` of a command followed by ``echo {_RC_MARKER}$?``.
|
"""``(output, exit_status)`` of a command followed by ``echo {_RC_MARKER}$?``.
|
||||||
|
|
||||||
@@ -137,9 +121,9 @@ _REFRESH = {
|
|||||||
}
|
}
|
||||||
#: yum's counterpart of DNF_SECURITY_COMMAND, in the same shape: parse_dnf_security
|
#: yum's counterpart of DNF_SECURITY_COMMAND, in the same shape: parse_dnf_security
|
||||||
#: (napalm-device-types 4.7) needs the ``__DNF_RC=`` line, and behind ``| cat`` it is
|
#: (napalm-device-types 4.7) needs the ``__DNF_RC=`` line, and behind ``| cat`` it is
|
||||||
#: the only place yum's own exit status survives.
|
#: the only place yum's own exit status survives. Cached metadata only, as there.
|
||||||
_YUM_SECURITY_COMMAND = (
|
_YUM_SECURITY_COMMAND = (
|
||||||
"{ LC_ALL=C yum updateinfo list security -q 2>/dev/null; echo __DNF_RC=$?; } | cat"
|
"{ LC_ALL=C yum --cacheonly updateinfo list security -q 2>/dev/null; echo __DNF_RC=$?; } | cat"
|
||||||
)
|
)
|
||||||
#: The exit status ``DNF_SECURITY_COMMAND`` and ``_YUM_SECURITY_COMMAND`` report.
|
#: The exit status ``DNF_SECURITY_COMMAND`` and ``_YUM_SECURITY_COMMAND`` report.
|
||||||
_DNF_RC_RE = re.compile(r"^__DNF_RC=(\d+)\s*$", re.MULTILINE)
|
_DNF_RC_RE = re.compile(r"^__DNF_RC=(\d+)\s*$", re.MULTILINE)
|
||||||
@@ -147,6 +131,11 @@ _DNF_RC_RE = re.compile(r"^__DNF_RC=(\d+)\s*$", re.MULTILINE)
|
|||||||
#: Restart the host two seconds later, detached from this session: the launcher's
|
#: Restart the host two seconds later, detached from this session: the launcher's
|
||||||
#: exit status comes back before the host goes down, and closing the session
|
#: exit status comes back before the host goes down, and closing the session
|
||||||
#: cannot take the restart with it.
|
#: cannot take the restart with it.
|
||||||
|
#: ``running`` when systemd has lldpd active or a process of that name runs, else ``stopped``.
|
||||||
|
_LLDPD_STATE_COMMAND = (
|
||||||
|
"if systemctl is-active --quiet lldpd 2>/dev/null || pgrep -x lldpd >/dev/null 2>&1; "
|
||||||
|
"then echo running; else echo stopped; fi"
|
||||||
|
)
|
||||||
_REBOOT_DETACHED = "sh -c '(trap \"\" HUP; sleep 2; /sbin/reboot) </dev/null >/dev/null 2>&1 &'"
|
_REBOOT_DETACHED = "sh -c '(trap \"\" HUP; sleep 2; /sbin/reboot) </dev/null >/dev/null 2>&1 &'"
|
||||||
|
|
||||||
#: Why ``run_command`` refuses stdin to a privileged command while a sudo
|
#: Why ``run_command`` refuses stdin to a privileged command while a sudo
|
||||||
@@ -159,12 +148,6 @@ _PRIVILEGED_STDIN = (
|
|||||||
"mktemp, cat) and have root install that file."
|
"mktemp, cat) and have root install that file."
|
||||||
)
|
)
|
||||||
|
|
||||||
#: 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``;
|
|
||||||
#: a shell without ``set -o history`` (dash) only complains, to /dev/null.
|
|
||||||
_HISTORY_OFF = " unset HISTFILE; set +o history 2>/dev/null; true"
|
|
||||||
|
|
||||||
#: First in every script ``_sudo`` runs: a sudo that does not prompt (NOPASSWD,
|
#: First in every script ``_sudo`` runs: a sudo that does not prompt (NOPASSWD,
|
||||||
#: a cached timestamp) leaves the password on stdin, and nothing may read it.
|
#: a cached timestamp) leaves the password on stdin, and nothing may read it.
|
||||||
_NO_STDIN = "exec </dev/null; "
|
_NO_STDIN = "exec </dev/null; "
|
||||||
@@ -313,6 +296,8 @@ def render_snmpd_conf(settings: SnmpAgentSettingsDict) -> str:
|
|||||||
|
|
||||||
|
|
||||||
class LinuxDriver(
|
class LinuxDriver(
|
||||||
|
LinuxSudoHistoryMixin,
|
||||||
|
LinuxLocalAccountsMixin,
|
||||||
SnmpAgentMixin,
|
SnmpAgentMixin,
|
||||||
EnsurePackageMixin,
|
EnsurePackageMixin,
|
||||||
AptProxyMixin,
|
AptProxyMixin,
|
||||||
@@ -325,8 +310,10 @@ class LinuxDriver(
|
|||||||
SystemIdentityMixin,
|
SystemIdentityMixin,
|
||||||
OpenSshServerMixin,
|
OpenSshServerMixin,
|
||||||
SystemdServicesMixin,
|
SystemdServicesMixin,
|
||||||
|
PosixScheduledJobsMixin,
|
||||||
HostStatusMixin,
|
HostStatusMixin,
|
||||||
ContainerEngineMixin,
|
ContainerEngineMixin,
|
||||||
|
HostnameMixin,
|
||||||
OSDriver,
|
OSDriver,
|
||||||
):
|
):
|
||||||
"""NAPALM driver for generic Linux systems.
|
"""NAPALM driver for generic Linux systems.
|
||||||
@@ -373,7 +360,6 @@ class LinuxDriver(
|
|||||||
self._secret: str = optional_args.get("secret", password)
|
self._secret: str = optional_args.get("secret", password)
|
||||||
# Optional sudo password for privilege escalation (e.g. apt-get update)
|
# Optional sudo password for privilege escalation (e.g. apt-get update)
|
||||||
self._sudo_password: str | None = optional_args.get("sudo_password")
|
self._sudo_password: str | None = optional_args.get("sudo_password")
|
||||||
# Expected apt proxy URL — checked as a device warning on apt systems.
|
|
||||||
|
|
||||||
if optional_args.get("debugging"):
|
if optional_args.get("debugging"):
|
||||||
logger.setLevel(logging.DEBUG)
|
logger.setLevel(logging.DEBUG)
|
||||||
@@ -397,37 +383,23 @@ class LinuxDriver(
|
|||||||
"""Open the SSH connection and detect the package manager."""
|
"""Open the SSH connection and detect the package manager."""
|
||||||
try:
|
try:
|
||||||
with self._host_key.netmiko(self.netmiko_optional_args) as netmiko_args_:
|
with self._host_key.netmiko(self.netmiko_optional_args) as netmiko_args_:
|
||||||
self._device = ConnectHandler(
|
self._device = retry_refused( # waits out sshd's penalty (NetOrk/netork#1800)
|
||||||
device_type=self.NETMIKO_DEVICE_TYPE,
|
lambda: ConnectHandler(
|
||||||
host=self.hostname,
|
device_type=self.NETMIKO_DEVICE_TYPE,
|
||||||
username=self.username,
|
host=self.hostname,
|
||||||
password=self.password,
|
username=self.username,
|
||||||
port=self.port,
|
password=self.password,
|
||||||
secret=self._secret,
|
port=self.port,
|
||||||
timeout=self.timeout,
|
secret=self._secret,
|
||||||
**netmiko_args_,
|
timeout=self.timeout,
|
||||||
|
**netmiko_args_,
|
||||||
|
)
|
||||||
)
|
)
|
||||||
except NetmikoAuthenticationException as exc:
|
except (NetmikoAuthenticationException, NetmikoTimeoutException) as exc:
|
||||||
raise ConnectionException(str(exc)) from exc
|
|
||||||
except NetmikoTimeoutException as exc:
|
|
||||||
raise ConnectionException(str(exc)) from exc
|
raise ConnectionException(str(exc)) from exc
|
||||||
self._host_key.observe(self._device.remote_conn_pre.get_transport())
|
self._host_key.observe(self._device.remote_conn_pre.get_transport())
|
||||||
|
|
||||||
# No history before anything else is typed (NetOrk/netork#1039).
|
prepare_shell(self._device, self.hostname)
|
||||||
try:
|
|
||||||
self._device.send_command(_HISTORY_OFF, expect_string=_PROMPT_RE)
|
|
||||||
except Exception:
|
|
||||||
logger.debug(
|
|
||||||
"Could not switch the shell history off on %s", self.hostname, exc_info=True
|
|
||||||
)
|
|
||||||
|
|
||||||
# Prevent PTY from wrapping long output lines (e.g. docker JSON).
|
|
||||||
try:
|
|
||||||
self._device.send_command(
|
|
||||||
"stty cols 10000 2>/dev/null || true", expect_string=r"[#$>]\s*$"
|
|
||||||
)
|
|
||||||
except Exception:
|
|
||||||
logger.debug("%s: ignored", self.hostname, exc_info=True)
|
|
||||||
|
|
||||||
self._pkg_manager = self._forced_pkg_manager or self._detect_pkg_manager()
|
self._pkg_manager = self._forced_pkg_manager or self._detect_pkg_manager()
|
||||||
logger.debug("Connected to %s, pkg_manager=%s", self.hostname, self._pkg_manager)
|
logger.debug("Connected to %s, pkg_manager=%s", self.hostname, self._pkg_manager)
|
||||||
@@ -455,15 +427,16 @@ class LinuxDriver(
|
|||||||
# ------------------------------------------------------------------
|
# ------------------------------------------------------------------
|
||||||
|
|
||||||
def _send(self, command: str, read_timeout: float = 100) -> str:
|
def _send(self, command: str, read_timeout: float = 100) -> str:
|
||||||
"""Send a command and return stripped output."""
|
"""Send a command and return its output without terminal codes, stripped."""
|
||||||
if not self._device:
|
if not self._device:
|
||||||
raise ConnectionClosedException("Not connected")
|
raise ConnectionClosedException("Not connected")
|
||||||
return self._device.send_command(
|
output = self._device.send_command(
|
||||||
command,
|
command,
|
||||||
read_timeout=read_timeout,
|
read_timeout=read_timeout,
|
||||||
cmd_verify=False,
|
cmd_verify=False,
|
||||||
expect_string=_expect_for(command),
|
expect_string=expect_for(command),
|
||||||
).strip()
|
)
|
||||||
|
return strip_terminal_codes(output).strip()
|
||||||
|
|
||||||
def _sudo(self, command: str, read_timeout: float = 100) -> str:
|
def _sudo(self, command: str, read_timeout: float = 100) -> str:
|
||||||
"""Run *command* as root and return its output, stdout and stderr together.
|
"""Run *command* as root and return its output, stdout and stderr together.
|
||||||
@@ -547,14 +520,14 @@ class LinuxDriver(
|
|||||||
return self._run_privileged(command, timeout)
|
return self._run_privileged(command, timeout)
|
||||||
|
|
||||||
def _run_privileged(self, command: str, timeout: float = 100) -> str:
|
def _run_privileged(self, command: str, timeout: float = 100) -> str:
|
||||||
"""Run *command* as root: directly for a root login, through ``_sudo``
|
"""Run all of *command* as root: directly for a root login, through ``_sudo``
|
||||||
with a sudo password, and through ``sudo -n`` without one -- which fails at
|
with a sudo password, and through ``sudo -n sh -c`` without one -- which fails
|
||||||
once where a password prompt would hang the session until the timeout."""
|
at once where a password prompt would hang the session until the timeout."""
|
||||||
if self._is_root():
|
if self._is_root():
|
||||||
return self._send(command, read_timeout=timeout)
|
return self._send(command, read_timeout=timeout)
|
||||||
if self._sudo_password:
|
if self._sudo_password:
|
||||||
return self._sudo(command, read_timeout=timeout) # never typed into the shell
|
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)
|
# Command channel (napalm-device-types CommandChannelMixin)
|
||||||
@@ -568,12 +541,10 @@ class LinuxDriver(
|
|||||||
|
|
||||||
def _privileged(self, command: str, privileged: bool) -> tuple[str, bytes | None]:
|
def _privileged(self, command: str, privileged: bool) -> tuple[str, bytes | None]:
|
||||||
"""The command line that runs *command* with the privileges asked for,
|
"""The command line that runs *command* with the privileges asked for,
|
||||||
and what has to reach its stdin first.
|
and what has to reach its stdin first: root's as ``_as_root`` says.
|
||||||
|
|
||||||
Root runs it directly. With a sudo password, ``sudo -S`` reads it from
|
The command goes to ``sh -c`` as one argument, so the privilege covers
|
||||||
stdin, so it never appears in a process list. Without one, ``sudo -n``
|
every part of it.
|
||||||
fails at once where a prompt would hang. The command goes to ``sh -c``
|
|
||||||
as one argument, so the privilege covers every part of it.
|
|
||||||
"""
|
"""
|
||||||
if not privileged or self._is_root():
|
if not privileged or self._is_root():
|
||||||
return command, None
|
return command, None
|
||||||
@@ -799,53 +770,24 @@ class LinuxDriver(
|
|||||||
except (IndexError, ValueError):
|
except (IndexError, ValueError):
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
def get_lldp_neighbors(self) -> dict[str, list[dict[str, Any]]]:
|
def get_lldp_neighbors(self) -> dict[str, list[LldpNeighborDict]]:
|
||||||
"""Return LLDP neighbors if lldpd is installed and currently running.
|
"""Return LLDP neighbors if lldpd is installed and currently running.
|
||||||
|
|
||||||
Uses ``lldpctl -f keyvalue``. Returns an empty dict when lldpd is
|
Uses ``lldpctl -f keyvalue``, parsed by napalm-device-types: every
|
||||||
|
neighbour of a port is its own entry, and one whose chassis id is a MAC
|
||||||
|
address carries it as ``mac``. Returns an empty dict when lldpd is
|
||||||
absent or stopped — does NOT attempt to start the daemon.
|
absent or stopped — does NOT attempt to start the daemon.
|
||||||
|
|
||||||
|
:raises ValueError: when lldpd runs but lldpctl fails (socket out of reach).
|
||||||
"""
|
"""
|
||||||
# Check lldpctl is available
|
|
||||||
if not self._send("command -v lldpctl 2>/dev/null").strip():
|
if not self._send("command -v lldpctl 2>/dev/null").strip():
|
||||||
return {}
|
return {}
|
||||||
|
|
||||||
# Check lldpd is active (systemd or fallback to pgrep)
|
# The whole state: systemctl's "inactive" holds the word "active" (#66).
|
||||||
running = self._send(
|
if self._send(_LLDPD_STATE_COMMAND).strip() != "running":
|
||||||
"systemctl is-active lldpd 2>/dev/null || "
|
|
||||||
"service lldpd status 2>/dev/null | grep -q running && echo active || "
|
|
||||||
"pgrep -x lldpd >/dev/null 2>&1 && echo active || true"
|
|
||||||
).strip()
|
|
||||||
if "active" not in running:
|
|
||||||
return {}
|
return {}
|
||||||
|
|
||||||
output = self._send("lldpctl -f keyvalue 2>/dev/null || true")
|
return lldp_neighbors(parse_lldpctl_keyvalue(self._send(LLDPCTL_KEYVALUE_COMMAND)))
|
||||||
neighbors: dict[str, list[dict[str, Any]]] = {}
|
|
||||||
entries: dict[str, dict[str, str]] = {}
|
|
||||||
|
|
||||||
for line in output.splitlines():
|
|
||||||
line = line.strip()
|
|
||||||
if "=" not in line:
|
|
||||||
continue
|
|
||||||
key, _, value = line.partition("=")
|
|
||||||
parts = key.split(".")
|
|
||||||
if len(parts) < 3 or parts[0] != "lldp":
|
|
||||||
continue
|
|
||||||
iface = parts[1]
|
|
||||||
subkey = ".".join(parts[2:])
|
|
||||||
entries.setdefault(iface, {})[subkey] = value
|
|
||||||
|
|
||||||
for iface, data in entries.items():
|
|
||||||
entry = {
|
|
||||||
"hostname": data.get("chassis.name", ""),
|
|
||||||
"port": data.get("port.ifname", data.get("port.id.value", "")),
|
|
||||||
}
|
|
||||||
if data.get("chassis.id.subtype") == "mac":
|
|
||||||
mac = data.get("chassis.id.value", "")
|
|
||||||
if re.match(r"^([0-9A-Fa-f]{2}:){5}[0-9A-Fa-f]{2}$", mac):
|
|
||||||
entry["mac"] = mac.lower()
|
|
||||||
neighbors.setdefault(iface, []).append(entry)
|
|
||||||
|
|
||||||
return neighbors
|
|
||||||
|
|
||||||
def get_interfaces(self) -> dict[str, Any]:
|
def get_interfaces(self) -> dict[str, Any]:
|
||||||
"""Return interface operational data."""
|
"""Return interface operational data."""
|
||||||
@@ -972,7 +914,7 @@ class LinuxDriver(
|
|||||||
def _make_entry(
|
def _make_entry(
|
||||||
proto: str, nexthop: str, iface: str, metric: int, network: str
|
proto: str, nexthop: str, iface: str, metric: int, network: str
|
||||||
) -> dict[str, Any]:
|
) -> dict[str, Any]:
|
||||||
family = "ipv6" if (":" in network or (nexthop and ":" in nexthop)) else "ipv4"
|
family = "ipv6" if ":" in network else "ipv4" # not the next hop's (#65)
|
||||||
return {
|
return {
|
||||||
"protocol": proto,
|
"protocol": proto,
|
||||||
"family": family,
|
"family": family,
|
||||||
@@ -987,54 +929,19 @@ class LinuxDriver(
|
|||||||
"protocol_attributes": {},
|
"protocol_attributes": {},
|
||||||
}
|
}
|
||||||
|
|
||||||
def _add(network: str, proto: str, nexthop: str, iface: str, metric: int) -> None:
|
def _add(route: IpRoute) -> None:
|
||||||
if destination and network != destination:
|
if destination and route.network != destination:
|
||||||
return
|
return
|
||||||
mapped = proto_map.get(proto, proto)
|
mapped = proto_map.get(route.proto, route.proto)
|
||||||
if protocol and mapped != protocol.lower():
|
if protocol and mapped != protocol.lower():
|
||||||
return
|
return
|
||||||
routes.setdefault(network, []).append(
|
entry = _make_entry(mapped, route.nexthop, route.iface, route.metric, route.network)
|
||||||
_make_entry(mapped, nexthop, iface, metric, network)
|
entry["protocol_attributes"] = route.attributes
|
||||||
)
|
routes.setdefault(route.network, []).append(entry)
|
||||||
|
|
||||||
out = self._send("ip -4 route show && ip -6 route show")
|
for family, command in IP_ROUTE_COMMANDS.items():
|
||||||
for line in out.splitlines():
|
for route in parse_ip_route(self._send(command), family):
|
||||||
line = line.strip()
|
_add(route)
|
||||||
if not line or line.startswith("#"):
|
|
||||||
continue
|
|
||||||
dest_m = re.match(r"^(\S+)", line)
|
|
||||||
if not dest_m:
|
|
||||||
continue
|
|
||||||
raw_dest = dest_m.group(1)
|
|
||||||
network = (
|
|
||||||
"0.0.0.0/0"
|
|
||||||
if raw_dest == "default"
|
|
||||||
else ("::/0" if raw_dest == "default6" else raw_dest)
|
|
||||||
)
|
|
||||||
if "/" not in network:
|
|
||||||
network += "/32"
|
|
||||||
|
|
||||||
nexthop = ""
|
|
||||||
nh_m = re.search(r"via\s+(\S+)", line)
|
|
||||||
if nh_m:
|
|
||||||
nexthop = nh_m.group(1)
|
|
||||||
|
|
||||||
iface = ""
|
|
||||||
dev_m = re.search(r"dev\s+(\S+)", line)
|
|
||||||
if dev_m:
|
|
||||||
iface = dev_m.group(1)
|
|
||||||
|
|
||||||
proto = "kernel"
|
|
||||||
proto_m = re.search(r"proto\s+(\S+)", line)
|
|
||||||
if proto_m:
|
|
||||||
proto = proto_m.group(1)
|
|
||||||
|
|
||||||
metric = 0
|
|
||||||
metric_m = re.search(r"metric\s+(\d+)", line)
|
|
||||||
if metric_m:
|
|
||||||
metric = int(metric_m.group(1))
|
|
||||||
|
|
||||||
_add(network, proto, nexthop, iface, metric)
|
|
||||||
|
|
||||||
# FRR/Zebra enrichment via vtysh — properly attributes OSPF/BGP/RIP protocols.
|
# FRR/Zebra enrichment via vtysh — properly attributes OSPF/BGP/RIP protocols.
|
||||||
# FRR installs routes into the kernel as "proto zebra"; vtysh gives the real source.
|
# FRR installs routes into the kernel as "proto zebra"; vtysh gives the real source.
|
||||||
@@ -1063,12 +970,10 @@ class LinuxDriver(
|
|||||||
if not vm:
|
if not vm:
|
||||||
continue
|
continue
|
||||||
code = vm.group(1)
|
code = vm.group(1)
|
||||||
prefix = vm.group(2)
|
prefix = with_length(vm.group(2))
|
||||||
metric = int(vm.group(4))
|
metric = int(vm.group(4))
|
||||||
nexthop = vm.group(5) or ""
|
nexthop = vm.group(5) or ""
|
||||||
iface = (vm.group(6) or "").rstrip(",")
|
iface = (vm.group(6) or "").rstrip(",")
|
||||||
if "/" not in prefix:
|
|
||||||
prefix += "/32"
|
|
||||||
mapped_proto = _frr_code.get(code, code.lower())
|
mapped_proto = _frr_code.get(code, code.lower())
|
||||||
if prefix in routes:
|
if prefix in routes:
|
||||||
for entry in routes[prefix]:
|
for entry in routes[prefix]:
|
||||||
@@ -1097,7 +1002,7 @@ class LinuxDriver(
|
|||||||
"interface": m.group(2),
|
"interface": m.group(2),
|
||||||
"mac": m.group(3),
|
"mac": m.group(3),
|
||||||
"ip": m.group(1),
|
"ip": m.group(1),
|
||||||
"age": 0.0,
|
"age": -1.0, # `ip neigh` has no age: unknown, never 0.0 ("just now")
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
return entries
|
return entries
|
||||||
@@ -1160,48 +1065,8 @@ class LinuxDriver(
|
|||||||
count: int = 5,
|
count: int = 5,
|
||||||
vrf: str = "",
|
vrf: str = "",
|
||||||
) -> dict[str, Any]:
|
) -> dict[str, Any]:
|
||||||
"""Execute ping from the remote host."""
|
"""Ping from the remote host; ValueError for a bad number or a name starting with -."""
|
||||||
src_opt = f"-I {source}" if source else ""
|
return parse_ping(self._send(ping_command(destination, source, ttl, timeout, size, count)))
|
||||||
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,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
# ------------------------------------------------------------------
|
# ------------------------------------------------------------------
|
||||||
# KernelFactsMixin – the transport for get_kernel_facts
|
# KernelFactsMixin – the transport for get_kernel_facts
|
||||||
@@ -1279,52 +1144,7 @@ class LinuxDriver(
|
|||||||
raise NotImplementedError(f"Package manager '{self._pkg_manager}' is not supported")
|
raise NotImplementedError(f"Package manager '{self._pkg_manager}' is not supported")
|
||||||
|
|
||||||
def _get_packages_apt(self) -> list[PackageDict]:
|
def _get_packages_apt(self) -> list[PackageDict]:
|
||||||
out = self._send(
|
return parse_dpkg_query(self._send(DPKG_QUERY_COMMAND))
|
||||||
"dpkg-query -W -f='${Package}\\t${Version}\\t${Installed-Size}"
|
|
||||||
"\\t${source:Package}\\t${source:Version}\\t${binary:Summary}\\n' 2>/dev/null"
|
|
||||||
)
|
|
||||||
packages: list[PackageDict] = []
|
|
||||||
for line in out.splitlines():
|
|
||||||
# Summary stays last and keeps whatever it contains: maxsplit must
|
|
||||||
# equal the number of tabs the format writes, not the field count.
|
|
||||||
parts = line.split("\t", 5)
|
|
||||||
if len(parts) < 2:
|
|
||||||
continue
|
|
||||||
name = parts[0].strip()
|
|
||||||
version = parts[1].strip()
|
|
||||||
size = (
|
|
||||||
int(parts[2].strip()) * 1024 if len(parts) > 2 and parts[2].strip().isdigit() else 0
|
|
||||||
)
|
|
||||||
# Debian source package (e.g. openssh-server → openssh) for OSV matching.
|
|
||||||
source_package = parts[3].strip() if len(parts) > 3 and parts[3].strip() else name
|
|
||||||
# And its version, which is a different number from this package's.
|
|
||||||
#
|
|
||||||
# OSV states Debian ranges in *source* versions. A source package
|
|
||||||
# that ships several binaries gives each its own upstream version:
|
|
||||||
# libldb2 is 2:2.11.0+samba4.22.11+dfsg-… while its source, samba,
|
|
||||||
# is 2:4.22.11+dfsg-…. A consumer matching on source_package and
|
|
||||||
# comparing `version` compares two unrelated numbers — dpkg reads
|
|
||||||
# ldb's 2.11.0 as older than the 2:4.17.4+dfsg-1 that fixed
|
|
||||||
# CVE-2022-44640, and a host five releases past the fix was reported
|
|
||||||
# vulnerable on four packages at once.
|
|
||||||
#
|
|
||||||
# dpkg leaves this empty when it equals `Version`; so does an older
|
|
||||||
# dpkg that does not know the field at all.
|
|
||||||
source_version = parts[4].strip() if len(parts) > 4 and parts[4].strip() else version
|
|
||||||
description = parts[5].strip() if len(parts) > 5 else ""
|
|
||||||
packages.append(
|
|
||||||
{
|
|
||||||
"name": name,
|
|
||||||
"version": version,
|
|
||||||
"installed": True,
|
|
||||||
"description": description,
|
|
||||||
"size": size,
|
|
||||||
"source": "apt",
|
|
||||||
"source_package": source_package,
|
|
||||||
"source_version": source_version,
|
|
||||||
}
|
|
||||||
)
|
|
||||||
return packages
|
|
||||||
|
|
||||||
def _get_packages_rpm(self) -> list[PackageDict]:
|
def _get_packages_rpm(self) -> list[PackageDict]:
|
||||||
out = self._send(
|
out = self._send(
|
||||||
@@ -1497,39 +1317,33 @@ class LinuxDriver(
|
|||||||
return packages
|
return packages
|
||||||
|
|
||||||
def install_package(self, name: str) -> dict[str, Any]:
|
def install_package(self, name: str) -> dict[str, Any]:
|
||||||
"""Install a package by name. Returns ``{"success": bool, "output": str}``."""
|
"""Install a package by name. Returns ``{"success": bool, "output": str}``.
|
||||||
|
|
||||||
|
Success is the package manager's exit status, as for ``uninstall_package``."""
|
||||||
from shlex import quote as _q
|
from shlex import quote as _q
|
||||||
|
|
||||||
safe = _q(name)
|
safe = _q(name)
|
||||||
pm = self._pkg_manager
|
pm = self._pkg_manager
|
||||||
if pm == "apt":
|
if pm == "apt":
|
||||||
raw = self._sudo(
|
cmd = f"DEBIAN_FRONTEND=noninteractive apt-get install -y {safe} 2>&1"
|
||||||
f"DEBIAN_FRONTEND=noninteractive apt-get install -y {safe} 2>&1 || true"
|
|
||||||
)
|
|
||||||
elif pm in ("dnf", "yum"):
|
elif pm in ("dnf", "yum"):
|
||||||
raw = self._sudo(f"{pm} install -y {safe} 2>&1 || true")
|
cmd = f"{pm} install -y {safe} 2>&1"
|
||||||
elif pm == "apk":
|
elif pm == "apk":
|
||||||
raw = self._sudo(f"apk add {safe} 2>&1 || true")
|
cmd = f"apk add {safe} 2>&1"
|
||||||
elif pm == "pacman":
|
elif pm == "pacman":
|
||||||
raw = self._sudo(f"pacman -S --noconfirm {safe} 2>&1 || true")
|
cmd = f"pacman -S --noconfirm {safe} 2>&1"
|
||||||
else:
|
else:
|
||||||
return {"success": False, "output": f"Unsupported package manager: {pm}"}
|
return {"success": False, "output": f"Unsupported package manager: {pm}"}
|
||||||
low = raw.lower()
|
raw, rc = self._sudo_status(cmd)
|
||||||
success = not any(
|
failed = self._package_command_failed(raw, rc, self._INSTALL_FAILED)
|
||||||
kw in low
|
return {"success": not failed, "output": raw.strip()}
|
||||||
for kw in (
|
|
||||||
"error:",
|
#: Words in a package manager's output that mean an install did not happen.
|
||||||
"failed",
|
#: Only consulted when the exit status is unknown; see ``_package_command_failed``.
|
||||||
"no packages",
|
_INSTALL_FAILED = ("error:", "failed", "no packages", "not found", "unable to locate")
|
||||||
"not found",
|
|
||||||
"unable to locate",
|
|
||||||
"no match",
|
|
||||||
)
|
|
||||||
)
|
|
||||||
return {"success": success, "output": raw.strip()}
|
|
||||||
|
|
||||||
#: Words in a package manager's output that mean it did not do the job.
|
#: Words in a package manager's output that mean it did not do the job.
|
||||||
#: Only consulted when the exit status is unknown; see ``_uninstall_failed``.
|
#: Only consulted when the exit status is unknown; see ``_package_command_failed``.
|
||||||
_UNINSTALL_FAILED = ("error:", "failed", "not found", "is not installed", "no packages")
|
_UNINSTALL_FAILED = ("error:", "failed", "not found", "is not installed", "no packages")
|
||||||
|
|
||||||
def uninstall_package(self, name: str, purge: bool = False) -> dict[str, Any]:
|
def uninstall_package(self, name: str, purge: bool = False) -> dict[str, Any]:
|
||||||
@@ -1575,16 +1389,17 @@ class LinuxDriver(
|
|||||||
return {"success": False, "output": f"Unsupported package manager: {pm}"}
|
return {"success": False, "output": f"Unsupported package manager: {pm}"}
|
||||||
|
|
||||||
raw, rc = self._sudo_status(cmd)
|
raw, rc = self._sudo_status(cmd)
|
||||||
failed = self._uninstall_failed(raw, rc)
|
failed = self._package_command_failed(raw, rc, self._UNINSTALL_FAILED)
|
||||||
|
|
||||||
if failed and pm == "apt":
|
if failed and pm == "apt":
|
||||||
forced, forced_rc = self._sudo_status(f"dpkg --purge --force-all {safe} 2>&1")
|
forced, forced_rc = self._sudo_status(f"dpkg --purge --force-all {safe} 2>&1")
|
||||||
raw = f"{raw.strip()}\n--- dpkg --purge --force-all ---\n{forced.strip()}"
|
raw = f"{raw.strip()}\n--- dpkg --purge --force-all ---\n{forced.strip()}"
|
||||||
failed = self._uninstall_failed(forced, forced_rc)
|
failed = self._package_command_failed(forced, forced_rc, self._UNINSTALL_FAILED)
|
||||||
|
|
||||||
return {"success": not failed, "output": raw.strip()}
|
return {"success": not failed, "output": raw.strip()}
|
||||||
|
|
||||||
def _uninstall_failed(self, output: str, rc: int | None = None) -> bool:
|
@staticmethod
|
||||||
|
def _package_command_failed(output: str, rc: int | None, keywords: tuple[str, ...]) -> bool:
|
||||||
"""Whether the package manager did not do the job.
|
"""Whether the package manager did not do the job.
|
||||||
|
|
||||||
The exit status decides whenever there is one (netork#267): it is the
|
The exit status decides whenever there is one (netork#267): it is the
|
||||||
@@ -1608,7 +1423,7 @@ class LinuxDriver(
|
|||||||
low = output.lower()
|
low = output.lower()
|
||||||
if any(line.lstrip().startswith("e: ") for line in low.splitlines()):
|
if any(line.lstrip().startswith("e: ") for line in low.splitlines()):
|
||||||
return True
|
return True
|
||||||
return any(kw in low for kw in self._UNINSTALL_FAILED)
|
return any(kw in low for kw in keywords)
|
||||||
|
|
||||||
def get_available_updates(self) -> list[UpdateDict]:
|
def get_available_updates(self) -> list[UpdateDict]:
|
||||||
if self._pkg_manager == "apt":
|
if self._pkg_manager == "apt":
|
||||||
@@ -1628,17 +1443,17 @@ class LinuxDriver(
|
|||||||
return parse_apt_upgradable(self._send(APT_UPGRADABLE_COMMAND, read_timeout=60))
|
return parse_apt_upgradable(self._send(APT_UPGRADABLE_COMMAND, read_timeout=60))
|
||||||
|
|
||||||
def _get_updates_rpm(self) -> list[UpdateDict]:
|
def _get_updates_rpm(self) -> list[UpdateDict]:
|
||||||
"""dnf/yum check-update: exit 100 means updates, 0 none, anything else failed."""
|
"""dnf/yum check-update on the cached metadata only, as apt reads its local index
|
||||||
|
(#59): exit 100 means updates, 0 none, anything else failed."""
|
||||||
cmd = (
|
cmd = (
|
||||||
"dnf check-update --quiet 2>/dev/null"
|
"dnf --cacheonly check-update --quiet 2>/dev/null"
|
||||||
if self._pkg_manager == "dnf"
|
if self._pkg_manager == "dnf"
|
||||||
else "yum check-update -q 2>/dev/null"
|
else "yum --cacheonly check-update -q 2>/dev/null"
|
||||||
)
|
)
|
||||||
output, status = _split_status(self._run_privileged(f"{cmd}; echo {_RC_MARKER}$?", 120))
|
output, status = _split_status(self._run_privileged(f"{cmd}; echo {_RC_MARKER}$?", 120))
|
||||||
if status not in (0, 100):
|
if status not in (0, 100):
|
||||||
raise RuntimeError(
|
reason = output[-200:] or "no cached metadata? refresh the package index"
|
||||||
f"{self._pkg_manager} check-update failed (exit {status}): {output[-200:]}"
|
raise RuntimeError(f"{self._pkg_manager} check-update failed (exit {status}): {reason}")
|
||||||
)
|
|
||||||
security = self._rpm_security_names()
|
security = self._rpm_security_names()
|
||||||
updates: list[UpdateDict] = []
|
updates: list[UpdateDict] = []
|
||||||
for line in output.splitlines():
|
for line in output.splitlines():
|
||||||
@@ -1899,14 +1714,15 @@ class LinuxDriver(
|
|||||||
|
|
||||||
def get_processes(self) -> list[ProcessDict]:
|
def get_processes(self) -> list[ProcessDict]:
|
||||||
"""Return running processes via ``ps axo``."""
|
"""Return running processes via ``ps axo``."""
|
||||||
|
# lstart is five words ("Tue Oct 6 08:47:15 2026") -- one word each only in C.
|
||||||
out = self._send(
|
out = self._send(
|
||||||
"ps axo pid,ppid,user:20,pcpu,pmem,vsz,rss,tty,stat,lstart,args "
|
"LC_ALL=C ps axo pid,ppid,user:20,pcpu,pmem,vsz,rss,tty,stat,lstart,args "
|
||||||
"--no-headers 2>/dev/null"
|
"--no-headers 2>/dev/null"
|
||||||
)
|
)
|
||||||
processes: list[ProcessDict] = []
|
processes: list[ProcessDict] = []
|
||||||
for line in out.splitlines():
|
for line in out.splitlines():
|
||||||
parts = line.split(None, 10)
|
parts = line.split(None, 14)
|
||||||
if len(parts) < 11:
|
if len(parts) < 15:
|
||||||
continue
|
continue
|
||||||
try:
|
try:
|
||||||
pid = int(parts[0])
|
pid = int(parts[0])
|
||||||
@@ -1918,12 +1734,8 @@ class LinuxDriver(
|
|||||||
rss = int(parts[6])
|
rss = int(parts[6])
|
||||||
tty = parts[7] if parts[7] != "?" else ""
|
tty = parts[7] if parts[7] != "?" else ""
|
||||||
state = parts[8][0] if parts[8] else "?"
|
state = parts[8][0] if parts[8] else "?"
|
||||||
# lstart is 5 tokens: "Mon May 27 12:34:56 2024" → parts[9..13]
|
started = " ".join(parts[9:14])
|
||||||
# args starts at parts[14] but we merged from 10 onward
|
command = parts[14]
|
||||||
# With --no-headers and ps axo, lstart takes 5 parts
|
|
||||||
# Rebuild: parts[9] is start, args is parts[10]
|
|
||||||
started = parts[9]
|
|
||||||
command = parts[10]
|
|
||||||
except (ValueError, IndexError):
|
except (ValueError, IndexError):
|
||||||
continue
|
continue
|
||||||
processes.append(
|
processes.append(
|
||||||
@@ -1943,77 +1755,6 @@ class LinuxDriver(
|
|||||||
)
|
)
|
||||||
return processes
|
return processes
|
||||||
|
|
||||||
# ------------------------------------------------------------------
|
|
||||||
# OSDriver – cron jobs
|
|
||||||
# ------------------------------------------------------------------
|
|
||||||
|
|
||||||
def get_cron_jobs(self) -> list[CronJobDict]:
|
|
||||||
"""Return cron entries from user crontabs and /etc/cron.d."""
|
|
||||||
jobs: list[CronJobDict] = []
|
|
||||||
|
|
||||||
# /etc/cron.d/* — system-wide cron fragments (include user field)
|
|
||||||
cron_d_files = self._send("ls /etc/cron.d/ 2>/dev/null").splitlines()
|
|
||||||
for fname in cron_d_files:
|
|
||||||
fname = fname.strip()
|
|
||||||
if not fname:
|
|
||||||
continue
|
|
||||||
content = self._send(f"cat /etc/cron.d/{fname} 2>/dev/null")
|
|
||||||
for line in content.splitlines():
|
|
||||||
job = self._parse_cron_line(line, source_user="root", has_user_field=True)
|
|
||||||
if job:
|
|
||||||
jobs.append(job)
|
|
||||||
|
|
||||||
# Per-user crontabs from /var/spool/cron/crontabs (Debian) or /var/spool/cron (RHEL)
|
|
||||||
for spool_dir in ("/var/spool/cron/crontabs", "/var/spool/cron"):
|
|
||||||
ls_out = self._send(f"ls {spool_dir} 2>/dev/null")
|
|
||||||
for uname in ls_out.splitlines():
|
|
||||||
uname = uname.strip()
|
|
||||||
if not uname:
|
|
||||||
continue
|
|
||||||
content = self._send(f"cat {spool_dir}/{uname} 2>/dev/null")
|
|
||||||
for line in content.splitlines():
|
|
||||||
job = self._parse_cron_line(line, source_user=uname, has_user_field=False)
|
|
||||||
if job:
|
|
||||||
jobs.append(job)
|
|
||||||
|
|
||||||
return jobs
|
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def _parse_cron_line(line: str, source_user: str, has_user_field: bool) -> CronJobDict | None:
|
|
||||||
"""Parse a single crontab line; returns ``None`` for comments/blanks."""
|
|
||||||
stripped = line.strip()
|
|
||||||
# Remove trailing comment
|
|
||||||
comment = ""
|
|
||||||
if "#" in stripped:
|
|
||||||
idx = stripped.index("#")
|
|
||||||
comment = stripped[idx + 1 :].strip()
|
|
||||||
stripped = stripped[:idx].strip()
|
|
||||||
|
|
||||||
if not stripped or stripped.startswith("@") or stripped.startswith("MAILTO"):
|
|
||||||
return None
|
|
||||||
|
|
||||||
parts = stripped.split(None, 6 if has_user_field else 5)
|
|
||||||
expected = 6 if has_user_field else 5
|
|
||||||
if len(parts) < expected:
|
|
||||||
return None
|
|
||||||
|
|
||||||
schedule = " ".join(parts[:5])
|
|
||||||
if has_user_field:
|
|
||||||
user = parts[5]
|
|
||||||
command = parts[6] if len(parts) > 6 else ""
|
|
||||||
else:
|
|
||||||
user = source_user
|
|
||||||
command = parts[5] if len(parts) > 5 else ""
|
|
||||||
|
|
||||||
job: CronJobDict = {
|
|
||||||
"user": user,
|
|
||||||
"schedule": schedule,
|
|
||||||
"command": command,
|
|
||||||
}
|
|
||||||
if comment:
|
|
||||||
job["description"] = comment
|
|
||||||
return job
|
|
||||||
|
|
||||||
# ── Device actions ────────────────────────────────────────────────────────
|
# ── Device actions ────────────────────────────────────────────────────────
|
||||||
|
|
||||||
def get_snmp_config(self) -> SNMPConfigDict | None:
|
def get_snmp_config(self) -> SNMPConfigDict | None:
|
||||||
@@ -2159,8 +1900,8 @@ class LinuxDriver(
|
|||||||
Falls back to interface-level data from ``ip link`` + ``/proc/net/dev``
|
Falls back to interface-level data from ``ip link`` + ``/proc/net/dev``
|
||||||
when root access is unavailable.
|
when root access is unavailable.
|
||||||
|
|
||||||
Full data keyed by ``wireguard-<iface>-<pubkey[:8]>`` (one entry per peer).
|
Full data keyed by ``wireguard-<iface>-<pubkey[:8]>``, one per peer, URL-safe
|
||||||
Fallback keyed by ``wireguard-<iface>`` (one entry per WireGuard interface).
|
(``+`` → ``-``, ``/`` → ``_``); the fallback by ``wireguard-<iface>``, one per interface.
|
||||||
"""
|
"""
|
||||||
import time as _time
|
import time as _time
|
||||||
|
|
||||||
@@ -2237,7 +1978,7 @@ class LinuxDriver(
|
|||||||
local_port = listen_ports.get(iface, "")
|
local_port = listen_ports.get(iface, "")
|
||||||
local_ep = f":{local_port}" if local_port and local_port != "0" else ""
|
local_ep = f":{local_port}" if local_port and local_port != "0" else ""
|
||||||
|
|
||||||
key = f"wireguard-{iface}-{pubkey[:8]}"
|
key = f"wireguard-{iface}-{pubkey[:8].replace('+', '-').replace('/', '_')}"
|
||||||
tunnels[key] = {
|
tunnels[key] = {
|
||||||
"type": "WireGuard",
|
"type": "WireGuard",
|
||||||
"local_endpoint": local_ep,
|
"local_endpoint": local_ep,
|
||||||
|
|||||||
+5
-7
@@ -4,10 +4,10 @@ build-backend = "setuptools.build_meta"
|
|||||||
|
|
||||||
[project]
|
[project]
|
||||||
name = "napalm-linux"
|
name = "napalm-linux"
|
||||||
version = "0.15.0"
|
version = "0.16.0"
|
||||||
description = "NAPALM driver for generic Linux systems via SSH"
|
description = "NAPALM driver for generic Linux systems via SSH"
|
||||||
readme = "README.md"
|
readme = "README.md"
|
||||||
requires-python = ">=3.10"
|
requires-python = ">=3.12"
|
||||||
license = "Apache-2.0"
|
license = "Apache-2.0"
|
||||||
authors = [
|
authors = [
|
||||||
{ name = "Christian Manivong", email = "christian@manivong.de" },
|
{ name = "Christian Manivong", email = "christian@manivong.de" },
|
||||||
@@ -26,8 +26,6 @@ classifiers = [
|
|||||||
"Intended Audience :: System Administrators",
|
"Intended Audience :: System Administrators",
|
||||||
"Operating System :: OS Independent",
|
"Operating System :: OS Independent",
|
||||||
"Programming Language :: Python :: 3",
|
"Programming Language :: Python :: 3",
|
||||||
"Programming Language :: Python :: 3.10",
|
|
||||||
"Programming Language :: Python :: 3.11",
|
|
||||||
"Programming Language :: Python :: 3.12",
|
"Programming Language :: Python :: 3.12",
|
||||||
"Programming Language :: Python :: 3.13",
|
"Programming Language :: Python :: 3.13",
|
||||||
"Topic :: System :: Networking",
|
"Topic :: System :: Networking",
|
||||||
@@ -36,7 +34,7 @@ classifiers = [
|
|||||||
]
|
]
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"napalm>=4.0",
|
"napalm>=4.0",
|
||||||
"napalm-device-types>=4.22.0",
|
"napalm-device-types>=4.27.0",
|
||||||
"netmiko>=4.0.0",
|
"netmiko>=4.0.0",
|
||||||
"paramiko>=5.0.0", # CVE-2026-44405
|
"paramiko>=5.0.0", # CVE-2026-44405
|
||||||
]
|
]
|
||||||
@@ -66,7 +64,7 @@ napalm_linux = ["py.typed"]
|
|||||||
|
|
||||||
[tool.ruff]
|
[tool.ruff]
|
||||||
line-length = 100
|
line-length = 100
|
||||||
target-version = "py310"
|
target-version = "py312"
|
||||||
|
|
||||||
[tool.ruff.lint]
|
[tool.ruff.lint]
|
||||||
select = ["E", "F", "I", "UP", "B"]
|
select = ["E", "F", "I", "UP", "B"]
|
||||||
@@ -76,7 +74,7 @@ select = ["E", "F", "I", "UP", "B"]
|
|||||||
"tests/**" = ["E501"]
|
"tests/**" = ["E501"]
|
||||||
|
|
||||||
[tool.mypy]
|
[tool.mypy]
|
||||||
python_version = "3.10"
|
python_version = "3.12"
|
||||||
strict = true
|
strict = true
|
||||||
# napalm itself is untyped, so every driver subclasses Any.
|
# napalm itself is untyped, so every driver subclasses Any.
|
||||||
disallow_subclassing_any = false
|
disallow_subclassing_any = false
|
||||||
|
|||||||
@@ -2,9 +2,8 @@
|
|||||||
"coverage_floor": 57.0,
|
"coverage_floor": 57.0,
|
||||||
"function_complexity": {
|
"function_complexity": {
|
||||||
"napalm_linux/linux.py::LinuxDriver._collect_platform_info": 24,
|
"napalm_linux/linux.py::LinuxDriver._collect_platform_info": 24,
|
||||||
"napalm_linux/linux.py::LinuxDriver._parse_cron_line": 12,
|
|
||||||
"napalm_linux/linux.py::LinuxDriver.get_networks": 13,
|
"napalm_linux/linux.py::LinuxDriver.get_networks": 13,
|
||||||
"napalm_linux/linux.py::LinuxDriver.get_route_to": 23,
|
"napalm_linux/linux.py::LinuxDriver.get_route_to": 13,
|
||||||
"napalm_linux/linux.py::LinuxDriver.get_snmp_config": 13,
|
"napalm_linux/linux.py::LinuxDriver.get_snmp_config": 13,
|
||||||
"napalm_linux/linux.py::LinuxDriver.get_vpn_tunnels": 28,
|
"napalm_linux/linux.py::LinuxDriver.get_vpn_tunnels": 28,
|
||||||
"napalm_linux/linux.py::LinuxDriver.search_packages": 26
|
"napalm_linux/linux.py::LinuxDriver.search_packages": 26
|
||||||
@@ -12,9 +11,8 @@
|
|||||||
"function_lines": {
|
"function_lines": {
|
||||||
"napalm_linux/linux.py::LinuxDriver._collect_platform_info": 106,
|
"napalm_linux/linux.py::LinuxDriver._collect_platform_info": 106,
|
||||||
"napalm_linux/linux.py::LinuxDriver.get_networks": 52,
|
"napalm_linux/linux.py::LinuxDriver.get_networks": 52,
|
||||||
"napalm_linux/linux.py::LinuxDriver.get_route_to": 138,
|
"napalm_linux/linux.py::LinuxDriver.get_route_to": 101,
|
||||||
"napalm_linux/linux.py::LinuxDriver.get_vpn_tunnels": 152,
|
"napalm_linux/linux.py::LinuxDriver.get_vpn_tunnels": 152,
|
||||||
"napalm_linux/linux.py::LinuxDriver.ping": 52,
|
|
||||||
"napalm_linux/linux.py::LinuxDriver.search_packages": 106,
|
"napalm_linux/linux.py::LinuxDriver.search_packages": 106,
|
||||||
"napalm_linux/linux.py::LinuxDriver.uninstall_package": 51
|
"napalm_linux/linux.py::LinuxDriver.uninstall_package": 51
|
||||||
},
|
},
|
||||||
@@ -22,7 +20,7 @@
|
|||||||
"napalm_linux.linux"
|
"napalm_linux.linux"
|
||||||
],
|
],
|
||||||
"python_file_lines": {
|
"python_file_lines": {
|
||||||
"napalm_linux/linux.py": 2307
|
"napalm_linux/linux.py": 2048
|
||||||
},
|
},
|
||||||
"untyped_public": []
|
"untyped_public": []
|
||||||
}
|
}
|
||||||
@@ -169,7 +169,10 @@ class TestPackageProxyAndEngineAccess:
|
|||||||
|
|
||||||
result = driver.configure_package_proxy("http://cache.lan:3142")
|
result = driver.configure_package_proxy("http://cache.lan:3142")
|
||||||
|
|
||||||
assert [s["step"] for s in result["steps"]] == ["configure", "conflicts", "verify", "fetch"]
|
# The steps napalm-device-types runs on this driver's transports; later ones
|
||||||
|
# (https, NetOrk/netork#1811) are the mixin's own.
|
||||||
|
steps = [s["step"] for s in result["steps"]]
|
||||||
|
assert steps[:4] == ["configure", "conflicts", "verify", "fetch"]
|
||||||
assert any("dists/trixie/InRelease" in c for c in ran)
|
assert any("dists/trixie/InRelease" in c for c in ran)
|
||||||
|
|
||||||
def test_another_file_s_proxy_is_commented_out_as_root(self, driver):
|
def test_another_file_s_proxy_is_commented_out_as_root(self, driver):
|
||||||
|
|||||||
+607
-40
@@ -5,6 +5,7 @@ from unittest.mock import MagicMock, patch
|
|||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
|
from napalm_linux._routes import IpRoute, parse_ip_route
|
||||||
from napalm_linux.linux import (
|
from napalm_linux.linux import (
|
||||||
_YUM_SECURITY_COMMAND,
|
_YUM_SECURITY_COMMAND,
|
||||||
LinuxDriver,
|
LinuxDriver,
|
||||||
@@ -113,39 +114,6 @@ def _mock_send(driver_fixture, output: str):
|
|||||||
driver_fixture._device.send_command.return_value = output
|
driver_fixture._device.send_command.return_value = output
|
||||||
|
|
||||||
|
|
||||||
# ---------------------------------------------------------------------------
|
|
||||||
# _parse_cron_line
|
|
||||||
# ---------------------------------------------------------------------------
|
|
||||||
|
|
||||||
|
|
||||||
class TestParseCronLine:
|
|
||||||
def test_regular_user_cron(self):
|
|
||||||
line = "0 4 * * * /usr/local/bin/backup.sh # nightly backup"
|
|
||||||
job = LinuxDriver._parse_cron_line(line, source_user="root", has_user_field=False)
|
|
||||||
assert job is not None
|
|
||||||
assert job["user"] == "root"
|
|
||||||
assert job["schedule"] == "0 4 * * *"
|
|
||||||
assert job["command"] == "/usr/local/bin/backup.sh"
|
|
||||||
assert job.get("description") == "nightly backup"
|
|
||||||
|
|
||||||
def test_cron_d_with_user_field(self):
|
|
||||||
line = "*/5 * * * * www-data /usr/bin/php /var/www/cron.php"
|
|
||||||
job = LinuxDriver._parse_cron_line(line, source_user="root", has_user_field=True)
|
|
||||||
assert job is not None
|
|
||||||
assert job["user"] == "www-data"
|
|
||||||
assert job["schedule"] == "*/5 * * * *"
|
|
||||||
assert "/usr/bin/php" in job["command"]
|
|
||||||
|
|
||||||
def test_comment_line_returns_none(self):
|
|
||||||
assert LinuxDriver._parse_cron_line("# this is a comment", "root", False) is None
|
|
||||||
|
|
||||||
def test_blank_line_returns_none(self):
|
|
||||||
assert LinuxDriver._parse_cron_line(" ", "root", False) is None
|
|
||||||
|
|
||||||
def test_mailto_returns_none(self):
|
|
||||||
assert LinuxDriver._parse_cron_line("MAILTO=root", "root", False) is None
|
|
||||||
|
|
||||||
|
|
||||||
# ---------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------
|
||||||
# get_interfaces (parsing)
|
# get_interfaces (parsing)
|
||||||
# ---------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------
|
||||||
@@ -169,6 +137,29 @@ def test_get_interfaces_parses_state(driver):
|
|||||||
assert result["eth1"]["is_up"] is False
|
assert result["eth1"]["is_up"] is False
|
||||||
|
|
||||||
|
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
# get_arp_table
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
IP_NEIGH_OUTPUT = """\
|
||||||
|
192.168.1.1 dev eth0 lladdr aa:bb:cc:dd:ee:01 REACHABLE
|
||||||
|
192.168.1.20 dev eth0 lladdr aa:bb:cc:dd:ee:02 STALE
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
def test_get_arp_table_reports_no_age(driver):
|
||||||
|
"""`ip neigh show` says nothing about when a neighbour was last confirmed --
|
||||||
|
a STALE entry may be hours old. 0.0 would read as "just now" to a caller
|
||||||
|
that ranks sightings by freshness (NetOrk/netork#1327)."""
|
||||||
|
with patch.object(driver, "_send", return_value=IP_NEIGH_OUTPUT):
|
||||||
|
result = driver.get_arp_table()
|
||||||
|
assert [(e["ip"], e["age"]) for e in result] == [
|
||||||
|
("192.168.1.1", -1.0),
|
||||||
|
("192.168.1.20", -1.0),
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
# ---------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------
|
||||||
# get_config
|
# get_config
|
||||||
# ---------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------
|
||||||
@@ -236,17 +227,41 @@ def test_parse_uptime_invalid(driver):
|
|||||||
#: assertion noticed. A fixture simpler than the data cannot fail the way the
|
#: assertion noticed. A fixture simpler than the data cannot fail the way the
|
||||||
#: data does.
|
#: data does.
|
||||||
APT_PKG_OUTPUT = (
|
APT_PKG_OUTPUT = (
|
||||||
"openssh-server\t1:9.2p1-2+deb12u2\t512\topenssh\t1:9.2p1-2+deb12u2"
|
"ii \topenssh-server\t1:9.2p1-2+deb12u2\t512\topenssh\t1:9.2p1-2+deb12u2"
|
||||||
"\tsecure shell server\n"
|
"\tsecure shell server\n"
|
||||||
"curl\t7.88.1-10+deb12u5\t1024\tcurl\t7.88.1-10+deb12u5"
|
"ii \tcurl\t7.88.1-10+deb12u5\t1024\tcurl\t7.88.1-10+deb12u5"
|
||||||
"\tcommand line tool for transferring data\n"
|
"\tcommand line tool for transferring data\n"
|
||||||
# The shape that matters: a binary package whose own upstream version has
|
# The shape that matters: a binary package whose own upstream version has
|
||||||
# nothing to do with its source package's. ldb 2.11.0 is built from samba
|
# nothing to do with its source package's. ldb 2.11.0 is built from samba
|
||||||
# 4.22.11, and OSV states Debian ranges in source versions.
|
# 4.22.11, and OSV states Debian ranges in source versions.
|
||||||
"libldb2\t2:2.11.0+samba4.22.11+dfsg-0+deb13u1\t2048\tsamba"
|
"ii \tlibldb2\t2:2.11.0+samba4.22.11+dfsg-0+deb13u1\t2048\tsamba"
|
||||||
"\t2:4.22.11+dfsg-0+deb13u1\tLDB shared library\n"
|
"\t2:4.22.11+dfsg-0+deb13u1\tLDB shared library\n"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
#: Removed packages whose configuration dpkg still keeps (state ``rc``), next to
|
||||||
|
#: a held one and one whose postinst failed -- both still on disk (#17).
|
||||||
|
APT_PKG_STATES = (
|
||||||
|
"rc \twazuh-agent\t4.14.7-1\t\twazuh-agent\t4.14.7-1\tWazuh agent\n"
|
||||||
|
"rc \tlinux-image-6.8.0-117-generic\t6.8.0-117.117\t\tlinux-signed\t6.8.0-117.117\tSigned kernel image generic\n"
|
||||||
|
"hi \tdocker-ce\t5:28.5.1-1~debian.13~trixie\t104858\tdocker-ce\t5:28.5.1-1~debian.13~trixie\tDocker: the open-source application container engine\n"
|
||||||
|
"iU \tzabbix-agent\t1:7.0.10-1\t1550\tzabbix\t1:7.0.10-1\tZabbix network monitoring solution - agent\n"
|
||||||
|
"ii \tcurl\t8.14.1-2\t512\tcurl\t8.14.1-2\tcommand line tool for transferring data with URL syntax\n"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def test_get_packages_apt_reads_the_package_state(driver):
|
||||||
|
driver._pkg_manager = "apt"
|
||||||
|
with patch.object(driver, "_send", return_value=APT_PKG_STATES) as send:
|
||||||
|
driver.get_packages()
|
||||||
|
assert send.call_args[0][0].startswith("dpkg-query -W -f='${db:Status-Abbrev}\\t${Package}")
|
||||||
|
|
||||||
|
|
||||||
|
def test_get_packages_apt_leaves_out_what_only_left_its_configuration(driver):
|
||||||
|
driver._pkg_manager = "apt"
|
||||||
|
with patch.object(driver, "_send", return_value=APT_PKG_STATES):
|
||||||
|
names = [p["name"] for p in driver.get_packages()]
|
||||||
|
assert names == ["docker-ce", "zabbix-agent", "curl"]
|
||||||
|
|
||||||
|
|
||||||
def test_get_packages_apt(driver):
|
def test_get_packages_apt(driver):
|
||||||
driver._pkg_manager = "apt"
|
driver._pkg_manager = "apt"
|
||||||
@@ -286,7 +301,7 @@ def test_get_packages_apt_falls_back_when_dpkg_gives_no_source(driver):
|
|||||||
"""`source:Package` is empty for a package whose source name equals its own.
|
"""`source:Package` is empty for a package whose source name equals its own.
|
||||||
Older dpkg builds leave `source:Version` empty in that case too."""
|
Older dpkg builds leave `source:Version` empty in that case too."""
|
||||||
driver._pkg_manager = "apt"
|
driver._pkg_manager = "apt"
|
||||||
with patch.object(driver, "_send", return_value="curl\t7.88.1-10\t1024\t\t\ttool\n"):
|
with patch.object(driver, "_send", return_value="ii \tcurl\t7.88.1-10\t1024\t\t\ttool\n"):
|
||||||
(pkg,) = driver.get_packages()
|
(pkg,) = driver.get_packages()
|
||||||
|
|
||||||
assert pkg["source_package"] == "curl"
|
assert pkg["source_package"] == "curl"
|
||||||
@@ -376,6 +391,141 @@ def test_ping_parses_output(driver):
|
|||||||
assert result["success"]["rtt_avg"] == pytest.approx(12.1)
|
assert result["success"]["rtt_avg"] == pytest.approx(12.1)
|
||||||
|
|
||||||
|
|
||||||
|
#: BusyBox ping (Alpine 3.22), IPv4: "packets received", "seq=", no mdev (#15).
|
||||||
|
PING_BUSYBOX = """\
|
||||||
|
PING 192.0.2.1 (192.0.2.1): 100 data bytes
|
||||||
|
108 bytes from 192.0.2.1: seq=0 ttl=64 time=0.412 ms
|
||||||
|
108 bytes from 192.0.2.1: seq=1 ttl=64 time=0.388 ms
|
||||||
|
|
||||||
|
--- 192.0.2.1 ping statistics ---
|
||||||
|
2 packets transmitted, 2 packets received, 0% packet loss
|
||||||
|
round-trip min/avg/max = 0.388/0.400/0.412 ms
|
||||||
|
"""
|
||||||
|
|
||||||
|
#: BusyBox ping, IPv6 link-local.
|
||||||
|
PING_BUSYBOX_V6 = """\
|
||||||
|
PING fe80::1%eth0 (fe80::1%eth0): 100 data bytes
|
||||||
|
108 bytes from fe80::1%eth0: seq=0 ttl=64 time=0.511 ms
|
||||||
|
|
||||||
|
--- fe80::1%eth0 ping statistics ---
|
||||||
|
2 packets transmitted, 1 packets received, 50% packet loss
|
||||||
|
round-trip min/avg/max = 0.511/0.511/0.511 ms
|
||||||
|
"""
|
||||||
|
|
||||||
|
#: iputils ping (Debian 13), IPv6.
|
||||||
|
PING_IPUTILS_V6 = """\
|
||||||
|
PING 2001:db8::53 (2001:db8::53) 100 data bytes
|
||||||
|
108 bytes from 2001:db8::53: icmp_seq=1 ttl=58 time=10.5 ms
|
||||||
|
108 bytes from 2001:db8::53: icmp_seq=2 ttl=58 time=10.9 ms
|
||||||
|
|
||||||
|
--- 2001:db8::53 ping statistics ---
|
||||||
|
2 packets transmitted, 2 received, 0% packet loss, time 1002ms
|
||||||
|
rtt min/avg/max/mdev = 10.500/10.700/10.900/0.200 ms
|
||||||
|
"""
|
||||||
|
|
||||||
|
#: iputils ping to a name: the reply names the host, the address in brackets;
|
||||||
|
#: unreachable probes add an "errors" field to the summary.
|
||||||
|
PING_IPUTILS_NAME = """\
|
||||||
|
PING gw.example.net (192.0.2.1) 100(128) bytes of data.
|
||||||
|
108 bytes from gw.example.net (192.0.2.1): icmp_seq=1 ttl=64 time=0.301 ms
|
||||||
|
From 192.0.2.254 icmp_seq=2 Destination Host Unreachable
|
||||||
|
|
||||||
|
--- gw.example.net ping statistics ---
|
||||||
|
2 packets transmitted, 1 received, +1 errors, 50% packet loss, time 1001ms
|
||||||
|
rtt min/avg/max/mdev = 0.301/0.301/0.301/0.000 ms
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
class TestPingFormats:
|
||||||
|
"""``ping`` reads BusyBox's and iputils' output, IPv4 and IPv6 (#15)."""
|
||||||
|
|
||||||
|
def _ping(self, driver, output):
|
||||||
|
with patch.object(driver, "_send", return_value=output):
|
||||||
|
return driver.ping("192.0.2.1", count=2)["success"]
|
||||||
|
|
||||||
|
def test_busybox_summary_and_rtt(self, driver):
|
||||||
|
reply = self._ping(driver, PING_BUSYBOX)
|
||||||
|
|
||||||
|
assert (reply["probes_sent"], reply["packet_loss"]) == (2, 0)
|
||||||
|
assert (reply["rtt_min"], reply["rtt_avg"], reply["rtt_max"]) == (0.388, 0.4, 0.412)
|
||||||
|
assert reply["rtt_stddev"] == 0.0
|
||||||
|
|
||||||
|
def test_busybox_probes(self, driver):
|
||||||
|
reply = self._ping(driver, PING_BUSYBOX)
|
||||||
|
|
||||||
|
assert reply["results"] == [
|
||||||
|
{"ip_address": "192.0.2.1", "rtt": 0.412},
|
||||||
|
{"ip_address": "192.0.2.1", "rtt": 0.388},
|
||||||
|
]
|
||||||
|
|
||||||
|
def test_busybox_ipv6(self, driver):
|
||||||
|
reply = self._ping(driver, PING_BUSYBOX_V6)
|
||||||
|
|
||||||
|
assert (reply["probes_sent"], reply["packet_loss"]) == (2, 1)
|
||||||
|
assert reply["results"] == [{"ip_address": "fe80::1%eth0", "rtt": 0.511}]
|
||||||
|
|
||||||
|
def test_iputils_ipv6(self, driver):
|
||||||
|
reply = self._ping(driver, PING_IPUTILS_V6)
|
||||||
|
|
||||||
|
assert reply["results"] == [
|
||||||
|
{"ip_address": "2001:db8::53", "rtt": 10.5},
|
||||||
|
{"ip_address": "2001:db8::53", "rtt": 10.9},
|
||||||
|
]
|
||||||
|
assert (reply["rtt_avg"], reply["rtt_stddev"]) == (10.7, 0.2)
|
||||||
|
|
||||||
|
def test_iputils_to_a_name_with_errors(self, driver):
|
||||||
|
reply = self._ping(driver, PING_IPUTILS_NAME)
|
||||||
|
|
||||||
|
assert (reply["probes_sent"], reply["packet_loss"]) == (2, 1)
|
||||||
|
assert reply["results"] == [{"ip_address": "192.0.2.1", "rtt": 0.301}]
|
||||||
|
|
||||||
|
def test_no_summary_is_an_error(self, driver):
|
||||||
|
with patch.object(driver, "_send", return_value="ping: unknown host nosuch"):
|
||||||
|
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"
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
("destination", "source"), [("-f", ""), ("192.0.2.1", "-f"), ("-p ff", "")]
|
||||||
|
)
|
||||||
|
def test_an_option_in_place_of_a_name_is_refused_before_anything_is_sent(
|
||||||
|
self, driver, destination, source
|
||||||
|
):
|
||||||
|
"""Quoting keeps a word one word, and ping still reads ``-f`` as its option (#63)."""
|
||||||
|
with patch.object(driver, "_send") as send, pytest.raises(ValueError, match="option"):
|
||||||
|
driver.ping(destination, source=source)
|
||||||
|
|
||||||
|
send.assert_not_called()
|
||||||
|
|
||||||
|
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
|
# apply_updates – input validation
|
||||||
# ---------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------
|
||||||
@@ -1253,6 +1403,51 @@ class TestUninstallExitStatus:
|
|||||||
assert result["success"] is False
|
assert result["success"] is False
|
||||||
|
|
||||||
|
|
||||||
|
class TestInstallExitStatus:
|
||||||
|
"""``install_package`` decides by the exit status too (#4), as removal does
|
||||||
|
since netork#267: keywords in the output were guesswork in both directions."""
|
||||||
|
|
||||||
|
def test_a_non_zero_exit_is_a_failure_whatever_the_output_says(self, driver):
|
||||||
|
driver._pkg_manager = "dnf"
|
||||||
|
_mock_send(driver, _with_rc("Last metadata expiration check: 0:01:02 ago.", 1))
|
||||||
|
|
||||||
|
result = driver.install_package("curl")
|
||||||
|
|
||||||
|
assert result["success"] is False
|
||||||
|
|
||||||
|
def test_a_zero_exit_is_a_success_even_if_the_output_mentions_failure(self, driver):
|
||||||
|
"""A postinst that cannot start a unit in a container says "Failed" and
|
||||||
|
apt still installs the package."""
|
||||||
|
_mock_send(
|
||||||
|
driver,
|
||||||
|
_with_rc(
|
||||||
|
"Setting up nginx (1.26.3-3) ...\n"
|
||||||
|
"Failed to start nginx.service: System has not been booted with systemd.",
|
||||||
|
0,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
result = driver.install_package("nginx")
|
||||||
|
|
||||||
|
assert result["success"] is True
|
||||||
|
|
||||||
|
def test_the_install_command_keeps_its_exit_status(self, driver):
|
||||||
|
_mock_send(driver, _with_rc("Setting up curl ...", 0))
|
||||||
|
|
||||||
|
result = driver.install_package("curl")
|
||||||
|
|
||||||
|
sent = driver._device.send_command.call_args[0][0]
|
||||||
|
assert "|| true" not in sent
|
||||||
|
assert result["output"] == "Setting up curl ..."
|
||||||
|
|
||||||
|
def test_without_an_exit_status_the_output_is_read(self, driver):
|
||||||
|
_mock_send(driver, _with_rc("E: Unable to locate package nosuchpkg", -1))
|
||||||
|
|
||||||
|
result = driver.install_package("nosuchpkg")
|
||||||
|
|
||||||
|
assert result["success"] is False
|
||||||
|
|
||||||
|
|
||||||
# ---------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------
|
||||||
# get_kernel_facts -- the command and its parse live in napalm-device-types
|
# get_kernel_facts -- the command and its parse live in napalm-device-types
|
||||||
# ---------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------
|
||||||
@@ -1386,7 +1581,7 @@ class TestManageService:
|
|||||||
|
|
||||||
driver.manage_service("cron", "enable")
|
driver.manage_service("cron", "enable")
|
||||||
|
|
||||||
assert self._sent(driver)[1].startswith("sudo -n timeout 45 systemctl")
|
assert self._sent(driver)[1].startswith("sudo -n sh -c 'timeout 45 systemctl")
|
||||||
|
|
||||||
def test_a_missing_sudo_password_is_explained(self, driver):
|
def test_a_missing_sudo_password_is_explained(self, driver):
|
||||||
driver._device.send_command.side_effect = [
|
driver._device.send_command.side_effect = [
|
||||||
@@ -1499,7 +1694,7 @@ class TestAvailableUpdates:
|
|||||||
# parse_dnf_security (napalm-device-types 4.7) needs the __DNF_RC marker, and
|
# parse_dnf_security (napalm-device-types 4.7) needs the __DNF_RC marker, and
|
||||||
# behind "| cat" the outer status no longer says whether yum failed.
|
# behind "| cat" the outer status no longer says whether yum failed.
|
||||||
assert "echo __DNF_RC=$?" in _YUM_SECURITY_COMMAND
|
assert "echo __DNF_RC=$?" in _YUM_SECURITY_COMMAND
|
||||||
assert "yum updateinfo list security" in _YUM_SECURITY_COMMAND
|
assert "yum --cacheonly updateinfo list security" in _YUM_SECURITY_COMMAND
|
||||||
|
|
||||||
def test_yum_marks_what_a_security_advisory_covers(self, driver):
|
def test_yum_marks_what_a_security_advisory_covers(self, driver):
|
||||||
driver._pkg_manager = "yum"
|
driver._pkg_manager = "yum"
|
||||||
@@ -1564,6 +1759,32 @@ class TestAvailableUpdates:
|
|||||||
with pytest.raises(RuntimeError):
|
with pytest.raises(RuntimeError):
|
||||||
driver.get_available_updates()
|
driver.get_available_updates()
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("manager", ["dnf", "yum"])
|
||||||
|
def test_rpm_reads_only_the_cached_metadata(self, driver, manager):
|
||||||
|
"""An expired cache made check-update download the repositories' metadata
|
||||||
|
inside the poll, which outlasted its whole budget (#59)."""
|
||||||
|
driver._pkg_manager = manager
|
||||||
|
driver._device.send_command.side_effect = [
|
||||||
|
"0",
|
||||||
|
"__NETORK_RC=0",
|
||||||
|
"__DNF_RC=0\n__NETORK_RC=0",
|
||||||
|
]
|
||||||
|
|
||||||
|
driver.get_available_updates()
|
||||||
|
|
||||||
|
sent = [c[0][0] for c in driver._device.send_command.call_args_list]
|
||||||
|
assert f"{manager} --cacheonly check-update" in sent[1]
|
||||||
|
assert f"{manager} --cacheonly updateinfo" in sent[2]
|
||||||
|
|
||||||
|
def test_rpm_without_a_cache_says_to_refresh_it(self, driver):
|
||||||
|
# dnf --cacheonly fails at once without cached metadata; its stderr is
|
||||||
|
# discarded, so the reason has to come from here.
|
||||||
|
driver._pkg_manager = "dnf"
|
||||||
|
driver._device.send_command.side_effect = ["0", "__NETORK_RC=1"]
|
||||||
|
|
||||||
|
with pytest.raises(RuntimeError, match="refresh the package index"):
|
||||||
|
driver.get_available_updates()
|
||||||
|
|
||||||
|
|
||||||
class TestRefreshAvailableUpdates:
|
class TestRefreshAvailableUpdates:
|
||||||
def _sent(self, driver) -> list:
|
def _sent(self, driver) -> list:
|
||||||
@@ -1591,7 +1812,7 @@ class TestRefreshAvailableUpdates:
|
|||||||
result = driver.refresh_available_updates()
|
result = driver.refresh_available_updates()
|
||||||
|
|
||||||
assert result["success"] is False
|
assert result["success"] is False
|
||||||
assert self._sent(driver)[1].startswith("sudo -n apt-get update")
|
assert self._sent(driver)[1].startswith("sudo -n sh -c 'apt-get update")
|
||||||
|
|
||||||
def test_dnf_refreshes_its_metadata(self, driver):
|
def test_dnf_refreshes_its_metadata(self, driver):
|
||||||
driver._pkg_manager = "dnf"
|
driver._pkg_manager = "dnf"
|
||||||
@@ -1972,3 +2193,349 @@ class TestHardensItsSshServer:
|
|||||||
with patch.object(driver, "run_command", return_value=CommandResult(out, "", 0)) as run:
|
with patch.object(driver, "run_command", return_value=CommandResult(out, "", 0)) as run:
|
||||||
assert driver.get_ssh_server_algorithms()["mac"] == ["d"]
|
assert driver.get_ssh_server_algorithms()["mac"] == ["d"]
|
||||||
assert run.call_args.kwargs["privileged"] is True
|
assert run.call_args.kwargs["privileged"] is True
|
||||||
|
|
||||||
|
|
||||||
|
class TestWireGuardTunnelKeys:
|
||||||
|
"""A tunnel's key is a URL path segment for its consumer (#43): the public
|
||||||
|
key's base64 prefix is made URL-safe, as napalm-opnsense does."""
|
||||||
|
|
||||||
|
_DUMP = (
|
||||||
|
"wg0\tPRIVKEY\tSERVERPUBKEY=\t51820\toff\n"
|
||||||
|
"wg0\tab/c+d9EfGhIjKlMnOpQrStUvWxYz0123456789AB=\t(none)\t"
|
||||||
|
"198.51.100.7:51820\t10.8.0.2/32\t0\t1024\t2048\toff\n"
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_a_slash_or_plus_in_the_key_prefix_is_url_safe(self, driver):
|
||||||
|
_mock_send(driver, self._DUMP)
|
||||||
|
|
||||||
|
tunnels = driver.get_vpn_tunnels()
|
||||||
|
|
||||||
|
assert list(tunnels) == ["wireguard-wg0-ab_c-d9E"]
|
||||||
|
|
||||||
|
def test_the_public_key_itself_is_reported_unchanged(self, driver):
|
||||||
|
_mock_send(driver, self._DUMP)
|
||||||
|
|
||||||
|
(tunnel,) = driver.get_vpn_tunnels().values()
|
||||||
|
|
||||||
|
assert tunnel["public_key"] == "ab/c+d9EfGhIjKlMnOpQrStUvWxYz0123456789AB="
|
||||||
|
|
||||||
|
|
||||||
|
class TestGetProcesses:
|
||||||
|
"""``lstart`` is five words; the split has to leave them together (#21)."""
|
||||||
|
|
||||||
|
_PS = (
|
||||||
|
" 1 0 root 0.0 0.1 23456 12345 ? Ss Tue Oct 6 08:47:15 2026 /sbin/init splash\n"
|
||||||
|
" 1234 1 chris 1.5 2.3 456789 98765 pts/0 S+ Tue Oct 6 09:01:02 2026 bash -c echo two spaces\n"
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_started_is_the_whole_start_time(self, driver):
|
||||||
|
_mock_send(driver, self._PS)
|
||||||
|
|
||||||
|
init, shell = driver.get_processes()
|
||||||
|
|
||||||
|
assert init["started"] == "Tue Oct 6 08:47:15 2026"
|
||||||
|
assert shell["started"] == "Tue Oct 6 09:01:02 2026"
|
||||||
|
|
||||||
|
def test_command_is_the_command_line_alone(self, driver):
|
||||||
|
_mock_send(driver, self._PS)
|
||||||
|
|
||||||
|
init, shell = driver.get_processes()
|
||||||
|
|
||||||
|
assert init["command"] == "/sbin/init splash"
|
||||||
|
assert shell["command"] == "bash -c echo two spaces"
|
||||||
|
|
||||||
|
def test_the_other_columns(self, driver):
|
||||||
|
_mock_send(driver, self._PS)
|
||||||
|
|
||||||
|
shell = driver.get_processes()[1]
|
||||||
|
|
||||||
|
assert (shell["pid"], shell["ppid"], shell["user"], shell["tty"]) == (
|
||||||
|
1234,
|
||||||
|
1,
|
||||||
|
"chris",
|
||||||
|
"pts/0",
|
||||||
|
)
|
||||||
|
assert (shell["cpu"], shell["memory"], shell["vsz"], shell["rss"]) == (
|
||||||
|
1.5,
|
||||||
|
2.3,
|
||||||
|
456789,
|
||||||
|
98765,
|
||||||
|
)
|
||||||
|
assert shell["state"] == "S"
|
||||||
|
assert driver.get_processes()[0]["tty"] == ""
|
||||||
|
|
||||||
|
def test_the_start_time_is_in_c_locale_words(self, driver):
|
||||||
|
"""A German host says "Di Okt" -- one word each too, but only C promises that."""
|
||||||
|
_mock_send(driver, self._PS)
|
||||||
|
|
||||||
|
driver.get_processes()
|
||||||
|
|
||||||
|
assert driver._device.send_command.call_args[0][0].startswith("LC_ALL=C ps ")
|
||||||
|
|
||||||
|
def test_a_line_cut_short_is_skipped(self, driver):
|
||||||
|
_mock_send(driver, " 1 0 root 0.0 0.1 23456 12345 ? Ss Tue Oct 6\n")
|
||||||
|
|
||||||
|
assert driver.get_processes() == []
|
||||||
|
|
||||||
|
|
||||||
|
class TestRouteFamilies:
|
||||||
|
"""``ip -6 route show`` calls its default route ``default`` too (#6)."""
|
||||||
|
|
||||||
|
_V4 = (
|
||||||
|
"default via 192.0.2.1 dev eth0 proto dhcp src 192.0.2.10 metric 100\n"
|
||||||
|
"192.0.2.0/24 dev eth0 proto kernel scope link src 192.0.2.10 metric 100\n"
|
||||||
|
)
|
||||||
|
_V6 = (
|
||||||
|
"2001:db8:1::/64 dev eth0 proto ra metric 100 expires 86391sec pref medium\n"
|
||||||
|
"fe80::/64 dev eth0 proto kernel metric 256 pref medium\n"
|
||||||
|
"default via fe80::1 dev eth0 proto ra metric 100 expires 1791sec pref medium\n"
|
||||||
|
)
|
||||||
|
|
||||||
|
def _routes(self, driver, v6=_V6):
|
||||||
|
def send(command, **_kw):
|
||||||
|
out = ""
|
||||||
|
if "ip -4 route" in command:
|
||||||
|
out += self._V4
|
||||||
|
if "ip -6 route" in command:
|
||||||
|
out += v6
|
||||||
|
return out
|
||||||
|
|
||||||
|
with patch.object(driver, "_send", side_effect=send):
|
||||||
|
return driver.get_route_to()
|
||||||
|
|
||||||
|
def test_the_ipv6_default_route_is_its_own(self, driver):
|
||||||
|
routes = self._routes(driver)
|
||||||
|
|
||||||
|
assert [r["family"] for r in routes["::/0"]] == ["ipv6"]
|
||||||
|
assert routes["::/0"][0]["next_hop"] == "fe80::1"
|
||||||
|
|
||||||
|
def test_the_ipv4_default_route_stays_ipv4(self, driver):
|
||||||
|
routes = self._routes(driver)
|
||||||
|
|
||||||
|
assert [r["family"] for r in routes["0.0.0.0/0"]] == ["ipv4"]
|
||||||
|
assert routes["0.0.0.0/0"][0]["next_hop"] == "192.0.2.1"
|
||||||
|
|
||||||
|
def test_an_ipv6_host_route_is_a_128(self, driver):
|
||||||
|
routes = self._routes(
|
||||||
|
driver, v6="2001:db8:2::7 via fe80::1 dev eth0 proto static metric 1024\n"
|
||||||
|
)
|
||||||
|
|
||||||
|
assert "2001:db8:2::7/128" in routes
|
||||||
|
|
||||||
|
|
||||||
|
#: ``ip -4 route show`` / ``ip -6 route show``, iproute2 6.1.0 on Linux 7.0 (Linux Mint
|
||||||
|
#: 22.3), recorded in a network namespace set up for it: a multipath default per
|
||||||
|
#: family, and unreachable, prohibit, throw and blackhole routes (#57). Documentation
|
||||||
|
#: addresses only. The trailing blanks and the tabs are ip's.
|
||||||
|
_IP4_ROUTE_TYPES = (
|
||||||
|
"default proto static metric 100 \n"
|
||||||
|
"\tnexthop via 192.0.2.1 dev eth0 weight 1 \n"
|
||||||
|
"\tnexthop via 198.51.100.1 dev eth1 weight 1 \n"
|
||||||
|
"unreachable 10.0.0.0/8 metric 1024 \n"
|
||||||
|
"prohibit 172.16.0.0/12 \n"
|
||||||
|
"192.0.2.0/24 dev eth0 proto kernel scope link src 192.0.2.10 \n"
|
||||||
|
"throw 192.168.0.0/16 \n"
|
||||||
|
"198.51.100.0/24 dev eth1 proto kernel scope link src 198.51.100.10 \n"
|
||||||
|
"blackhole 203.0.113.0/24 proto static \n"
|
||||||
|
"203.0.113.7 via 192.0.2.1 dev eth0 \n"
|
||||||
|
)
|
||||||
|
_IP6_ROUTE_TYPES = (
|
||||||
|
"2001:db8:1::/64 dev eth0 proto kernel metric 256 pref medium\n"
|
||||||
|
"2001:db8:2::/64 dev eth1 proto kernel metric 256 pref medium\n"
|
||||||
|
"blackhole 2001:db8:beef::/48 dev lo metric 1024 pref medium\n"
|
||||||
|
"unreachable 2001:db8:dead::/48 dev lo metric 1024 pref medium\n"
|
||||||
|
"fe80::/64 dev eth0 proto kernel metric 256 pref medium\n"
|
||||||
|
"fe80::/64 dev eth1 proto kernel metric 256 pref medium\n"
|
||||||
|
"default proto static metric 1024 pref medium\n"
|
||||||
|
"\tnexthop via 2001:db8:1::1 dev eth0 weight 1 \n"
|
||||||
|
"\tnexthop via 2001:db8:2::1 dev eth1 weight 1 \n"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class TestRouteTypesAndMultipath:
|
||||||
|
"""A route type in front of the destination is the type, and a multipath
|
||||||
|
route's ``nexthop`` lines are its next hops (#57)."""
|
||||||
|
|
||||||
|
@pytest.fixture()
|
||||||
|
def routes(self, driver):
|
||||||
|
def send(command, **_kw):
|
||||||
|
if "ip -4 route" in command:
|
||||||
|
return _IP4_ROUTE_TYPES
|
||||||
|
if "ip -6 route" in command:
|
||||||
|
return _IP6_ROUTE_TYPES
|
||||||
|
return ""
|
||||||
|
|
||||||
|
with patch.object(driver, "_send", side_effect=send):
|
||||||
|
return driver.get_route_to()
|
||||||
|
|
||||||
|
def test_every_destination_is_an_address(self, routes):
|
||||||
|
assert sorted(routes) == sorted(
|
||||||
|
[
|
||||||
|
"0.0.0.0/0",
|
||||||
|
"10.0.0.0/8",
|
||||||
|
"172.16.0.0/12",
|
||||||
|
"192.0.2.0/24",
|
||||||
|
"192.168.0.0/16",
|
||||||
|
"198.51.100.0/24",
|
||||||
|
"203.0.113.0/24",
|
||||||
|
"203.0.113.7/32",
|
||||||
|
"2001:db8:1::/64",
|
||||||
|
"2001:db8:2::/64",
|
||||||
|
"2001:db8:beef::/48",
|
||||||
|
"2001:db8:dead::/48",
|
||||||
|
"fe80::/64",
|
||||||
|
"::/0",
|
||||||
|
]
|
||||||
|
)
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
("network", "route_type", "metric"),
|
||||||
|
[
|
||||||
|
("10.0.0.0/8", "unreachable", 1024),
|
||||||
|
("172.16.0.0/12", "prohibit", 0),
|
||||||
|
("192.168.0.0/16", "throw", 0),
|
||||||
|
("203.0.113.0/24", "blackhole", 0),
|
||||||
|
("2001:db8:beef::/48", "blackhole", 1024),
|
||||||
|
("2001:db8:dead::/48", "unreachable", 1024),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
def test_a_route_that_forwards_nothing_says_its_type(self, routes, network, route_type, metric):
|
||||||
|
(entry,) = routes[network]
|
||||||
|
|
||||||
|
assert entry["protocol_attributes"] == {"type": route_type}
|
||||||
|
assert entry["preference"] == metric
|
||||||
|
# Nothing leaves through an interface; IPv6 names lo for it.
|
||||||
|
assert (entry["next_hop"], entry["outgoing_interface"]) == ("", "")
|
||||||
|
|
||||||
|
def test_the_rest_of_a_typed_line_is_read_as_before(self, routes):
|
||||||
|
assert routes["203.0.113.0/24"][0]["protocol"] == "static"
|
||||||
|
|
||||||
|
def test_a_unicast_route_has_no_type(self, routes):
|
||||||
|
assert routes["192.0.2.0/24"][0]["protocol_attributes"] == {}
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
("network", "hops"),
|
||||||
|
[
|
||||||
|
("0.0.0.0/0", [("192.0.2.1", "eth0"), ("198.51.100.1", "eth1")]),
|
||||||
|
("::/0", [("2001:db8:1::1", "eth0"), ("2001:db8:2::1", "eth1")]),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
def test_a_multipath_route_has_one_entry_per_next_hop(self, routes, network, hops):
|
||||||
|
entries = routes[network]
|
||||||
|
|
||||||
|
assert [(e["next_hop"], e["outgoing_interface"]) for e in entries] == hops
|
||||||
|
assert {(e["protocol"], e["preference"]) for e in entries} == {
|
||||||
|
("static", 100 if network == "0.0.0.0/0" else 1024)
|
||||||
|
}
|
||||||
|
|
||||||
|
def test_no_next_hop_line_is_a_route_of_its_own(self, routes):
|
||||||
|
assert not any(network.startswith("nexthop") for network in routes)
|
||||||
|
|
||||||
|
|
||||||
|
class TestParseIpRoute:
|
||||||
|
"""``parse_ip_route`` line by line (#57)."""
|
||||||
|
|
||||||
|
def test_a_typed_route_that_forwards_keeps_its_interface(self):
|
||||||
|
# ``ip route show table local``: a local route names the interface it is on.
|
||||||
|
line = "local 192.0.2.10 dev eth0 proto kernel scope host src 192.0.2.10\n"
|
||||||
|
|
||||||
|
assert list(parse_ip_route(line, "ipv4")) == [
|
||||||
|
IpRoute("192.0.2.10/32", "kernel", "", "eth0", 0, "local")
|
||||||
|
]
|
||||||
|
|
||||||
|
def test_a_next_hop_line_without_a_route_is_dropped(self):
|
||||||
|
out = "\tnexthop via 192.0.2.1 dev eth0 weight 1\n192.0.2.0/24 dev eth0 proto kernel\n"
|
||||||
|
|
||||||
|
assert [r.network for r in parse_ip_route(out, "ipv4")] == ["192.0.2.0/24"]
|
||||||
|
|
||||||
|
def test_a_route_without_next_hop_lines_is_one_route(self):
|
||||||
|
out = "default via 192.0.2.1 dev eth0 proto dhcp metric 100\n"
|
||||||
|
|
||||||
|
assert list(parse_ip_route(out, "ipv4")) == [
|
||||||
|
IpRoute("0.0.0.0/0", "dhcp", "192.0.2.1", "eth0", 100)
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
class TestFrrHostRoutes:
|
||||||
|
"""The FRR enrichment completes a prefix without a length by its family (#64)."""
|
||||||
|
|
||||||
|
# FRR installs its routes as "proto zebra"; vtysh names the real source.
|
||||||
|
_IP6 = "2001:db8:2::7 via fe80::1 dev eth0 proto zebra metric 20 pref medium\n"
|
||||||
|
_VTYSH = (
|
||||||
|
"O>* 2001:db8:2::7 [110/20] via fe80::1, eth0, weight 1, 00:01:02\n"
|
||||||
|
"O>* 2001:db8:3::9 [110/30] via fe80::1, eth0, weight 1, 00:01:02\n"
|
||||||
|
"O>* 10.10.0.7 [110/20] via 10.255.255.2, wg0, weight 1, 00:01:02\n"
|
||||||
|
"O>* 10.10.1.0/24 [110/20] via 10.255.255.2, wg0, weight 1, 00:01:02\n"
|
||||||
|
)
|
||||||
|
|
||||||
|
@pytest.fixture()
|
||||||
|
def routes(self, driver):
|
||||||
|
def send(command, **_kw):
|
||||||
|
if command.startswith("command -v vtysh"):
|
||||||
|
return "/usr/bin/vtysh"
|
||||||
|
if command.startswith("vtysh"):
|
||||||
|
return self._VTYSH
|
||||||
|
if "ip -6 route" in command:
|
||||||
|
return self._IP6
|
||||||
|
return ""
|
||||||
|
|
||||||
|
with patch.object(driver, "_send", side_effect=send):
|
||||||
|
return driver.get_route_to()
|
||||||
|
|
||||||
|
def test_an_ipv6_host_route_is_a_128(self, routes):
|
||||||
|
assert "2001:db8:3::9/128" in routes
|
||||||
|
assert not any(
|
||||||
|
network.endswith("::7/32") or network.endswith("::9/32") for network in routes
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_it_meets_the_kernel_route_of_the_same_host(self, routes):
|
||||||
|
assert [e["protocol"] for e in routes["2001:db8:2::7/128"]] == ["ospf"]
|
||||||
|
|
||||||
|
def test_an_ipv4_host_route_stays_a_32(self, routes):
|
||||||
|
assert "10.10.0.7/32" in routes
|
||||||
|
|
||||||
|
def test_a_prefix_with_a_length_keeps_it(self, routes):
|
||||||
|
assert "10.10.1.0/24" in routes
|
||||||
|
|
||||||
|
|
||||||
|
#: ``ip -4 route show``, iproute2 6.1.0 on Linux 7.0 (Linux Mint 22.3), recorded in a
|
||||||
|
#: network namespace set up for it: IPv4 routes with IPv6 next hops (RFC 8950), single
|
||||||
|
#: and multipath, as BGP unnumbered installs them (#65). Documentation addresses only.
|
||||||
|
_IP4_VIA_INET6 = (
|
||||||
|
"default via inet6 2001:db8:1::1 dev eth0 \n"
|
||||||
|
"10.20.0.0/16 proto bgp metric 20 \n"
|
||||||
|
"\tnexthop via inet6 2001:db8:1::1 dev eth0 weight 1 \n"
|
||||||
|
"\tnexthop via inet6 2001:db8:2::1 dev eth1 weight 1 \n"
|
||||||
|
"10.40.0.0/16 via inet6 2001:db8:1::1 dev eth0 proto bgp metric 20 \n"
|
||||||
|
"192.0.2.0/24 dev eth0 proto kernel scope link src 192.0.2.10 \n"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class TestIpv6NextHops:
|
||||||
|
"""``via inet6 <address>``: the next hop is the address (#65)."""
|
||||||
|
|
||||||
|
@pytest.fixture()
|
||||||
|
def routes(self, driver):
|
||||||
|
def send(command, **_kw):
|
||||||
|
return _IP4_VIA_INET6 if "ip -4 route" in command else ""
|
||||||
|
|
||||||
|
with patch.object(driver, "_send", side_effect=send):
|
||||||
|
return driver.get_route_to()
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("network", ["0.0.0.0/0", "10.40.0.0/16"])
|
||||||
|
def test_the_next_hop_is_the_address(self, routes, network):
|
||||||
|
assert [e["next_hop"] for e in routes[network]] == ["2001:db8:1::1"]
|
||||||
|
|
||||||
|
def test_a_multipath_route_has_each_address(self, routes):
|
||||||
|
hops = [(e["next_hop"], e["outgoing_interface"]) for e in routes["10.20.0.0/16"]]
|
||||||
|
|
||||||
|
assert hops == [("2001:db8:1::1", "eth0"), ("2001:db8:2::1", "eth1")]
|
||||||
|
|
||||||
|
def test_the_route_stays_ipv4(self, routes):
|
||||||
|
"""The family is the destination's; an IPv6 next hop does not change it."""
|
||||||
|
assert {e["family"] for entries in routes.values() for e in entries} == {"ipv4"}
|
||||||
|
|
||||||
|
def test_a_family_word_for_ipv4_is_skipped_too(self):
|
||||||
|
out = "10.50.0.0/16 via inet 192.0.2.1 dev eth0\n"
|
||||||
|
|
||||||
|
assert [r.nexthop for r in parse_ip_route(out, "ipv4")] == ["192.0.2.1"]
|
||||||
@@ -0,0 +1,232 @@
|
|||||||
|
"""get_lldp_neighbors: every neighbour of a port, its chassis MAC and its port (#58).
|
||||||
|
|
||||||
|
lldpd's ``lldpctl -f keyvalue`` is parsed by napalm-device-types'
|
||||||
|
``parse_lldpctl_keyvalue``. Before, the driver grouped the lines by interface,
|
||||||
|
so a second neighbour on a port overwrote the first; it looked for
|
||||||
|
``chassis.id.subtype``/``chassis.id.value`` and ``port.id.value``, keys lldpd
|
||||||
|
never prints, so the chassis MAC was never reported and a port given by MAC or
|
||||||
|
local id came back empty; and it cut ``eth0.100`` at its dot.
|
||||||
|
|
||||||
|
#66: the running check took systemctl's ``inactive`` for running, and a failed
|
||||||
|
lldpctl read as no neighbours.
|
||||||
|
"""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import subprocess
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
from napalm_device_types import LLDPCTL_KEYVALUE_COMMAND
|
||||||
|
|
||||||
|
from napalm_linux.linux import LinuxDriver
|
||||||
|
|
||||||
|
_RC = "__LLDPCTL_RC"
|
||||||
|
|
||||||
|
# Laid out the way lldpd's keyvalue writer prints ``show neighbors details``
|
||||||
|
# (src/client/display.c, kv_writer.c): every neighbour opens with ``via``, an id
|
||||||
|
# is printed under its type. Not recorded from a device. On eth0 a phone and the
|
||||||
|
# PC behind it (an unmanaged switch between them and the host), on the VLAN
|
||||||
|
# interface eth0.100 a switch that names its port by a local id.
|
||||||
|
LLDPCTL_KEYVALUE = """\
|
||||||
|
lldp.eth0.via=LLDP
|
||||||
|
lldp.eth0.rid=1
|
||||||
|
lldp.eth0.age=0 day, 00:10:00
|
||||||
|
lldp.eth0.chassis.mac=00:11:22:33:44:AA
|
||||||
|
lldp.eth0.chassis.name=desk-phone
|
||||||
|
lldp.eth0.chassis.descr=SIP phone
|
||||||
|
lldp.eth0.chassis.Tel.enabled=on
|
||||||
|
lldp.eth0.port.mac=00:11:22:33:44:aa
|
||||||
|
lldp.eth0.port.descr=WAN
|
||||||
|
lldp.eth0.port.ttl=120
|
||||||
|
lldp.eth0.via=LLDP
|
||||||
|
lldp.eth0.rid=2
|
||||||
|
lldp.eth0.age=0 day, 00:09:00
|
||||||
|
lldp.eth0.chassis.mac=00:11:22:33:44:bb
|
||||||
|
lldp.eth0.chassis.name=desk-pc
|
||||||
|
lldp.eth0.chassis.Station.enabled=on
|
||||||
|
lldp.eth0.port.ifname=eno1
|
||||||
|
lldp.eth0.port.ttl=120
|
||||||
|
lldp.eth0.100.via=LLDP
|
||||||
|
lldp.eth0.100.rid=3
|
||||||
|
lldp.eth0.100.age=1 day, 02:00:00
|
||||||
|
lldp.eth0.100.chassis.local=access-sw-01
|
||||||
|
lldp.eth0.100.chassis.name=access-sw
|
||||||
|
lldp.eth0.100.chassis.Bridge.enabled=on
|
||||||
|
lldp.eth0.100.port.local=gi1/0/7
|
||||||
|
lldp.eth0.100.port.descr=GigabitEthernet1/0/7
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
#: Shell stubs for the host's lldpd state: ``systemctl`` and ``service`` as a
|
||||||
|
#: systemd host answers them, ``pgrep`` as procps does.
|
||||||
|
_RUNNING = (
|
||||||
|
"systemctl() { case $* in *--quiet*) ;; *) echo active ;; esac; return 0; }; "
|
||||||
|
"service() { echo ' Active: active (running)'; return 0; }; "
|
||||||
|
"pgrep() { return 0; }; "
|
||||||
|
)
|
||||||
|
_STOPPED = (
|
||||||
|
"systemctl() { case $* in *--quiet*) ;; *) echo inactive ;; esac; return 3; }; "
|
||||||
|
"service() { echo ' Active: inactive (dead)'; return 3; }; "
|
||||||
|
"pgrep() { return 1; }; "
|
||||||
|
)
|
||||||
|
_FAILED = (
|
||||||
|
"systemctl() { case $* in *--quiet*) ;; *) echo failed ;; esac; return 3; }; "
|
||||||
|
"service() { echo ' Active: failed (Result: exit-code)'; return 3; }; "
|
||||||
|
"pgrep() { return 1; }; "
|
||||||
|
)
|
||||||
|
#: No systemd, lldpd started by its init script: only pgrep finds it.
|
||||||
|
_RUNNING_WITHOUT_SYSTEMD = (
|
||||||
|
"systemctl() { return 127; }; service() { return 1; }; pgrep() { return 0; }; "
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def _shell(stubs: str, command: str) -> str:
|
||||||
|
"""What *command* prints in ``sh`` with the state commands stubbed out."""
|
||||||
|
done = subprocess.run( # a local sh, no network
|
||||||
|
["sh", "-c", stubs + command], capture_output=True, text=True, check=False, timeout=10
|
||||||
|
)
|
||||||
|
return done.stdout.strip()
|
||||||
|
|
||||||
|
|
||||||
|
def _answer(
|
||||||
|
driver: LinuxDriver,
|
||||||
|
lldpctl: str,
|
||||||
|
*,
|
||||||
|
installed: bool = True,
|
||||||
|
state: str = _RUNNING,
|
||||||
|
lldpctl_error: str = "",
|
||||||
|
lldpctl_status: int = 0,
|
||||||
|
):
|
||||||
|
"""Answer ``_send`` the way the host's shell would.
|
||||||
|
|
||||||
|
lldpctl prints *lldpctl* on stdout, *lldpctl_error* on stderr and exits with
|
||||||
|
*lldpctl_status*; the redirections and the status echo of the command sent
|
||||||
|
decide what of that comes back.
|
||||||
|
"""
|
||||||
|
sent: list[str] = []
|
||||||
|
|
||||||
|
def send(command: str, read_timeout: float = 100) -> str:
|
||||||
|
sent.append(command)
|
||||||
|
if command.startswith("command -v lldpctl"):
|
||||||
|
return "/usr/sbin/lldpctl" if installed else ""
|
||||||
|
if "lldpd" in command and "lldpctl" not in command:
|
||||||
|
return _shell(state, command)
|
||||||
|
if command.startswith("lldpctl"):
|
||||||
|
out = lldpctl
|
||||||
|
if "2>&1" in command:
|
||||||
|
out += lldpctl_error
|
||||||
|
if f"echo {_RC}" in command:
|
||||||
|
out += f"\n{_RC}={lldpctl_status}"
|
||||||
|
return out.strip()
|
||||||
|
raise AssertionError(f"unexpected command {command!r}")
|
||||||
|
|
||||||
|
driver._send = send # type: ignore[method-assign]
|
||||||
|
return sent
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture()
|
||||||
|
def driver() -> LinuxDriver:
|
||||||
|
return LinuxDriver.__new__(LinuxDriver)
|
||||||
|
|
||||||
|
|
||||||
|
class TestNeighbours:
|
||||||
|
def test_every_neighbour_of_a_port_is_reported(self, driver):
|
||||||
|
_answer(driver, LLDPCTL_KEYVALUE)
|
||||||
|
|
||||||
|
result = driver.get_lldp_neighbors()
|
||||||
|
|
||||||
|
assert [n["hostname"] for n in result["eth0"]] == ["desk-phone", "desk-pc"]
|
||||||
|
|
||||||
|
def test_the_chassis_mac_is_reported_in_lower_case(self, driver):
|
||||||
|
_answer(driver, LLDPCTL_KEYVALUE)
|
||||||
|
|
||||||
|
phone, pc = driver.get_lldp_neighbors()["eth0"]
|
||||||
|
|
||||||
|
assert phone["mac"] == "00:11:22:33:44:aa"
|
||||||
|
assert pc["mac"] == "00:11:22:33:44:bb"
|
||||||
|
|
||||||
|
def test_a_port_given_by_mac_or_local_id_is_reported(self, driver):
|
||||||
|
_answer(driver, LLDPCTL_KEYVALUE)
|
||||||
|
|
||||||
|
result = driver.get_lldp_neighbors()
|
||||||
|
|
||||||
|
assert result["eth0"][0]["port"] == "00:11:22:33:44:aa"
|
||||||
|
assert result["eth0"][1]["port"] == "eno1"
|
||||||
|
assert result["eth0.100"][0]["port"] == "gi1/0/7"
|
||||||
|
|
||||||
|
def test_a_dotted_interface_name_stays_whole(self, driver):
|
||||||
|
_answer(driver, LLDPCTL_KEYVALUE)
|
||||||
|
|
||||||
|
assert sorted(driver.get_lldp_neighbors()) == ["eth0", "eth0.100"]
|
||||||
|
|
||||||
|
def test_a_chassis_not_named_by_a_mac_has_no_mac(self, driver):
|
||||||
|
_answer(driver, LLDPCTL_KEYVALUE)
|
||||||
|
|
||||||
|
assert driver.get_lldp_neighbors()["eth0.100"] == [
|
||||||
|
{"hostname": "access-sw", "port": "gi1/0/7"}
|
||||||
|
]
|
||||||
|
|
||||||
|
def test_a_chassis_mac_that_is_no_mac_address_is_left_out(self, driver):
|
||||||
|
_answer(driver, "lldp.eth0.via=LLDP\nlldp.eth0.chassis.mac=00:11\n")
|
||||||
|
|
||||||
|
assert driver.get_lldp_neighbors() == {"eth0": [{"hostname": "", "port": ""}]}
|
||||||
|
|
||||||
|
|
||||||
|
class TestWithoutLldpd:
|
||||||
|
def test_without_lldpctl_there_are_no_neighbours(self, driver):
|
||||||
|
sent = _answer(driver, LLDPCTL_KEYVALUE, installed=False)
|
||||||
|
|
||||||
|
assert driver.get_lldp_neighbors() == {}
|
||||||
|
assert not any(c.startswith("lldpctl") for c in sent)
|
||||||
|
|
||||||
|
def test_lldpd_without_neighbours_lists_none(self, driver):
|
||||||
|
_answer(driver, "")
|
||||||
|
|
||||||
|
assert driver.get_lldp_neighbors() == {}
|
||||||
|
|
||||||
|
|
||||||
|
class TestLldpdState:
|
||||||
|
"""Only a running lldpd is asked; a stopped one is no neighbours (#66)."""
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("state", [_STOPPED, _FAILED], ids=["inactive", "failed"])
|
||||||
|
def test_a_stopped_lldpd_is_not_asked(self, driver, state):
|
||||||
|
sent = _answer(driver, LLDPCTL_KEYVALUE, state=state)
|
||||||
|
|
||||||
|
assert driver.get_lldp_neighbors() == {}
|
||||||
|
assert not any(c.startswith("lldpctl") for c in sent)
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
"state", [_RUNNING, _RUNNING_WITHOUT_SYSTEMD], ids=["systemd", "without-systemd"]
|
||||||
|
)
|
||||||
|
def test_a_running_lldpd_is_asked(self, driver, state):
|
||||||
|
_answer(driver, LLDPCTL_KEYVALUE, state=state)
|
||||||
|
|
||||||
|
assert sorted(driver.get_lldp_neighbors()) == ["eth0", "eth0.100"]
|
||||||
|
|
||||||
|
|
||||||
|
class TestFailedRead:
|
||||||
|
"""With lldpd running, a failed lldpctl raises instead of reading as none (#66)."""
|
||||||
|
|
||||||
|
def test_lldpctl_is_asked_with_its_exit_status(self, driver):
|
||||||
|
sent = _answer(driver, LLDPCTL_KEYVALUE)
|
||||||
|
|
||||||
|
driver.get_lldp_neighbors()
|
||||||
|
|
||||||
|
assert sent[-1] == LLDPCTL_KEYVALUE_COMMAND
|
||||||
|
|
||||||
|
def test_an_unreachable_lldpd_raises(self, driver):
|
||||||
|
_answer(
|
||||||
|
driver,
|
||||||
|
"",
|
||||||
|
lldpctl_error="[WARN/control] unable to connect to socket /run/lldpd.socket: "
|
||||||
|
"Permission denied",
|
||||||
|
lldpctl_status=1,
|
||||||
|
)
|
||||||
|
|
||||||
|
with pytest.raises(ValueError, match="Permission denied"):
|
||||||
|
driver.get_lldp_neighbors()
|
||||||
|
|
||||||
|
def test_lldpctl_without_neighbours_lists_none(self, driver):
|
||||||
|
_answer(driver, "")
|
||||||
|
|
||||||
|
assert driver.get_lldp_neighbors() == {}
|
||||||
@@ -0,0 +1,110 @@
|
|||||||
|
"""Personal accounts on a Linux host (NetOrk/netork#1793).
|
||||||
|
|
||||||
|
napalm-device-types' ``LocalAccountsMixin`` runs the steps; the driver names
|
||||||
|
shadow-utils' commands. A locked account is an expired one (``usermod -e 1``):
|
||||||
|
PAM refuses it a key login too, where a ``!`` in front of the password alone
|
||||||
|
stops only the password.
|
||||||
|
"""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import time
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
from napalm_device_types import CommandResult, LocalAccountsMixin
|
||||||
|
|
||||||
|
from napalm_linux import LinuxDriver
|
||||||
|
|
||||||
|
HASH = "$6$salt$" + "x" * 86
|
||||||
|
_TODAY = int(time.time() // 86400)
|
||||||
|
|
||||||
|
|
||||||
|
class _Driver(LinuxDriver):
|
||||||
|
"""A Linux driver whose host answers ``getent shadow`` with *shadow*."""
|
||||||
|
|
||||||
|
def __init__(self, shadow: str = "alice:$6$old:20000:0:99999:7:::") -> None:
|
||||||
|
super().__init__("host", "netadmin", "pw")
|
||||||
|
self.shadow = shadow
|
||||||
|
self.ran: list[tuple[str, bool]] = []
|
||||||
|
|
||||||
|
def run_command(self, command, *, privileged=False, timeout=60, stdin=None):
|
||||||
|
self.ran.append((command, privileged))
|
||||||
|
if command == "getent shadow alice":
|
||||||
|
return CommandResult(self.shadow + "\n", "", 0 if self.shadow else 2)
|
||||||
|
return CommandResult("", "", 0)
|
||||||
|
|
||||||
|
|
||||||
|
def _passwd() -> list[str]:
|
||||||
|
return "alice:x:1001:1001::/home/alice:/bin/bash".split(":")
|
||||||
|
|
||||||
|
|
||||||
|
def test_a_linux_host_can_add_and_lock_personal_accounts() -> None:
|
||||||
|
assert issubclass(LinuxDriver, LocalAccountsMixin)
|
||||||
|
assert LinuxDriver.ACCOUNT_PASSWORD_SCHEME == "sha512"
|
||||||
|
assert LinuxDriver.ACCOUNT_SHELL == "/bin/bash"
|
||||||
|
# Debian and Ubuntu grant %sudo, the Red Hat family %wheel, old Ubuntu %admin.
|
||||||
|
assert set(LinuxDriver.ACCOUNT_ADMIN_GROUPS) == {"sudo", "wheel", "admin"}
|
||||||
|
|
||||||
|
|
||||||
|
def test_a_new_account_gets_a_home_and_bash() -> None:
|
||||||
|
d = _Driver()
|
||||||
|
assert d._account_create("alice", HASH) == [
|
||||||
|
["useradd", "-m", "-s", "/bin/bash", "-p", HASH, "alice"]
|
||||||
|
]
|
||||||
|
assert d._account_create("alice", None) == [["useradd", "-m", "-s", "/bin/bash", "alice"]]
|
||||||
|
|
||||||
|
|
||||||
|
def test_locking_expires_the_account_and_locks_its_password() -> None:
|
||||||
|
d = _Driver()
|
||||||
|
assert d._account_lock("alice", locked=False) == [["usermod", "-L", "-e", "1", "alice"]]
|
||||||
|
assert d._account_lock("alice", locked=True) == []
|
||||||
|
|
||||||
|
|
||||||
|
def test_opening_lifts_the_expiry_and_sets_the_new_password() -> None:
|
||||||
|
d = _Driver()
|
||||||
|
assert d._account_open("alice", HASH, locked=True) == [
|
||||||
|
["usermod", "-e", "", "-p", HASH, "alice"]
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
def test_opening_without_a_new_password_gives_back_the_old_one() -> None:
|
||||||
|
d = _Driver(shadow="alice:!$6$old:20000:0:99999:7::1:")
|
||||||
|
assert d._account_open("alice", None, locked=True) == [["usermod", "-U", "-e", "", "alice"]]
|
||||||
|
|
||||||
|
|
||||||
|
def test_an_account_that_never_had_a_password_keeps_it_locked() -> None:
|
||||||
|
# usermod -U refuses to leave an account without a password.
|
||||||
|
for field in ("!", "!!", "!*", "*"):
|
||||||
|
d = _Driver(shadow=f"alice:{field}:20000:0:99999:7::1:")
|
||||||
|
assert d._account_open("alice", None, locked=True) == [["usermod", "-e", "", "alice"]]
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
("expire", "locked"),
|
||||||
|
[("", False), ("1", True), (str(_TODAY), True), (str(_TODAY + 30), False)],
|
||||||
|
)
|
||||||
|
def test_an_expired_account_is_locked(expire: str, locked: bool) -> None:
|
||||||
|
d = _Driver(shadow=f"alice:$6$old:20000:0:99999:7::{expire}:")
|
||||||
|
assert d._account_locked("alice", _passwd()) is locked
|
||||||
|
assert d.ran == [("getent shadow alice", True)]
|
||||||
|
|
||||||
|
|
||||||
|
def test_a_shadow_that_cannot_be_read_raises() -> None:
|
||||||
|
with pytest.raises(RuntimeError, match="alice"):
|
||||||
|
_Driver(shadow="")._account_locked("alice", _passwd())
|
||||||
|
|
||||||
|
|
||||||
|
def test_leaving_a_group_is_gpasswd() -> None:
|
||||||
|
assert _Driver()._account_leave_group("alice", "sudo", ["alice", "sudo"]) == [
|
||||||
|
"gpasswd",
|
||||||
|
"-d",
|
||||||
|
"alice",
|
||||||
|
"sudo",
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
def test_netorks_own_account_is_made_with_its_uid() -> None:
|
||||||
|
"""A fixed uid on every host, below UID_MIN (NetOrk/netork#1817)."""
|
||||||
|
assert _Driver()._account_create_with_uid("netork", 900) == [
|
||||||
|
["useradd", "-m", "-u", "900", "-s", "/bin/bash", "netork"]
|
||||||
|
]
|
||||||
@@ -0,0 +1,58 @@
|
|||||||
|
"""Scheduled jobs on a Linux host: napalm-device-types' POSIX implementation (#67).
|
||||||
|
|
||||||
|
The report, its parse and every write are ``PosixScheduledJobsMixin``'s; the
|
||||||
|
driver carries the command across ``run_command``, as root, in one round trip.
|
||||||
|
"""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from unittest.mock import MagicMock
|
||||||
|
|
||||||
|
from napalm_device_types import CommandResult, PosixScheduledJobsMixin
|
||||||
|
|
||||||
|
from napalm_linux import LinuxDriver
|
||||||
|
|
||||||
|
REPORT = """SJ_BEGIN
|
||||||
|
[system /etc/cron.d/certbot]
|
||||||
|
|0 */12 * * * root certbot -q renew
|
||||||
|
[tz]
|
||||||
|
/usr/share/zoneinfo/Europe/Berlin
|
||||||
|
[owners]
|
||||||
|
certbot: /etc/cron.d/certbot
|
||||||
|
SJ_END
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
def _driver() -> LinuxDriver:
|
||||||
|
driver = LinuxDriver("host", "user", "pw")
|
||||||
|
driver.run_command = MagicMock(return_value=CommandResult(REPORT, "", 0)) # type: ignore[method-assign]
|
||||||
|
return driver
|
||||||
|
|
||||||
|
|
||||||
|
def test_a_linux_host_offers_every_verb():
|
||||||
|
assert issubclass(LinuxDriver, PosixScheduledJobsMixin)
|
||||||
|
for verb in (
|
||||||
|
"get_scheduled_jobs",
|
||||||
|
"manage_scheduled_job",
|
||||||
|
"put_scheduled_job",
|
||||||
|
"delete_scheduled_job",
|
||||||
|
):
|
||||||
|
assert callable(getattr(LinuxDriver, verb))
|
||||||
|
|
||||||
|
|
||||||
|
def test_the_jobs_are_read_as_root_in_one_round_trip():
|
||||||
|
driver = _driver()
|
||||||
|
reading = driver.get_scheduled_jobs()
|
||||||
|
assert reading["time_zone"] == "Europe/Berlin"
|
||||||
|
(job,) = reading["jobs"]
|
||||||
|
assert (job["run_as"], job["managed_by"], job["command"]) == (
|
||||||
|
"root",
|
||||||
|
"package",
|
||||||
|
"certbot -q renew",
|
||||||
|
)
|
||||||
|
driver.run_command.assert_called_once()
|
||||||
|
assert driver.run_command.call_args.kwargs["privileged"] is True
|
||||||
|
|
||||||
|
|
||||||
|
def test_the_old_cron_reader_is_gone():
|
||||||
|
assert not hasattr(LinuxDriver, "get_cron_jobs")
|
||||||
@@ -0,0 +1,289 @@
|
|||||||
|
"""``set_hostname`` renames the host itself, not only what netOrk calls it (#45).
|
||||||
|
|
||||||
|
The static and runtime hostname take the short name; ``/etc/hosts`` follows, so
|
||||||
|
the host still resolves its own name; and a cloud-init host is told to keep the
|
||||||
|
name, or its next boot puts the image's name back. Every file root writes is
|
||||||
|
staged without privilege first, as ``_install_file_as_root`` does for the fixes.
|
||||||
|
"""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import shlex
|
||||||
|
from types import SimpleNamespace
|
||||||
|
from unittest.mock import MagicMock
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
from napalm_linux import LinuxDriver
|
||||||
|
from napalm_linux._hostname import _rename_in_hosts, _split_hostname
|
||||||
|
|
||||||
|
_PW = "pw-45"
|
||||||
|
_STAGED = "/tmp/netork-stage.Xy34Zw"
|
||||||
|
_HOSTS = "127.0.0.1\tlocalhost\n127.0.1.1\told.example.net old\n\n::1 localhost ip6-localhost\n"
|
||||||
|
|
||||||
|
|
||||||
|
class Host:
|
||||||
|
"""The host's exec channels: every line, its stdin, and whether it ran as root."""
|
||||||
|
|
||||||
|
def __init__(self, *, hosts=_HOSTS, cloud_init=False, rename_status=0, hosts_status=0):
|
||||||
|
self.runs: list[SimpleNamespace] = []
|
||||||
|
self.hosts = hosts
|
||||||
|
self.cloud_init = cloud_init
|
||||||
|
self.rename_status = rename_status
|
||||||
|
self.hosts_status = hosts_status
|
||||||
|
|
||||||
|
def open_session(self):
|
||||||
|
return _Channel(self)
|
||||||
|
|
||||||
|
def answer(self, command: str, script: str) -> tuple[str, int]:
|
||||||
|
if command.startswith("sudo -n "):
|
||||||
|
return "sudo: a password is required\n", 1
|
||||||
|
if script == "hostname":
|
||||||
|
return "old\n", 0
|
||||||
|
if script.startswith("hostname -f"):
|
||||||
|
return "old.example.net\n", 0
|
||||||
|
if script == "cat /etc/hosts":
|
||||||
|
return self.hosts, 0
|
||||||
|
if script.startswith("test -d /etc/cloud"):
|
||||||
|
return "", 0 if self.cloud_init else 1
|
||||||
|
if "mktemp" in script:
|
||||||
|
return _STAGED, 0
|
||||||
|
if "hostnamectl" in script:
|
||||||
|
return (
|
||||||
|
"hostname: you must be root\n" if self.rename_status else ""
|
||||||
|
), self.rename_status
|
||||||
|
if "/etc/hosts" in script and "install" in script:
|
||||||
|
return ("install: cannot create\n" if self.hosts_status else ""), self.hosts_status
|
||||||
|
return "", 0
|
||||||
|
|
||||||
|
@property
|
||||||
|
def root_scripts(self) -> list[str]:
|
||||||
|
return [run.script for run in self.runs if run.root]
|
||||||
|
|
||||||
|
def staged(self) -> list[bytes]:
|
||||||
|
return [run.stdin for run in self.runs if "mktemp" in run.script]
|
||||||
|
|
||||||
|
|
||||||
|
class _Channel:
|
||||||
|
def __init__(self, host: Host):
|
||||||
|
self._host = host
|
||||||
|
self._run: SimpleNamespace | None = None
|
||||||
|
self._out = b""
|
||||||
|
self._status = 0
|
||||||
|
|
||||||
|
def exec_command(self, command):
|
||||||
|
assert _PW not in command
|
||||||
|
root = command.startswith(("sudo ", "sh -c "))
|
||||||
|
parts = shlex.split(command)
|
||||||
|
script = parts[parts.index("-c") + 1] if root else command
|
||||||
|
script = script.removeprefix("exec </dev/null; ")
|
||||||
|
self._run = SimpleNamespace(line=command, script=script, root=root, stdin=b"")
|
||||||
|
self._host.runs.append(self._run)
|
||||||
|
output, self._status = self._host.answer(command, script)
|
||||||
|
self._out = output.encode()
|
||||||
|
|
||||||
|
def settimeout(self, timeout):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def sendall(self, data):
|
||||||
|
self._run.stdin += data
|
||||||
|
|
||||||
|
def shutdown_write(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def close(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def recv_ready(self):
|
||||||
|
return bool(self._out)
|
||||||
|
|
||||||
|
def recv(self, size):
|
||||||
|
chunk, self._out = self._out[:size], self._out[size:]
|
||||||
|
return chunk
|
||||||
|
|
||||||
|
def recv_stderr_ready(self):
|
||||||
|
return False
|
||||||
|
|
||||||
|
def recv_stderr(self, size):
|
||||||
|
return b""
|
||||||
|
|
||||||
|
def exit_status_ready(self):
|
||||||
|
return not self._out
|
||||||
|
|
||||||
|
def recv_exit_status(self):
|
||||||
|
return self._status
|
||||||
|
|
||||||
|
|
||||||
|
def _driver(host: Host, *, sudo_password: str | None = _PW) -> LinuxDriver:
|
||||||
|
optional_args = {"sudo_password": sudo_password} if sudo_password else {}
|
||||||
|
driver = LinuxDriver("h", "admin", "p", optional_args=optional_args)
|
||||||
|
driver._device = MagicMock()
|
||||||
|
driver._device.remote_conn_pre = SimpleNamespace(get_transport=lambda: host)
|
||||||
|
driver._root = False
|
||||||
|
driver._pkg_manager = "apt"
|
||||||
|
return driver
|
||||||
|
|
||||||
|
|
||||||
|
def _hosts_written(host: Host) -> str:
|
||||||
|
"""What was staged for /etc/hosts, or "" when it was left alone."""
|
||||||
|
staged = b""
|
||||||
|
for run in host.runs:
|
||||||
|
if "mktemp" in run.script:
|
||||||
|
staged = run.stdin
|
||||||
|
elif run.root and "/etc/hosts" in run.script:
|
||||||
|
return staged.decode()
|
||||||
|
return ""
|
||||||
|
|
||||||
|
|
||||||
|
class TestSplitHostname:
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
"name, expected",
|
||||||
|
[
|
||||||
|
("web-01", ("web-01", None)),
|
||||||
|
("web-01.example.net", ("web-01", "web-01.example.net")),
|
||||||
|
("A1", ("A1", None)),
|
||||||
|
("a" * 63, ("a" * 63, None)),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
def test_valid(self, name, expected):
|
||||||
|
assert _split_hostname(name) == expected
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
"name",
|
||||||
|
[
|
||||||
|
"",
|
||||||
|
"web_01",
|
||||||
|
"-web",
|
||||||
|
"web-",
|
||||||
|
"a" * 64,
|
||||||
|
".".join(["a" * 60] * 5),
|
||||||
|
"web 01",
|
||||||
|
"web;reboot",
|
||||||
|
"web.",
|
||||||
|
".web",
|
||||||
|
"web..example",
|
||||||
|
"$(id)",
|
||||||
|
],
|
||||||
|
)
|
||||||
|
def test_invalid(self, name):
|
||||||
|
with pytest.raises(ValueError):
|
||||||
|
_split_hostname(name)
|
||||||
|
|
||||||
|
|
||||||
|
class TestRenameInHosts:
|
||||||
|
def test_old_names_replaced_on_every_line_they_appear(self):
|
||||||
|
content = "127.0.0.1 localhost old\n10.0.0.5 old.example.net old # me\n"
|
||||||
|
assert _rename_in_hosts(content, {"old", "old.example.net"}, "new", "new.example.net") == (
|
||||||
|
"127.0.0.1\tlocalhost new.example.net new\n10.0.0.5\tnew.example.net new # me\n"
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_other_aliases_and_lines_kept(self):
|
||||||
|
content = "# static\n127.0.1.1 old mail\n::1 localhost\n"
|
||||||
|
assert _rename_in_hosts(content, {"old"}, "new", None) == (
|
||||||
|
"# static\n127.0.1.1\tnew mail\n::1 localhost\n"
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_names_compared_without_case(self):
|
||||||
|
assert _rename_in_hosts("127.0.1.1 OLD\n", {"old"}, "new", None) == "127.0.1.1\tnew\n"
|
||||||
|
|
||||||
|
def test_no_old_name_but_a_127_0_1_1_line_takes_the_new_name(self):
|
||||||
|
content = "127.0.0.1 localhost\n127.0.1.1 ubuntu\n"
|
||||||
|
assert _rename_in_hosts(content, {"old"}, "new", "new.example.net") == (
|
||||||
|
"127.0.0.1 localhost\n127.0.1.1\tnew.example.net new\n"
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_neither_leaves_the_file_alone(self):
|
||||||
|
content = "127.0.0.1 localhost localhost.localdomain\n::1 localhost\n"
|
||||||
|
assert _rename_in_hosts(content, {"old"}, "new", None) == content
|
||||||
|
|
||||||
|
def test_localhost_is_never_renamed(self):
|
||||||
|
content = "127.0.0.1 localhost\n"
|
||||||
|
assert _rename_in_hosts(content, {"localhost"}, "new", None) == content
|
||||||
|
|
||||||
|
|
||||||
|
class TestSetHostname:
|
||||||
|
def test_short_name_set_as_root(self):
|
||||||
|
host = Host()
|
||||||
|
|
||||||
|
_driver(host).set_hostname("new")
|
||||||
|
|
||||||
|
rename = host.root_scripts[0]
|
||||||
|
assert "hostnamectl set-hostname new" in rename
|
||||||
|
assert "/etc/hostname" in rename and "hostname new" in rename
|
||||||
|
|
||||||
|
def test_fqdn_sets_the_short_name_and_both_in_hosts(self):
|
||||||
|
host = Host()
|
||||||
|
|
||||||
|
_driver(host).set_hostname("new.example.net")
|
||||||
|
|
||||||
|
assert "hostnamectl set-hostname new " in host.root_scripts[0]
|
||||||
|
assert "example" not in host.root_scripts[0]
|
||||||
|
assert _hosts_written(host) == (
|
||||||
|
"127.0.0.1\tlocalhost\n127.0.1.1\tnew.example.net new\n\n::1 localhost ip6-localhost\n"
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_hosts_installed_from_a_staged_file_never_privileged_stdin(self):
|
||||||
|
host = Host()
|
||||||
|
|
||||||
|
_driver(host).set_hostname("new")
|
||||||
|
|
||||||
|
hosts_install = [s for s in host.root_scripts if "/etc/hosts" in s]
|
||||||
|
assert len(hosts_install) == 1
|
||||||
|
assert f"install -m 644 -o root -g root -- {_STAGED}" in hosts_install[0]
|
||||||
|
for run in host.runs:
|
||||||
|
if run.root:
|
||||||
|
assert run.stdin == f"{_PW}\n".encode()
|
||||||
|
|
||||||
|
def test_unchanged_hosts_not_rewritten(self):
|
||||||
|
host = Host(hosts="127.0.0.1 localhost\n")
|
||||||
|
|
||||||
|
_driver(host).set_hostname("new")
|
||||||
|
|
||||||
|
assert not any("/etc/hosts" in s for s in host.root_scripts)
|
||||||
|
|
||||||
|
def test_cloud_init_host_keeps_the_name(self):
|
||||||
|
host = Host(cloud_init=True)
|
||||||
|
|
||||||
|
_driver(host).set_hostname("new")
|
||||||
|
|
||||||
|
assert any(
|
||||||
|
"/etc/cloud/cloud.cfg.d/99-preserve-hostname.cfg" in s for s in host.root_scripts
|
||||||
|
)
|
||||||
|
assert any(b"preserve_hostname: true" in staged for staged in host.staged())
|
||||||
|
|
||||||
|
def test_no_cloud_init_no_drop_in(self):
|
||||||
|
host = Host()
|
||||||
|
|
||||||
|
_driver(host).set_hostname("new")
|
||||||
|
|
||||||
|
assert not any("cloud.cfg.d" in s for s in host.root_scripts)
|
||||||
|
|
||||||
|
def test_invalid_name_runs_nothing(self):
|
||||||
|
host = Host()
|
||||||
|
driver = _driver(host)
|
||||||
|
|
||||||
|
with pytest.raises(ValueError):
|
||||||
|
driver.set_hostname("new;reboot")
|
||||||
|
|
||||||
|
assert host.runs == []
|
||||||
|
driver._device.send_command.assert_not_called()
|
||||||
|
|
||||||
|
def test_failed_rename_raises_and_writes_nothing(self):
|
||||||
|
host = Host(rename_status=1)
|
||||||
|
|
||||||
|
with pytest.raises(RuntimeError):
|
||||||
|
_driver(host).set_hostname("new")
|
||||||
|
|
||||||
|
assert not any("/etc/hosts" in s for s in host.root_scripts)
|
||||||
|
|
||||||
|
def test_sudo_without_password_says_what_to_do(self):
|
||||||
|
host = Host()
|
||||||
|
|
||||||
|
with pytest.raises(RuntimeError, match="no sudo password was given"):
|
||||||
|
_driver(host, sudo_password=None).set_hostname("new")
|
||||||
|
|
||||||
|
def test_failed_hosts_write_raises(self):
|
||||||
|
host = Host(hosts_status=1)
|
||||||
|
|
||||||
|
with pytest.raises(RuntimeError, match="/etc/hosts"):
|
||||||
|
_driver(host).set_hostname("new")
|
||||||
@@ -86,3 +86,30 @@ def test_a_changed_key_is_refused():
|
|||||||
driver.open()
|
driver.open()
|
||||||
assert driver._device is None
|
assert driver._device is None
|
||||||
assert seen[0]["accepted"] is False
|
assert seen[0]["accepted"] is False
|
||||||
|
|
||||||
|
|
||||||
|
def test_a_connection_sshd_turns_away_is_tried_again(monkeypatch):
|
||||||
|
"""OpenSSH 9.8+ closes every connection from a source it has just penalised,
|
||||||
|
before its banner; the penalty ends after seconds (NetOrk/netork#1800)."""
|
||||||
|
from napalm_device_types import ssh_host_keys
|
||||||
|
|
||||||
|
slept: list[float] = []
|
||||||
|
monkeypatch.setattr(ssh_host_keys.time, "sleep", slept.append)
|
||||||
|
attempts: list[int] = []
|
||||||
|
|
||||||
|
def turned_away(**kwargs):
|
||||||
|
attempts.append(1)
|
||||||
|
if len(attempts) < 3:
|
||||||
|
try:
|
||||||
|
raise paramiko.SSHException("Error reading SSH protocol banner")
|
||||||
|
except paramiko.SSHException as exc:
|
||||||
|
raise NetmikoTimeoutException(f"A paramiko SSHException occurred: {exc}") from exc
|
||||||
|
return _device()
|
||||||
|
|
||||||
|
driver = _driver([])
|
||||||
|
with patch("napalm_linux.linux.ConnectHandler", side_effect=turned_away):
|
||||||
|
driver.open()
|
||||||
|
|
||||||
|
assert driver._device is not None
|
||||||
|
assert len(attempts) == 3
|
||||||
|
assert slept == list(ssh_host_keys.REFUSED_RETRY_WAITS[:2])
|
||||||
@@ -0,0 +1,140 @@
|
|||||||
|
"""What the old ``_sudo`` left on every host, and taking it back (NetOrk/netork#1090).
|
||||||
|
|
||||||
|
Until NetOrk/netork#1039, ``_sudo`` typed ``echo '<password>' | sudo -S -p ""
|
||||||
|
<command>`` into netmiko's interactive shell: the login user's history kept the
|
||||||
|
password once per command. Where only the part before the first ``&&`` ran as
|
||||||
|
root, snmpd.conf and apt's proxy file were left to the login user. The driver
|
||||||
|
says so through napalm-device-types' ``SudoHistoryMixin``, which counts and
|
||||||
|
deletes the lines and gives the files back to root.
|
||||||
|
"""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import shlex
|
||||||
|
from types import SimpleNamespace
|
||||||
|
from unittest.mock import MagicMock
|
||||||
|
|
||||||
|
from napalm_device_types import SudoHistoryMixin
|
||||||
|
|
||||||
|
from napalm_linux import LinuxDriver
|
||||||
|
from napalm_linux.linux import _SNMPD_CONF
|
||||||
|
|
||||||
|
_PW = "s3cr3t"
|
||||||
|
|
||||||
|
|
||||||
|
class _Channel:
|
||||||
|
def __init__(self, transport: _Transport):
|
||||||
|
self.transport = transport
|
||||||
|
self.command = ""
|
||||||
|
self.stdin = b""
|
||||||
|
self._out = b""
|
||||||
|
|
||||||
|
def exec_command(self, command):
|
||||||
|
self.command = command
|
||||||
|
self.transport.commands.append(command)
|
||||||
|
self._out = self.transport.answer(command).encode()
|
||||||
|
|
||||||
|
def settimeout(self, timeout):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def sendall(self, data):
|
||||||
|
self.stdin += data
|
||||||
|
|
||||||
|
def shutdown_write(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def close(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def recv_ready(self):
|
||||||
|
return bool(self._out)
|
||||||
|
|
||||||
|
def recv(self, size):
|
||||||
|
chunk, self._out = self._out[:size], self._out[size:]
|
||||||
|
return chunk
|
||||||
|
|
||||||
|
def recv_stderr_ready(self):
|
||||||
|
return False
|
||||||
|
|
||||||
|
def recv_stderr(self, size):
|
||||||
|
return b""
|
||||||
|
|
||||||
|
def exit_status_ready(self):
|
||||||
|
return not self._out
|
||||||
|
|
||||||
|
def recv_exit_status(self):
|
||||||
|
return 0
|
||||||
|
|
||||||
|
|
||||||
|
class _Transport:
|
||||||
|
def __init__(self, answers: dict[str, str]):
|
||||||
|
self.answers = answers
|
||||||
|
self.commands: list[str] = []
|
||||||
|
self.channels: list[_Channel] = []
|
||||||
|
|
||||||
|
def answer(self, command: str) -> str:
|
||||||
|
return next((out for key, out in self.answers.items() if key in command), "")
|
||||||
|
|
||||||
|
def open_session(self):
|
||||||
|
self.channels.append(_Channel(self))
|
||||||
|
return self.channels[-1]
|
||||||
|
|
||||||
|
|
||||||
|
def _driver(answers: dict[str, str]) -> tuple[LinuxDriver, _Transport]:
|
||||||
|
driver = LinuxDriver("h", "admin", "p", optional_args={"sudo_password": _PW})
|
||||||
|
transport = _Transport(answers)
|
||||||
|
driver._device = MagicMock()
|
||||||
|
driver._device.remote_conn_pre = SimpleNamespace(get_transport=lambda: transport)
|
||||||
|
driver._root = False
|
||||||
|
return driver, transport
|
||||||
|
|
||||||
|
|
||||||
|
def test_the_marker_is_what_the_old_sudo_typed():
|
||||||
|
typed = f'echo {shlex.quote(_PW)} | sudo -S -p "" apt-get update'
|
||||||
|
assert isinstance(LinuxDriver.SUDO_HISTORY_MARKER, str)
|
||||||
|
assert LinuxDriver.SUDO_HISTORY_MARKER in typed
|
||||||
|
|
||||||
|
|
||||||
|
def test_the_files_the_partial_sudo_left_to_the_login_user():
|
||||||
|
assert LinuxDriver.SUDO_ROOT_FILES == (
|
||||||
|
_SNMPD_CONF,
|
||||||
|
LinuxDriver.APT_PROXY_FILE,
|
||||||
|
)
|
||||||
|
assert LinuxDriver.APT_PROXY_FILE == "/etc/apt/apt.conf.d/00proxy"
|
||||||
|
|
||||||
|
|
||||||
|
def test_it_is_the_device_types_mixin():
|
||||||
|
assert issubclass(LinuxDriver, SudoHistoryMixin)
|
||||||
|
|
||||||
|
|
||||||
|
def test_counting_runs_as_the_login_user_on_the_exec_channel():
|
||||||
|
driver, transport = _driver({"grep -c": "121 .bash_history\n0 .zsh_history\n"})
|
||||||
|
|
||||||
|
assert driver.get_history_sudo_lines() == [{"file": "~/.bash_history", "lines": 121}]
|
||||||
|
|
||||||
|
# No sudo, no password on stdin, nothing typed into the interactive shell
|
||||||
|
# (whose history is where the lines came from).
|
||||||
|
assert not transport.channels[-1].command.startswith("sudo")
|
||||||
|
assert transport.channels[-1].stdin == b""
|
||||||
|
driver._device.send_command.assert_not_called()
|
||||||
|
|
||||||
|
|
||||||
|
def test_the_owner_fix_runs_as_root_without_stdin_but_the_password():
|
||||||
|
driver, transport = _driver(
|
||||||
|
{
|
||||||
|
"sed -i": "done 3 0 .bash_history\n",
|
||||||
|
"id -u": "1000\nadmin\n",
|
||||||
|
"chown": "fixed /etc/snmp/snmpd.conf\n",
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
result = driver.remove_history_sudo_lines()
|
||||||
|
|
||||||
|
assert result["success"] is True
|
||||||
|
assert result["removed"] == 3
|
||||||
|
assert result["owners_restored"] == ["/etc/snmp/snmpd.conf"]
|
||||||
|
root = [c for c in transport.channels if c.command.startswith("sudo -S")]
|
||||||
|
assert len(root) == 1 and "chown -h 0:0" in root[0].command
|
||||||
|
# sudo -S reads the password, and the command itself reads nothing.
|
||||||
|
assert root[0].stdin == f"{_PW}\n".encode()
|
||||||
|
assert all(c.stdin == b"" for c in transport.channels if c is not root[0])
|
||||||
@@ -21,7 +21,7 @@ from unittest.mock import MagicMock, patch
|
|||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from napalm_linux import LinuxDriver
|
from napalm_linux import LinuxDriver
|
||||||
from napalm_linux.linux import _HISTORY_OFF
|
from napalm_linux._shell import HISTORY_OFF as _HISTORY_OFF
|
||||||
|
|
||||||
_PW = "s3cr3t 'q' $x"
|
_PW = "s3cr3t 'q' $x"
|
||||||
|
|
||||||
@@ -236,7 +236,8 @@ class TestHistoryOff:
|
|||||||
def test_a_shell_that_refuses_it_does_not_stop_the_connection(self):
|
def test_a_shell_that_refuses_it_does_not_stop_the_connection(self):
|
||||||
driver = LinuxDriver("h", "admin", "p")
|
driver = LinuxDriver("h", "admin", "p")
|
||||||
device = MagicMock()
|
device = MagicMock()
|
||||||
device.send_command.side_effect = [Exception("no prompt"), "", "/usr/bin/apt"]
|
# history off (refused), prompt hooks off (#41), stty, package manager
|
||||||
|
device.send_command.side_effect = [Exception("no prompt"), "", "", "/usr/bin/apt"]
|
||||||
with patch("napalm_linux.linux.ConnectHandler", return_value=device):
|
with patch("napalm_linux.linux.ConnectHandler", return_value=device):
|
||||||
driver.open()
|
driver.open()
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,199 @@
|
|||||||
|
"""Without a sudo password the whole command runs as root, in one ``sh -c`` (#56).
|
||||||
|
|
||||||
|
A login that is not root and has no sudo password (NOPASSWD sudo, e.g. netOrk's
|
||||||
|
own account from NetOrk/netork#1817) types its privileged commands into the
|
||||||
|
interactive shell. ``sudo -n <command>`` covered only the first simple command of
|
||||||
|
the line, and a command that starts with a brace group did not even parse:
|
||||||
|
``sudo -n { LC_ALL=C dnf …; echo __DNF_RC=$?; } | cat`` is a syntax error, so dnf's
|
||||||
|
and yum's security advisories were always unknown.
|
||||||
|
|
||||||
|
The command now goes to ``sudo -n sh -c`` as one argument, as ``_as_root`` does.
|
||||||
|
A status echo at its end (``; echo __NETORK_RC=$?``) stays outside: it reports
|
||||||
|
sh's status, which is the command's, and still arrives when sudo refuses.
|
||||||
|
"""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import re
|
||||||
|
import shutil
|
||||||
|
import subprocess
|
||||||
|
from pathlib import Path
|
||||||
|
from shlex import quote
|
||||||
|
from unittest.mock import MagicMock
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
from napalm_device_types import DNF_SECURITY_COMMAND
|
||||||
|
|
||||||
|
from napalm_linux import LinuxDriver
|
||||||
|
from napalm_linux.linux import _YUM_SECURITY_COMMAND
|
||||||
|
|
||||||
|
BASH = shutil.which("bash")
|
||||||
|
needs_bash = pytest.mark.skipif(BASH is None, reason="needs bash")
|
||||||
|
|
||||||
|
#: dnf's answer on a Fedora 40 host with one pending security advisory.
|
||||||
|
_ADVISORY = "FEDORA-2024-1a2b3c4d5e Important/Sec. openssl-libs-1:3.1.4-2.fc40.x86_64"
|
||||||
|
|
||||||
|
|
||||||
|
def _driver(pkg_manager: str = "dnf", answers: list[str] | None = None) -> LinuxDriver:
|
||||||
|
"""A driver logged in as a user that is not root, without a sudo password."""
|
||||||
|
d = LinuxDriver.__new__(LinuxDriver)
|
||||||
|
d.hostname = "testhost"
|
||||||
|
d._sudo_password = None
|
||||||
|
d._pkg_manager = pkg_manager
|
||||||
|
d._root = False
|
||||||
|
d._device = MagicMock()
|
||||||
|
d._device.send_command.side_effect = answers or []
|
||||||
|
return d
|
||||||
|
|
||||||
|
|
||||||
|
def _sent(driver: LinuxDriver) -> list[str]:
|
||||||
|
return [c[0][0] for c in driver._device.send_command.call_args_list]
|
||||||
|
|
||||||
|
|
||||||
|
def _bash_parses(line: str) -> str:
|
||||||
|
"""bash's complaint about *line*, empty when it parses."""
|
||||||
|
assert BASH is not None
|
||||||
|
checked = subprocess.run( # noqa: S603 - a fixed interpreter, the line only parsed
|
||||||
|
[BASH, "-n", "-c", line], capture_output=True, text=True, check=False
|
||||||
|
)
|
||||||
|
return checked.stderr
|
||||||
|
|
||||||
|
|
||||||
|
class TestTheSecurityAdvisories:
|
||||||
|
@needs_bash
|
||||||
|
@pytest.mark.parametrize("manager", ["dnf", "yum"])
|
||||||
|
def test_the_line_parses(self, manager):
|
||||||
|
driver = _driver(manager, [f"{_ADVISORY}\n__DNF_RC=0\n__NETORK_RC=0"])
|
||||||
|
|
||||||
|
driver._rpm_security_names()
|
||||||
|
|
||||||
|
assert _bash_parses(_sent(driver)[0]) == ""
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
("manager", "command"), [("dnf", DNF_SECURITY_COMMAND), ("yum", _YUM_SECURITY_COMMAND)]
|
||||||
|
)
|
||||||
|
def test_all_of_it_runs_as_root_and_the_status_stays_outside(self, manager, command):
|
||||||
|
driver = _driver(manager, [f"{_ADVISORY}\n__DNF_RC=0\n__NETORK_RC=0"])
|
||||||
|
|
||||||
|
driver._rpm_security_names()
|
||||||
|
|
||||||
|
assert _sent(driver) == [f"sudo -n sh -c {quote(command)}; echo __NETORK_RC=$?"]
|
||||||
|
|
||||||
|
def test_the_advisories_are_read(self):
|
||||||
|
driver = _driver("dnf", [f"{_ADVISORY}\n__DNF_RC=0\n__NETORK_RC=0"])
|
||||||
|
|
||||||
|
assert driver._rpm_security_names() == {"openssl-libs"}
|
||||||
|
|
||||||
|
|
||||||
|
#: Every caller of ``_run_privileged``, with an answer that satisfies it.
|
||||||
|
_CALLERS = {
|
||||||
|
"reboot_host": (lambda d: d.reboot_host(), ["__NETORK_RC=0"]),
|
||||||
|
"refresh apt": (lambda d: d.refresh_available_updates(), ["__NETORK_RC=0"]),
|
||||||
|
"check-update": (
|
||||||
|
lambda d: d.get_available_updates(),
|
||||||
|
["__NETORK_RC=0", "__DNF_RC=0\n__NETORK_RC=0"],
|
||||||
|
),
|
||||||
|
"manage_service": (lambda d: d.manage_service("wg-quick@wg0", "restart"), ["__SVC_RC=0"]),
|
||||||
|
"listening sockets": (
|
||||||
|
lambda d: d._run_listening_sockets_command("sh -c 'ss -H -ltn'", privileged=True),
|
||||||
|
[""],
|
||||||
|
),
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@needs_bash
|
||||||
|
@pytest.mark.parametrize("caller", sorted(_CALLERS))
|
||||||
|
def test_every_privileged_line_parses_and_runs_whole_as_root(caller):
|
||||||
|
call, answers = _CALLERS[caller]
|
||||||
|
driver = _driver("apt" if caller == "refresh apt" else "dnf", answers)
|
||||||
|
|
||||||
|
call(driver)
|
||||||
|
|
||||||
|
for line in _sent(driver):
|
||||||
|
assert line.startswith("sudo -n sh -c '")
|
||||||
|
assert _bash_parses(line) == ""
|
||||||
|
|
||||||
|
|
||||||
|
def test_the_service_status_stays_outside():
|
||||||
|
driver = _driver(answers=["__SVC_RC=0"])
|
||||||
|
|
||||||
|
driver.manage_service("cron", "restart")
|
||||||
|
|
||||||
|
assert _sent(driver) == [
|
||||||
|
"sudo -n sh -c 'timeout 45 systemctl --no-ask-password restart -- cron.service 2>&1';"
|
||||||
|
" echo __SVC_RC=$?"
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
def test_a_command_without_a_status_echo_is_wrapped_whole():
|
||||||
|
driver = _driver(answers=[""])
|
||||||
|
|
||||||
|
driver._run_privileged("ss -H -ltnp | sort")
|
||||||
|
|
||||||
|
assert _sent(driver) == ["sudo -n sh -c 'ss -H -ltnp | sort'"]
|
||||||
|
|
||||||
|
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
# The lines run in a real bash, with a stand-in sudo and package manager
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
_SUDO_OK = '#!/bin/sh\n[ "$1" = -n ] && shift\nexec "$@"\n'
|
||||||
|
_SUDO_REFUSES = "#!/bin/sh\necho 'sudo: a password is required' >&2\nexit 1\n"
|
||||||
|
_DNF = f"#!/bin/sh\necho '{_ADVISORY}'\n"
|
||||||
|
|
||||||
|
|
||||||
|
def _shell_host(tmp_path: Path, sudo: str) -> LinuxDriver:
|
||||||
|
"""A driver whose interactive shell is a real bash with stand-ins on its PATH.
|
||||||
|
|
||||||
|
Like netmiko, it reads until the command's expect pattern matches and fails
|
||||||
|
when it never does.
|
||||||
|
"""
|
||||||
|
for name, script in (("sudo", sudo), ("dnf", _DNF)):
|
||||||
|
stub = tmp_path / name
|
||||||
|
stub.write_text(script)
|
||||||
|
stub.chmod(0o755)
|
||||||
|
env = {"PATH": f"{tmp_path}:/usr/bin:/bin", "LC_ALL": "C"}
|
||||||
|
|
||||||
|
def send_command(command, *, expect_string, **_kwargs):
|
||||||
|
assert BASH is not None
|
||||||
|
ran = subprocess.run( # noqa: S603 - a fixed interpreter and stand-ins
|
||||||
|
[BASH, "-c", command],
|
||||||
|
stdout=subprocess.PIPE,
|
||||||
|
stderr=subprocess.STDOUT, # one stream, in order, as on the PTY
|
||||||
|
text=True,
|
||||||
|
env=env,
|
||||||
|
check=False,
|
||||||
|
)
|
||||||
|
output = f"{ran.stdout}user@host:~$ "
|
||||||
|
if not re.search(expect_string, output):
|
||||||
|
raise TimeoutError(f"pattern not detected: {expect_string!r} in {output!r}")
|
||||||
|
return output
|
||||||
|
|
||||||
|
driver = _driver("dnf")
|
||||||
|
driver._device.send_command.side_effect = send_command
|
||||||
|
return driver
|
||||||
|
|
||||||
|
|
||||||
|
@needs_bash
|
||||||
|
def test_a_real_shell_reads_the_advisories(tmp_path):
|
||||||
|
driver = _shell_host(tmp_path, _SUDO_OK)
|
||||||
|
|
||||||
|
assert driver._rpm_security_names() == {"openssl-libs"}
|
||||||
|
|
||||||
|
|
||||||
|
@needs_bash
|
||||||
|
def test_a_refused_sudo_still_reports_its_status(tmp_path):
|
||||||
|
driver = _shell_host(tmp_path, _SUDO_REFUSES)
|
||||||
|
|
||||||
|
result = driver.refresh_available_updates()
|
||||||
|
|
||||||
|
assert result["success"] is False
|
||||||
|
assert "a password is required" in result["output"]
|
||||||
|
assert "NOPASSWD" in result["output"] # the hint
|
||||||
|
|
||||||
|
|
||||||
|
@needs_bash
|
||||||
|
def test_a_refused_sudo_leaves_the_advisories_unknown(tmp_path):
|
||||||
|
driver = _shell_host(tmp_path, _SUDO_REFUSES)
|
||||||
|
|
||||||
|
assert driver._rpm_security_names() is None
|
||||||
@@ -0,0 +1,79 @@
|
|||||||
|
"""systemd's shell context sequences stay out of every command's output (#41).
|
||||||
|
|
||||||
|
systemd 258 (Ubuntu 26.04) ships ``/etc/profile.d/80-systemd-osc-context.sh``.
|
||||||
|
An interactive bash then wraps every command in OSC 3008 sequences, from ``PS0``
|
||||||
|
before it runs and from ``PROMPT_COMMAND`` before the next prompt, each with a
|
||||||
|
fresh UUID. netmiko's interactive shell is such a bash: the sequences ended up in
|
||||||
|
every parsed value, and since the prompt never looked the same twice, netmiko
|
||||||
|
left the prompt line in the output as well. ``os_version`` grew past 255
|
||||||
|
characters and netOrk could not store the poll.
|
||||||
|
"""
|
||||||
|
|
||||||
|
from unittest.mock import MagicMock, patch
|
||||||
|
|
||||||
|
from napalm_linux._shell import HISTORY_OFF, PROMPT_HOOKS_OFF
|
||||||
|
from napalm_linux.linux import LinuxDriver
|
||||||
|
|
||||||
|
#: What ``cat /etc/os-release | grep PRETTY_NAME ...`` returned through netmiko on
|
||||||
|
#: an Ubuntu 26.04.1 host (2026-10-23), the prompt line already gone.
|
||||||
|
_UBUNTU_2604_OS_RELEASE = (
|
||||||
|
"\x1b]3008;end=87c2e1c5-f850-4429-9f06-749e9be783e6;exit=success\x1b\\"
|
||||||
|
"\x1b]3008;start=bc3bc23b-2652-4826-8192-be447f97dd6f;type=command;"
|
||||||
|
"machineid=35cf42f3beb446bbb9aa95b937a0cf48;user=netork;hostname=ubuntu;"
|
||||||
|
"bootid=d32bd88c-dfce-40fe-9869-257bf50ef0e0;pid=20059;cwd=/home/netork\x1b\\"
|
||||||
|
"Ubuntu 26.04.1 LTS\n"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def _opened(device: MagicMock) -> LinuxDriver:
|
||||||
|
driver = LinuxDriver("h", "admin", "p")
|
||||||
|
with patch("napalm_linux.linux.ConnectHandler", return_value=device):
|
||||||
|
driver.open()
|
||||||
|
return driver
|
||||||
|
|
||||||
|
|
||||||
|
class TestPromptHooksOff:
|
||||||
|
def test_the_session_drops_the_hooks_right_after_the_history(self):
|
||||||
|
device = MagicMock()
|
||||||
|
device.send_command.return_value = "/usr/bin/apt"
|
||||||
|
|
||||||
|
_opened(device)
|
||||||
|
|
||||||
|
sent = [c.args[0] for c in device.send_command.call_args_list]
|
||||||
|
assert sent[:2] == [HISTORY_OFF, PROMPT_HOOKS_OFF]
|
||||||
|
|
||||||
|
def test_it_unsets_both_hooks_including_an_array_prompt_command(self):
|
||||||
|
# The script appends to PROMPT_COMMAND as an array; assigning the empty
|
||||||
|
# string would only clear element 0 and leave systemd's hook running.
|
||||||
|
assert PROMPT_HOOKS_OFF.startswith(" ")
|
||||||
|
assert "unset PS0 PROMPT_COMMAND" in PROMPT_HOOKS_OFF
|
||||||
|
|
||||||
|
def test_the_prompt_is_read_again_once_the_hooks_are_gone(self):
|
||||||
|
# netmiko learned the prompt with the sequences in front of it; a prompt
|
||||||
|
# carrying a UUID never matches again, so strip_prompt would keep failing.
|
||||||
|
device = MagicMock()
|
||||||
|
device.send_command.return_value = "/usr/bin/apt"
|
||||||
|
|
||||||
|
_opened(device)
|
||||||
|
|
||||||
|
calls = [(c[0], c.args[0] if c.args else None) for c in device.mock_calls]
|
||||||
|
hooks_off = calls.index(("send_command", PROMPT_HOOKS_OFF))
|
||||||
|
assert "set_base_prompt" in [name for name, _ in calls[hooks_off + 1 :]]
|
||||||
|
|
||||||
|
def test_a_shell_that_refuses_it_does_not_stop_the_connection(self):
|
||||||
|
device = MagicMock()
|
||||||
|
device.send_command.side_effect = ["", Exception("no prompt"), "", "/usr/bin/apt"]
|
||||||
|
|
||||||
|
driver = _opened(device)
|
||||||
|
|
||||||
|
assert driver._device is device
|
||||||
|
|
||||||
|
|
||||||
|
class TestSendStripsTerminalCodes:
|
||||||
|
def test_os_version_comes_back_without_the_context_sequences(self):
|
||||||
|
device = MagicMock()
|
||||||
|
device.send_command.return_value = _UBUNTU_2604_OS_RELEASE
|
||||||
|
driver = LinuxDriver("h", "admin", "p")
|
||||||
|
driver._device = device
|
||||||
|
|
||||||
|
assert driver._send("cat /etc/os-release") == "Ubuntu 26.04.1 LTS"
|
||||||
Reference in new issue
Block a user