Skip to Content
ManagerOperations

Operations

Complete API reference for Manager operations.

Manager

Constructor

from snmpkit.manager import Manager mgr = Manager( host="192.168.1.1", port=161, community="public", version=2, timeout=5.0, retries=3, # SNMPv3 USM (optional) user="admin", auth_protocol="SHA256", auth_password="auth_pass", priv_protocol="AES", priv_password="priv_pass", context_name="", )
ParameterTypeDefaultDescription
hoststrrequiredTarget device hostname or IP
portint161SNMP UDP port
communitystr”public”Community string (v1/v2c)
versionint2SNMP version: 1, 2 (v2c), or 3
timeoutfloat5.0Request timeout in seconds
retriesint3Number of retries on timeout
userstrNoneSNMPv3 username
auth_protocolstrNoneAuth protocol: “MD5”, “SHA”, “SHA224”, “SHA256”, “SHA384”, “SHA512”
auth_passwordstrNoneAuth password
priv_protocolstrNonePrivacy protocol: “DES”, “AES”
priv_passwordstrNonePrivacy password
context_namestr""SNMPv3 context name

Context Manager

async with Manager("192.168.1.1") as mgr: # mgr.connect() called automatically value = await mgr.get("1.3.6.1.2.1.1.1.0") # mgr.close() called automatically

Manual Connection

mgr = Manager("192.168.1.1") await mgr.connect() try: value = await mgr.get("1.3.6.1.2.1.1.1.0") finally: await mgr.close()

GET Operations

get

Retrieve a single OID value.

async def get(self, oid: str) -> Value
value = await mgr.get("1.3.6.1.2.1.1.1.0")

Raises:

  • NoSuchObjectError — OID does not exist
  • NoSuchInstanceError — Instance does not exist
  • TimeoutError — Request timed out after all retries
  • GenericError — SNMP error response

get_many

Retrieve multiple OIDs in a single request.

async def get_many(self, *oids: str) -> list[Value]
values = await mgr.get_many( "1.3.6.1.2.1.1.1.0", "1.3.6.1.2.1.1.3.0", ) # values[0] = sysDescr # values[1] = sysUpTime

get_next

Get the next OID in lexicographic order.

async def get_next(self, oid: str) -> tuple[str, Value]
next_oid, value = await mgr.get_next("1.3.6.1.2.1.1.1") # Returns ("1.3.6.1.2.1.1.1.0", <value>)

Raises:

  • EndOfMibViewError — No more OIDs available

get_bulk

Get multiple OIDs using GETBULK (v2c/v3 only).

async def get_bulk( self, *oids: str, non_repeaters: int = 0, max_repetitions: int = 10, ) -> list[tuple[str, Value]]
results = await mgr.get_bulk( "1.3.6.1.2.1.2.2.1.1", max_repetitions=25, ) for oid, value in results: print(f"{oid} = {value}")

get_bulk is not available in SNMPv1. Use version=2 or higher.

WALK Operations

walk

Iterate through an OID subtree using GETNEXT.

async def walk(self, oid: str) -> AsyncIterator[tuple[str, Value]]
async for oid, value in mgr.walk("1.3.6.1.2.1.1"): print(f"{oid} = {value}")

The iterator stops when:

  • An OID outside the subtree is returned
  • EndOfMibView is returned

bulk_walk

Efficient walk using GETBULK (v2c/v3 only).

async def bulk_walk( self, oid: str, bulk_size: int = 10, ) -> AsyncIterator[tuple[str, Value]]
async for oid, value in mgr.bulk_walk("1.3.6.1.2.1.2.2", bulk_size=25): print(f"{oid} = {value}")

bulk_walk is faster than walk because it retrieves multiple OIDs per request. Use bulk_size=25 for most cases.

SET Operations

set

Set an OID value (v2c/v3 only).

async def set(self, oid: str, value: Value) -> None
from snmpkit.core import Value await mgr.set("1.3.6.1.2.1.1.5.0", Value.OctetString(b"new-hostname")) await mgr.set("1.3.6.1.2.1.1.6.0", Value.OctetString(b"Server Room"))

Value Types

TypeExample
Value.Integer(n)Value.Integer(42)
Value.OctetString(b)Value.OctetString(b"hello")
Value.ObjectIdentifier(oid)Value.ObjectIdentifier(Oid("1.3.6.1"))
Value.IpAddress(a, b, c, d)Value.IpAddress(192, 168, 1, 1)
Value.Counter32(n)Value.Counter32(1000)
Value.Gauge32(n)Value.Gauge32(50)
Value.TimeTicks(n)Value.TimeTicks(12345)
Value.Counter64(n)Value.Counter64(1000000)

Table Operations

get_table

Retrieve a structured SNMP table.

async def get_table( self, base_oid: str, columns: list[int] | None = None, bulk_size: int = 25, ) -> dict[tuple[int, ...], dict[int, Value]]
table = await mgr.get_table("1.3.6.1.2.1.2.2.1", columns=[2, 8]) for index, cols in table.items(): print(f"Row {index}: {cols}")

See Tables for detailed usage.

Trap/Inform Operations

send_trap

Send an SNMPv2c/v3 Trap (fire-and-forget).

async def send_trap( self, trap_oid: str, varbinds: list[tuple[str, Value]] | None = None, uptime: int | None = None, ) -> None
await mgr.send_trap( trap_oid="1.3.6.1.4.1.12345.0.1", varbinds=[("1.3.6.1.4.1.12345.1.1.0", Value.Integer(95))], uptime=123456, )

send_inform

Send an SNMPv2c/v3 Inform and wait for acknowledgement.

async def send_inform( self, trap_oid: str, varbinds: list[tuple[str, Value]] | None = None, uptime: int | None = None, ) -> None
await mgr.send_inform( trap_oid="1.3.6.1.4.1.12345.0.1", varbinds=[("1.3.6.1.4.1.12345.1.1.0", Value.Integer(95))], )

See Traps for detailed usage.

Concurrent Polling

poll_many

Poll multiple targets concurrently:

from snmpkit.manager import PollTarget, poll_many targets = [ PollTarget(host="10.0.0.1"), PollTarget(host="10.0.0.2", community="private"), PollTarget(host="10.0.0.3", version=3, user="admin", auth_protocol="SHA256", auth_password="pass"), ] oids = ["1.3.6.1.2.1.1.1.0", "1.3.6.1.2.1.1.3.0"] async for result in poll_many(targets, oids, concurrency=50): if result.error: print(f"{result.target}: ERROR {result.error}") else: print(f"{result.target}: {result.oid} = {result.value}")

See PollTarget and PollResult in Advanced.

Exceptions

from snmpkit.manager.exceptions import ( SnmpError, # Base exception TimeoutError, # Request timed out NoSuchObjectError, # OID does not exist NoSuchInstanceError, # Instance does not exist EndOfMibViewError, # End of MIB reached GenericError, # SNMP error status )

GenericError

Contains the SNMP error status and index:

try: await mgr.set("1.3.6.1.2.1.1.1.0", Value.Integer(1)) except GenericError as e: print(f"Error status: {e.status}") print(f"Error index: {e.index}")

Error status codes:

CodeNameDescription
1tooBigResponse too large
2noSuchNameOID not found (v1)
3badValueInvalid value
4readOnlyCannot modify
5genErrGeneral error
6noAccessAccess denied
7wrongTypeWrong value type
Last updated on