Skip to content

gufo.snmp.sync_client

Sync SnmpSession.

SnmpSession

Bases: object

Synchronous SNMP client session.

Should be used either directly or via context manager.

Parameters:

Name Type Description Default
addr str

SNMP agent address, either IPv4 or IPv6.

required
port int

SNMP agent port.

161
community str

SNMP community (v1, v2c).

'public'
engine_id Optional[bytes]

SNMP Engine id (v3).

None
user Optional[User]

User instance (v3).

None
version Optional[SnmpVersion]

Protocol version. Autodetect if omitted:

  • v3: if user is set.
  • v2c: otherwise.
None
timeout float

Request timeout in seconds.

10.0
tos int

Set ToS/DSCP mark on egress packets.

0
send_buffer int

Send buffer size for UDP socket. 0 - use default size.

0
recv_buffer int

Receive buffer size for UDP socket. 0 - use default size.

0
max_repetitions int

Default max_repetitions for getbulk.

20
allow_bulk bool

Allow using GETBULK in SnmpSession.fetch() whenever possible.

True
policer Optional[BasePolicer]

Optional BasePolicer instance to limit outgoing requests. Overrides limit_rps parameter.

None
limit_rps Optional[Union[int, float]]

Limit outgouing requests to limit_rps requests per second.

None
Example
session = SnmpSession("127.0.0.1")
r = await session.get("1.3.6.1.2.1.1.6.0")
Example
async with SnmpSession("127.0.0.1") as session:
    r = await session.get("1.3.6.1.2.1.1.6.0")

__enter__()

Asynchronous context manager entry.

__exit__(exc_type, exc_val, exc_tb)

Asynchronous context manager exit.

fetch(oid)

Iterate over oids using fastest method available.

When SnmpSession's allow_bulk is set, use SnmpSession.getbulk() on SNMPv2, otherwise use SnmpSession.getnext().

Parameters:

Name Type Description Default
oid str

Starting oid

required

Returns:

Type Description
Iterator[Tuple[str, ValueType]]

Asynchronous iterator yielding pair of (oid, value)

Example
async for oid, value in session.fetch("1.3.6"):
    print(oid, value)

get(oid)

Send SNMP GET request and await for response.

Parameters:

Name Type Description Default
oid str

OID in numeric format, no leading dot.

required

Returns:

Type Description
ValueType

Request result. Return type depends on requested oid.

Raises:

Type Description
ValueError

On invalid oid format.

OSError

When unable to send request.

TimeoutError

When timed out.

NoSuchInstance

When requested key is not found.

SnmpError

On other SNMP-related errors.

get_engine_id()

Get effective engine id.

Returns:

Type Description
bytes

Engine id as bytes.

get_many(oids)

Send SNMP GET request for multiple oids and await for response.

Parameters:

Name Type Description Default
oids Iterable[str]

Iterable of oids in numeric format, no leading dots.

required

Returns:

Type Description
Dict[str, ValueType]

Dict where keys are requested oids, values are returned values.

Dict[str, ValueType]

Types of values are depends on requested oids.

Note

There is no guarante that all requested oids are present in result dict. Some values may be missed if not returned by agent.

Raises:

Type Description
ValueError

On invalid oid format.

OSError

When unable to send request.

TimeoutError

When timed out.

RuntimeError

On Python runtime failure.

SnmpError

On other SNMP-related errors.

getbulk(oid, max_repetitions=None)

Iterate over oids.

Parameters:

Name Type Description Default
oid str

Starting oid

required
max_repetitions Optional[int]

Maximal amount of items per response. Override the SnmpSession's defaults.

None

Returns:

Type Description
Iterator[Tuple[str, ValueType]]

Asynchronous iterator yielding pair of (oid, value)

Example
async for oid, value in session.getbulk("1.3.6"):
    print(oid, value)

getnext(oid)

Iterate over oids.

Parameters:

Name Type Description Default
oid str

Starting oid

required

Returns:

Type Description
Iterator[Tuple[str, ValueType]]

Asynchronous iterator yielding pair of (oid, value)

Example
async for oid, value in session.getnext("1.3.6"):
    print(oid, value)

refresh()

Send and receive REPORT to refresh authentication state.

SNMPv3 only.

Refresh sent automatically on entering the SnmpSession and should be resent manually if over 150 seconds left from the last request.