fix(snmp): read get_snmp_information's community from the Name column #15
No files matched your search
@@ -7,6 +7,10 @@ All notable changes to this driver. The format follows
|
|||||||
## [Unreleased]
|
## [Unreleased]
|
||||||
|
|
||||||
### Fixed
|
### Fixed
|
||||||
|
- `get_snmp_information()` reads each community from the table's `Name` column and its
|
||||||
|
access from the `Type` column (#14). It took the `Index` column as the community, so
|
||||||
|
the switch's `public` was reported as `1`, and read the access from the name, so every
|
||||||
|
community came out read-only.
|
||||||
- `get_snmp_config()` reads the community from the table's `Name` column. It took the
|
- `get_snmp_config()` reads the community from the table's `Name` column. It took the
|
||||||
first column, which is `Index` on the switch, and reported the community as `1`.
|
first column, which is `Index` on the switch, and reported the community as `1`.
|
||||||
- The SSH key exchange no longer uses `diffie-hellman-group16-sha512`. On an SG2210P
|
- The SSH key exchange no longer uses `diffie-hellman-group16-sha512`. On an SG2210P
|
||||||
|
|||||||
@@ -7,23 +7,58 @@ import re
|
|||||||
_DASHES = re.compile(r"^-{4,}$")
|
_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]:
|
def parse_snmp_communities(output: str) -> list[str]:
|
||||||
"""The community names in ``show snmp-server community``, in the switch's order.
|
"""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
|
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.
|
``Name``, found in the header line above the dashes. Without one, no name.
|
||||||
"""
|
"""
|
||||||
names: list[str] = []
|
header, rows = _community_table(output)
|
||||||
header: list[str] = []
|
name = _column(header, "name")
|
||||||
column: int | None = None
|
if name is None:
|
||||||
for line in output.splitlines():
|
return []
|
||||||
words = line.split()
|
return [row[name] for row in rows if name < len(row)]
|
||||||
if not words:
|
|
||||||
|
|
||||||
|
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
|
continue
|
||||||
if column is None:
|
typ = row[kind].lower() if kind is not None and kind < len(row) else ""
|
||||||
if _DASHES.match(words[0]):
|
access[row[name]] = "rw" if "write" in typ else "ro"
|
||||||
column = next((i for i, w in enumerate(header) if "name" in w.lower()), -1)
|
return access
|
||||||
header = words
|
|
||||||
elif 0 <= column < len(words):
|
|
||||||
names.append(words[column])
|
|
||||||
return names
|
|
||||||
@@ -39,7 +39,7 @@ from netmiko import ConnectHandler
|
|||||||
from netmiko.exceptions import NetmikoAuthenticationException, NetmikoTimeoutException
|
from netmiko.exceptions import NetmikoAuthenticationException, NetmikoTimeoutException
|
||||||
|
|
||||||
from napalm_tplink_jetstream._ssh import netmiko_connect_args
|
from napalm_tplink_jetstream._ssh import netmiko_connect_args
|
||||||
from napalm_tplink_jetstream.parsers import parse_snmp_communities
|
from napalm_tplink_jetstream.parsers import parse_snmp_communities, parse_snmp_community_access
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
@@ -1439,8 +1439,8 @@ class TPLinkJetstreamDriver(ConfigLifecycleMixin, SnmpAgentMixin, SwitchDriver):
|
|||||||
"""Return SNMP configuration.
|
"""Return SNMP configuration.
|
||||||
|
|
||||||
Reads contact/location from ``show system-info`` and communities
|
Reads contact/location from ``show system-info`` and communities
|
||||||
from ``show snmp community``. If SNMP is disabled an empty community
|
from ``show snmp community``: the ``Name`` column, the access from ``Type``.
|
||||||
dict is returned.
|
If SNMP is disabled an empty community dict is returned.
|
||||||
"""
|
"""
|
||||||
sys_info = self._send_command("show system-info")
|
sys_info = self._send_command("show system-info")
|
||||||
contact = self._parse_key_value(sys_info, "Contact Information")
|
contact = self._parse_key_value(sys_info, "Contact Information")
|
||||||
@@ -1448,22 +1448,8 @@ class TPLinkJetstreamDriver(ConfigLifecycleMixin, SnmpAgentMixin, SwitchDriver):
|
|||||||
mac = self._parse_key_value(sys_info, "Mac Address").replace("-", ":").upper()
|
mac = self._parse_key_value(sys_info, "Mac Address").replace("-", ":").upper()
|
||||||
|
|
||||||
snmp_out = self._send_command("show snmp community")
|
snmp_out = self._send_command("show snmp community")
|
||||||
communities: dict[str, dict] = {}
|
access = {} if "disabled" in snmp_out.lower() else parse_snmp_community_access(snmp_out)
|
||||||
|
communities = {name: {"acl": "", "mode": mode} for name, mode in access.items()}
|
||||||
if "disabled" not in snmp_out.lower():
|
|
||||||
in_table = False
|
|
||||||
for line in snmp_out.splitlines():
|
|
||||||
line_s = line.strip()
|
|
||||||
if re.match(r"^-{4,}", line_s):
|
|
||||||
in_table = True
|
|
||||||
continue
|
|
||||||
if not in_table or not line_s:
|
|
||||||
continue
|
|
||||||
parts = line_s.split()
|
|
||||||
if len(parts) >= 2:
|
|
||||||
comm_name = parts[0]
|
|
||||||
mode = "rw" if "write" in parts[1].lower() else "ro"
|
|
||||||
communities[comm_name] = {"acl": "", "mode": mode}
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
"contact": contact,
|
"contact": contact,
|
||||||
|
|||||||
@@ -27,7 +27,7 @@
|
|||||||
"napalm_tplink_jetstream.tplink_jetstream"
|
"napalm_tplink_jetstream.tplink_jetstream"
|
||||||
],
|
],
|
||||||
"python_file_lines": {
|
"python_file_lines": {
|
||||||
"napalm_tplink_jetstream/tplink_jetstream.py": 2013
|
"napalm_tplink_jetstream/tplink_jetstream.py": 1999
|
||||||
},
|
},
|
||||||
"untyped_public": []
|
"untyped_public": []
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,76 @@
|
|||||||
|
"""get_snmp_information reads the community from the Name column, its access from Type (#14).
|
||||||
|
|
||||||
|
It took the first word of every row, which is the ``Index`` column on the switch, and
|
||||||
|
read the access from the second, which is the name: the community came out as ``"1"``
|
||||||
|
and every one as read-only.
|
||||||
|
|
||||||
|
The fixture is ``show snmp-server community`` recorded from a JetStream switch; the
|
||||||
|
read-write rows below are that recording with a row added in the same layout.
|
||||||
|
"""
|
||||||
|
|
||||||
|
from pathlib import Path
|
||||||
|
from unittest.mock import MagicMock
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
from napalm_tplink_jetstream.parsers import parse_snmp_community_access
|
||||||
|
from napalm_tplink_jetstream.tplink_jetstream import TPLinkJetstreamDriver
|
||||||
|
|
||||||
|
FIXTURES = Path(__file__).parent.parent / "fixtures"
|
||||||
|
|
||||||
|
SHOW_SNMP_COMMUNITY = (FIXTURES / "jetstream-show-snmp-server-community.txt").read_text()
|
||||||
|
WITH_READ_WRITE = SHOW_SNMP_COMMUNITY + "2 private read-write viewDefault\n"
|
||||||
|
#: A read-only community whose name says "write": only the Type column decides.
|
||||||
|
NAMED_WRITE = SHOW_SNMP_COMMUNITY + "2 writers read-only viewDefault\n"
|
||||||
|
|
||||||
|
SYSTEM_INFO = """
|
||||||
|
System Description - JetStream 24-Port Gigabit L2 Managed Switch
|
||||||
|
Contact Information - noc
|
||||||
|
System Location - rack 2
|
||||||
|
Mac Address - 00-11-22-33-44-55
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
def _driver(community_output: str) -> TPLinkJetstreamDriver:
|
||||||
|
driver = TPLinkJetstreamDriver("10.0.0.30", "admin", "pw")
|
||||||
|
outputs = {"show system-info": SYSTEM_INFO, "show snmp community": community_output}
|
||||||
|
driver._send_command = MagicMock(side_effect=lambda command, **_: outputs[command])
|
||||||
|
return driver
|
||||||
|
|
||||||
|
|
||||||
|
def test_the_recorded_community_is_reported_by_name():
|
||||||
|
info = _driver(SHOW_SNMP_COMMUNITY).get_snmp_information()
|
||||||
|
|
||||||
|
assert info["community"] == {"public": {"acl": "", "mode": "ro"}}
|
||||||
|
assert (info["contact"], info["location"]) == ("noc", "rack 2")
|
||||||
|
|
||||||
|
|
||||||
|
def test_the_access_is_read_from_the_type_column():
|
||||||
|
info = _driver(WITH_READ_WRITE).get_snmp_information()
|
||||||
|
|
||||||
|
assert info["community"] == {
|
||||||
|
"public": {"acl": "", "mode": "ro"},
|
||||||
|
"private": {"acl": "", "mode": "rw"},
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def test_a_name_that_says_write_does_not_make_it_read_write():
|
||||||
|
assert _driver(NAMED_WRITE).get_snmp_information()["community"]["writers"]["mode"] == "ro"
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
("output", "expected"),
|
||||||
|
[
|
||||||
|
(SHOW_SNMP_COMMUNITY, {"public": "ro"}),
|
||||||
|
(WITH_READ_WRITE, {"public": "ro", "private": "rw"}),
|
||||||
|
# The header and dashes alone: no community.
|
||||||
|
("".join(SHOW_SNMP_COMMUNITY.splitlines(keepends=True)[:2]), {}),
|
||||||
|
# No column headed Name: its rows name no community.
|
||||||
|
(SHOW_SNMP_COMMUNITY.replace("Name ", "Comm ", 1), {}),
|
||||||
|
# No column headed Type: the access is not read from another one.
|
||||||
|
(NAMED_WRITE.replace("Type ", "Kind ", 1), {"public": "ro", "writers": "ro"}),
|
||||||
|
("something the parser does not know\n", {}),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
def test_parse_snmp_community_access(output, expected):
|
||||||
|
assert parse_snmp_community_access(output) == expected
|
||||||
Reference in new issue
Block a user