fix(snmp): read get_snmp_information's community from the Name column
driver / standard (pull_request) Successful in 18s
driver / lint (pull_request) Successful in 16s
driver / test (3.12) (pull_request) Successful in 1m5s
driver / types (pull_request) Successful in 1m12s
driver / test (3.13) (pull_request) Successful in 1m4s
driver / lowest (pull_request) Successful in 30s
driver / build (pull_request) Successful in 33s
driver / security (pull_request) Successful in 1m13s
CI / driver (pull_request) Successful in 2m55s
driver / publish (pull_request) Skipped

The switch's community table starts with an Index column. The getter
took the first word of each row as the community, so "public" was
reported as "1", and read the access from the second word, the name,
so every community came out read-only. It now uses the header-based
community table parser: the name from Name, the access from Type.

Closes #14
This commit is contained in:
Christian Manivong committed 2026-11-02 08:40:54 +01:00
1 parent 9ef8364ead
commit 9df131db78
5 files changed
+134 -33

No files matched your search

+48 -13
View File
@@ -7,23 +7,58 @@ import re
_DASHES = re.compile(r"^-{4,}$")
def _community_table(output: str) -> tuple[list[str], list[list[str]]]:
"""The header words of ``show snmp-server community`` and the rows below its dashes."""
header: list[str] = []
rows: list[list[str]] = []
previous: list[str] = []
in_table = False
for line in output.splitlines():
words = line.split()
if not words:
continue
if in_table:
rows.append(words)
elif _DASHES.match(words[0]):
header, in_table = previous, True
else:
previous = words
return header, rows
def _column(header: list[str], label: str) -> int | None:
"""The index of the header word containing *label*, case-insensitively."""
return next((i for i, word in enumerate(header) if label in word.lower()), None)
def parse_snmp_communities(output: str) -> list[str]:
"""The community names in ``show snmp-server community``, in the switch's order.
The table starts with an ``Index`` column: the name is the column headed
``Name``, found in the header line above the dashes. Without one, no name.
"""
names: list[str] = []
header: list[str] = []
column: int | None = None
for line in output.splitlines():
words = line.split()
if not words:
header, rows = _community_table(output)
name = _column(header, "name")
if name is None:
return []
return [row[name] for row in rows if name < len(row)]
def parse_snmp_community_access(output: str) -> dict[str, str]:
"""Each community in ``show snmp-server community`` and its access, ``"rw"`` or ``"ro"``.
The name is the column headed ``Name``, the access the column headed ``Type``
(``read-only``, ``read-write``): a community is ``"rw"`` when its type says
write, ``"ro"`` otherwise. Without a ``Name`` column, no community.
"""
header, rows = _community_table(output)
name, kind = _column(header, "name"), _column(header, "type")
access: dict[str, str] = {}
if name is None:
return access
for row in rows:
if name >= len(row):
continue
if column is None:
if _DASHES.match(words[0]):
column = next((i for i, w in enumerate(header) if "name" in w.lower()), -1)
header = words
elif 0 <= column < len(words):
names.append(words[column])
return names
typ = row[kind].lower() if kind is not None and kind < len(row) else ""
access[row[name]] = "rw" if "write" in typ else "ro"
return access