A comprehensive Python SNMP library supporting v1/v2c/v3 with authentication and privacy protocols
npx @tessl/cli install tessl/pypi-pysnmp@7.1.0A comprehensive Pure Python SNMP library supporting SNMP v1, v2c, and v3 protocols with complete authentication and privacy capabilities. PySNMP provides both high-level and low-level APIs for building SNMP agents, managers, and network monitoring applications, with full compliance to IETF RFC standards.
pip install pysnmpimport pysnmpFor high-level async operations (recommended):
from pysnmp.hlapi.v3arch.asyncio import *For legacy imports:
from pysnmp.hlapi.asyncio import *For specific components:
from pysnmp.hlapi.v3arch.asyncio import (
SnmpEngine, CommunityData, UsmUserData,
UdpTransportTarget, ContextData,
get_cmd, set_cmd, walk_cmd, is_end_of_mib
)For data types and SMI objects:
from pysnmp.proto.rfc1902 import (
Integer32, OctetString, ObjectIdentifier, Counter32,
Gauge32, TimeTicks, Counter64, Bits, IpAddress
)
from pysnmp.smi.rfc1902 import ObjectIdentity, ObjectType, NotificationType
from pysnmp.proto.rfc1905 import EndOfMibView, NoSuchObject, NoSuchInstanceimport asyncio
from pysnmp.hlapi.v3arch.asyncio import *
async def snmp_get():
errorIndication, errorStatus, errorIndex, varBinds = await get_cmd(
SnmpEngine(),
CommunityData('public'),
await UdpTransportTarget.create(('demo.pysnmp.com', 161)),
ContextData(),
ObjectType(ObjectIdentity('1.3.6.1.2.1.1.1.0'))
)
if errorIndication:
print(f"Error: {errorIndication}")
elif errorStatus:
print(f"Error: {errorStatus.prettyPrint()}")
else:
for varBind in varBinds:
print(f"{varBind[0]} = {varBind[1]}")
# Run the async function
asyncio.run(snmp_get())import asyncio
from pysnmp.hlapi.v3arch.asyncio import *
async def snmpv3_get():
errorIndication, errorStatus, errorIndex, varBinds = await get_cmd(
SnmpEngine(),
UsmUserData('testuser',
authKey='authenticationkey',
authProtocol=USM_AUTH_HMAC96_SHA),
await UdpTransportTarget.create(('demo.pysnmp.com', 161)),
ContextData(),
ObjectType(ObjectIdentity('1.3.6.1.2.1.1.1.0'))
)
if not errorIndication and not errorStatus:
for varBind in varBinds:
print(f"{varBind[0]} = {varBind[1]}")
asyncio.run(snmpv3_get())import asyncio
from pysnmp.hlapi.v3arch.asyncio import *
async def snmp_walk():
async for (errorIndication, errorStatus, errorIndex, varBinds) in walk_cmd(
SnmpEngine(),
CommunityData('public'),
await UdpTransportTarget.create(('demo.pysnmp.com', 161)),
ContextData(),
ObjectType(ObjectIdentity('1.3.6.1.2.1.1')), # Walk system subtree
lexicographicMode=False
):
if errorIndication:
print(f"Error: {errorIndication}")
break
elif errorStatus:
print(f"Error: {errorStatus.prettyPrint()}")
break
else:
for varBind in varBinds:
print(f"{varBind[0]} = {varBind[1]}")
asyncio.run(snmp_walk())PySNMP is built around a layered architecture that provides flexibility from high-level convenience functions to low-level protocol control:
The library supports both SNMPv3 architecture (recommended) and legacy v1 architecture, with full backward compatibility and seamless integration with asyncio for modern Python applications.
The primary interface for SNMP operations, providing async functions for GET, SET, WALK, and notification operations with built-in error handling and transport management.
async def get_cmd(snmpEngine, authData, transportTarget, contextData, *varBinds, **options): ...
async def set_cmd(snmpEngine, authData, transportTarget, contextData, *varBinds, **options): ...
async def next_cmd(snmpEngine, authData, transportTarget, contextData, *varBinds, **options): ...
async def bulk_cmd(snmpEngine, authData, transportTarget, contextData, *varBinds, **options): ...
async def walk_cmd(snmpEngine, authData, transportTarget, contextData, *varBinds, **options): ...
async def bulk_walk_cmd(snmpEngine, authData, transportTarget, contextData, *varBinds, **options): ...
async def send_notification(snmpEngine, authData, transportTarget, contextData, notifyType, *varBinds, **options): ...
def is_end_of_mib(varBinds): ...Comprehensive authentication and privacy protocols for SNMP v1/v2c community strings and SNMPv3 User-based Security Model (USM) with multiple authentication and encryption algorithms.
class CommunityData: ...
class UsmUserData: ...
# Authentication Protocols
USM_AUTH_NONE: int
USM_AUTH_HMAC96_MD5: int
USM_AUTH_HMAC96_SHA: int
USM_AUTH_HMAC128_SHA224: int
USM_AUTH_HMAC192_SHA256: int
USM_AUTH_HMAC256_SHA384: int
USM_AUTH_HMAC384_SHA512: int
# Privacy Protocols
USM_PRIV_NONE: int
USM_PRIV_CBC56_DES: int
USM_PRIV_CBC168_3DES: int
USM_PRIV_CFB128_AES: int
USM_PRIV_CFB192_AES: int
USM_PRIV_CFB256_AES: int
USM_PRIV_CFB192_AES_BLUMENTHAL: int
USM_PRIV_CFB256_AES_BLUMENTHAL: intComplete implementation of SNMP data types from RFC 1902 and RFC 1905, including integers, strings, object identifiers, counters, gauges, and specialized exception values.
class Integer32: ...
class OctetString: ...
class ObjectIdentifier: ...
class Counter32: ...
class Counter64: ...
class Gauge32: ...
class TimeTicks: ...
class IpAddress: ...
class Opaque: ...
class Bits: ...
# Exception Values
class NoSuchObject: ...
class NoSuchInstance: ...
class EndOfMibView: ...
class UnSpecified: ...SNMP Data Types and Exceptions
SNMP engine management, transport targets for UDP/IPv4 and IPv6 communication, and context data for SNMP operations.
class SnmpEngine: ...
class UdpTransportTarget: ...
class Udp6TransportTarget: ...
class ContextData: ...Structure of Management Information objects for working with MIB variables, object identities, and SNMP notifications with automatic OID resolution.
class ObjectIdentity: ...
class ObjectType: ...
class NotificationType: ...