fix(snmp): read the community from the Name column
driver / standard (pull_request) Successful in 14s
driver / lint (pull_request) Successful in 12s
driver / types (pull_request) Successful in 59s
driver / test (3.12) (pull_request) Successful in 58s
driver / test (3.13) (pull_request) Successful in 50s
driver / lowest (pull_request) Successful in 24s
driver / build (pull_request) Successful in 26s
driver / security (pull_request) Successful in 1m2s
CI / driver (pull_request) Successful in 2m26s
driver / publish (pull_request) Skipped

The switch's community table starts with an Index column, and
get_snmp_config took a row's first word, so it reported the community
as "1". The name now comes from the column headed Name, found in the
header line above the dashes; the tests read the recorded table.

Found while recording fixtures for #3.
This commit is contained in:
Christian Manivong committed 2026-11-01 22:19:33 +01:00
1 parent 34f00f9f3a
commit 8e7be0e47e
6 files changed
+55 -22

No files matched your search

+2
View File
@@ -7,6 +7,8 @@ All notable changes to this driver. The format follows
## [Unreleased] ## [Unreleased]
### Fixed ### Fixed
- `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`.
- 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
with firmware 5.30 its 4096-bit exchange took 9 to 16 seconds, against netmiko's with firmware 5.30 its 4096-bit exchange took 9 to 16 seconds, against netmiko's
10-second `conn_timeout`, so every other connection failed and the switch flapped 10-second `conn_timeout`, so every other connection failed and the switch flapped
+29
View File
@@ -0,0 +1,29 @@
"""Pure parsers: the switch's CLI output in, plain values out."""
from __future__ import annotations
import re
_DASHES = re.compile(r"^-{4,}$")
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:
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
+4 -15
View File
@@ -39,6 +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
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@@ -1939,22 +1940,10 @@ class TPLinkJetstreamDriver(ConfigLifecycleMixin, SnmpAgentMixin, SwitchDriver):
if not out or "disabled" in out.lower() or "no community entry exists" in out.lower(): if not out or "disabled" in out.lower() or "no community entry exists" in out.lower():
return no_agent return no_agent
community = None communities = parse_snmp_communities(out)
in_table = False if not communities:
for line in 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:
community = parts[0]
break
if community is None:
return no_agent return no_agent
return SNMPConfigDict(running=True, community=community, port=161, version="2c") return SNMPConfigDict(running=True, community=communities[0], port=161, version="2c")
def get_device_warnings(self) -> list: def get_device_warnings(self) -> list:
"""Return device warnings. Currently delegates to SNMP detection only.""" """Return device warnings. Currently delegates to SNMP detection only."""
@@ -0,0 +1,3 @@
Index Name Type MIB-View
----- ---- ---- --------
1 public read-only viewDefault
+1 -1
View File
@@ -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": 2024 "napalm_tplink_jetstream/tplink_jetstream.py": 2013
}, },
"untyped_public": [] "untyped_public": []
} }
+16 -6
View File
@@ -3,14 +3,24 @@
It started from "public" and kept it when no table row parsed, so a switch It started from "public" and kept it when no table row parsed, so a switch
whose community table could not be read reported a running agent with the whose community table could not be read reported a running agent with the
community "public". community "public".
The fixture is ``show snmp-server community`` recorded from a JetStream switch: its
table starts with an ``Index`` column, which was read as the community.
""" """
from pathlib import Path
from unittest.mock import MagicMock from unittest.mock import MagicMock
import pytest import pytest
from napalm_tplink_jetstream.tplink_jetstream import TPLinkJetstreamDriver 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()
#: The recording without its only row.
NO_COMMUNITY = "".join(SHOW_SNMP_COMMUNITY.splitlines(keepends=True)[:2])
def _driver(output: str) -> TPLinkJetstreamDriver: def _driver(output: str) -> TPLinkJetstreamDriver:
driver = TPLinkJetstreamDriver("10.0.0.30", "admin", "pw") driver = TPLinkJetstreamDriver("10.0.0.30", "admin", "pw")
@@ -19,18 +29,18 @@ def _driver(output: str) -> TPLinkJetstreamDriver:
def test_the_listed_community_is_reported(): def test_the_listed_community_is_reported():
out = "Index Community-Name Access\n----- -------------- ------\nc0mm read-only x\n" config = _driver(SHOW_SNMP_COMMUNITY).get_snmp_config()
config = _driver(out).get_snmp_config() assert config == {"running": True, "community": "public", "port": 161, "version": "2c"}
assert config == {"running": True, "community": "c0mm", "port": 161, "version": "2c"}
@pytest.mark.parametrize( @pytest.mark.parametrize(
"output", "output",
[ [
"Index Community-Name Access\n----- -------------- ------\n", NO_COMMUNITY,
"Index Community-Name Access\n", SHOW_SNMP_COMMUNITY.splitlines(keepends=True)[0],
# A table without a column headed Name: its rows name no community.
SHOW_SNMP_COMMUNITY.replace("Name ", "Comm ", 1),
"something the parser does not know\n", "something the parser does not know\n",
], ],
) )