fix(lldp): list every neighbour of a port, with its chassis MAC #62
No files matched your search
@@ -43,8 +43,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|||||||
### Changed
|
### Changed
|
||||||
- Python 3.12 is the floor (`requires-python = ">=3.12"`): netOrk runs every driver
|
- 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).
|
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
|
### Fixed
|
||||||
|
- `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.
|
||||||
- dnf and yum hosts read pending updates and security advisories from the cached
|
- 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
|
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
|
cache `check-update` downloaded the repositories' metadata first, and on an Oracle
|
||||||
|
|||||||
+14
-27
@@ -50,6 +50,7 @@ from napalm_device_types import (
|
|||||||
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,
|
||||||
@@ -127,6 +128,10 @@ _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.
|
||||||
|
#: lldpd's neighbours; a failure prints nothing and reads as none, as before (#58).
|
||||||
|
_LLDPCTL_COMMAND = "lldpctl -f keyvalue 2>/dev/null || true"
|
||||||
|
#: A chassis id lldpd names ``mac`` that really is one.
|
||||||
|
_MAC_RE = re.compile(r"^([0-9A-Fa-f]{2}:){5}[0-9A-Fa-f]{2}$")
|
||||||
_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
|
||||||
@@ -763,7 +768,9 @@ class LinuxDriver(
|
|||||||
def get_lldp_neighbors(self) -> dict[str, list[dict[str, Any]]]:
|
def get_lldp_neighbors(self) -> dict[str, list[dict[str, Any]]]:
|
||||||
"""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.
|
||||||
"""
|
"""
|
||||||
# Check lldpctl is available
|
# Check lldpctl is available
|
||||||
@@ -779,33 +786,13 @@ class LinuxDriver(
|
|||||||
if "active" not in running:
|
if "active" not in running:
|
||||||
return {}
|
return {}
|
||||||
|
|
||||||
output = self._send("lldpctl -f keyvalue 2>/dev/null || true")
|
output = self._send(_LLDPCTL_COMMAND)
|
||||||
neighbors: dict[str, list[dict[str, Any]]] = {}
|
neighbors: dict[str, list[dict[str, Any]]] = {}
|
||||||
entries: dict[str, dict[str, str]] = {}
|
for row in parse_lldpctl_keyvalue(output):
|
||||||
|
entry: dict[str, Any] = {"hostname": row["system_name"], "port": row["port_id"]}
|
||||||
for line in output.splitlines():
|
if row["chassis_id_type"] == "mac" and _MAC_RE.match(row["chassis_id"]):
|
||||||
line = line.strip()
|
entry["mac"] = row["chassis_id"].lower()
|
||||||
if "=" not in line:
|
neighbors.setdefault(row["local_port"], []).append(entry)
|
||||||
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
|
return neighbors
|
||||||
|
|
||||||
def get_interfaces(self) -> dict[str, Any]:
|
def get_interfaces(self) -> dict[str, Any]:
|
||||||
|
|||||||
+1
-1
@@ -34,7 +34,7 @@ classifiers = [
|
|||||||
]
|
]
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"napalm>=4.0",
|
"napalm>=4.0",
|
||||||
"napalm-device-types>=4.22.0",
|
"napalm-device-types>=4.24.0",
|
||||||
"netmiko>=4.0.0",
|
"netmiko>=4.0.0",
|
||||||
"paramiko>=5.0.0", # CVE-2026-44405
|
"paramiko>=5.0.0", # CVE-2026-44405
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -21,7 +21,7 @@
|
|||||||
"napalm_linux.linux"
|
"napalm_linux.linux"
|
||||||
],
|
],
|
||||||
"python_file_lines": {
|
"python_file_lines": {
|
||||||
"napalm_linux/linux.py": 2142
|
"napalm_linux/linux.py": 2129
|
||||||
},
|
},
|
||||||
"untyped_public": []
|
"untyped_public": []
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,127 @@
|
|||||||
|
"""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.
|
||||||
|
"""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
from napalm_linux.linux import LinuxDriver
|
||||||
|
|
||||||
|
# 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
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
def _answer(driver: LinuxDriver, lldpctl: str, *, installed: bool = True):
|
||||||
|
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 "is-active lldpd" in command:
|
||||||
|
return "active"
|
||||||
|
if command.startswith("lldpctl"):
|
||||||
|
return lldpctl
|
||||||
|
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() == {}
|
||||||
Reference in new issue
Block a user