fix(snmp): read the communities over the REST API too
driver / standard (pull_request) Successful in 15s
driver / lint (pull_request) Successful in 12s
driver / types (pull_request) Successful in 57s
driver / test (3.12) (pull_request) Successful in 56s
driver / test (3.13) (pull_request) Successful in 47s
driver / lowest (pull_request) Successful in 25s
driver / build (pull_request) Successful in 25s
driver / security (pull_request) Successful in 1m6s
CI / driver (pull_request) Successful in 2m29s
driver / publish (pull_request) Skipped
driver / standard (pull_request) Successful in 15s
driver / lint (pull_request) Successful in 12s
driver / types (pull_request) Successful in 57s
driver / test (3.12) (pull_request) Successful in 56s
driver / test (3.13) (pull_request) Successful in 47s
driver / lowest (pull_request) Successful in 25s
driver / build (pull_request) Successful in 25s
driver / security (pull_request) Successful in 1m6s
CI / driver (pull_request) Successful in 2m29s
driver / publish (pull_request) Skipped
get_snmp_config sent "show snmp-server" through the CLI session, which does not exist on the REST transport, and returned None, so every switch managed over REST read as "SNMP not configured". Over REST the command now runs through the API's /cli endpoint and the output goes through the parser the SSH path uses. A failed REST read raises. Closes #10
This commit is contained in:
1 parent
ee49948b2b
commit
b26584c7da
4 files changed
+132
-42
No files matched your search
+34
-38
@@ -15,6 +15,7 @@ A specific transport can be forced with ``optional_args={"transport": "ssh"}``.
|
||||
Netmiko device type: ``hp_procurve`` / ``hp_procurve_telnet``
|
||||
"""
|
||||
|
||||
import base64
|
||||
import logging
|
||||
import re
|
||||
from typing import Any, ClassVar
|
||||
@@ -34,7 +35,7 @@ from napalm_device_types import (
|
||||
add_lag_interfaces,
|
||||
run_fix_steps,
|
||||
)
|
||||
from napalm_device_types.models import InterfaceConfigDict, VlanConfigDict
|
||||
from napalm_device_types.models import InterfaceConfigDict, SNMPConfigDict, VlanConfigDict
|
||||
from napalm_device_types.ssh_host_keys import (
|
||||
KNOWN_KEYS_ARG,
|
||||
OBSERVER_ARG,
|
||||
@@ -1204,47 +1205,42 @@ class ProcurveDriver(ConfigLifecycleMixin, SwitchDriver):
|
||||
"""Return device warnings. SNMP detection handled by poll task."""
|
||||
return []
|
||||
|
||||
def get_snmp_config(self):
|
||||
"""Return SNMP config if a community is configured on the switch."""
|
||||
try:
|
||||
from napalm_device_types.models import SNMPConfigDict
|
||||
except ImportError:
|
||||
return None
|
||||
def get_snmp_config(self) -> SNMPConfigDict | None:
|
||||
"""The community of the built-in agent, read-only preferred; None without one.
|
||||
|
||||
# Use a single command with short read_timeout to avoid blocking the poll.
|
||||
try:
|
||||
out = (
|
||||
self._device.send_command(
|
||||
"show snmp-server",
|
||||
expect_string=rf"{re.escape(self._device.base_prompt)}[>#]",
|
||||
read_timeout=10,
|
||||
).strip()
|
||||
if self._device
|
||||
else ""
|
||||
)
|
||||
except Exception:
|
||||
Over REST, ``show snmp-server`` runs through the API's ``/cli`` endpoint (#10).
|
||||
"""
|
||||
if self._transport == "api":
|
||||
out = self._api_show("show snmp-server")
|
||||
else:
|
||||
# Use a single command with short read_timeout to avoid blocking the poll.
|
||||
try:
|
||||
out = (
|
||||
self._device.send_command(
|
||||
"show snmp-server",
|
||||
expect_string=rf"{re.escape(self._device.base_prompt)}[>#]",
|
||||
read_timeout=10,
|
||||
)
|
||||
if self._device
|
||||
else ""
|
||||
)
|
||||
except Exception:
|
||||
return None
|
||||
communities = parsers.parse_snmp_info(out)["community"]
|
||||
if not communities:
|
||||
return None
|
||||
if not out:
|
||||
return None
|
||||
|
||||
# Parse communities from output
|
||||
community = None
|
||||
for line in out.splitlines():
|
||||
m = re.match(r"\s+(\S+)\s+(Manager|Operator)\s+(Restricted|Unrestricted)", line, re.I)
|
||||
if m:
|
||||
comm = m.group(1)
|
||||
access = m.group(3).lower()
|
||||
if access == "restricted": # read-only — prefer this
|
||||
community = comm
|
||||
break
|
||||
elif community is None:
|
||||
community = comm # fallback to first found
|
||||
|
||||
if not community:
|
||||
return None
|
||||
|
||||
read_only = [name for name, c in communities.items() if c["mode"] == "ro"]
|
||||
community = (read_only or list(communities))[0]
|
||||
return SNMPConfigDict(running=True, community=community, port=161, version="2c")
|
||||
|
||||
def _api_show(self, command: str) -> str:
|
||||
"""The output of a show command through the REST API's ``/cli`` endpoint."""
|
||||
resp = self._api.post("cli", json={"cmd": command})
|
||||
if not resp.ok or resp.json().get("status") == "CCS_FAILURE":
|
||||
raise CommandErrorException(f"{command!r} over REST: HTTP {resp.status_code}")
|
||||
encoded = resp.json().get("result_base64_encoded", "")
|
||||
return base64.b64decode(encoded).decode("utf-8", "replace")
|
||||
|
||||
# ------------------------------------------------------------------
|
||||
# SNMP agent (napalm-device-types SnmpAgentMixin): the caller's values
|
||||
# ------------------------------------------------------------------
|
||||
|
||||
Reference in new issue
Block a user