{"id":4207,"library":"pysnmp","title":"pysnmp","description":"pysnmp is a robust, open-source Python library for implementing SNMP agents and managers. It provides comprehensive support for SNMP v1, v2c, and v3, including various transport mappings (UDP, TCP) and security models (USM, VACM). The library is currently at version 7.1.23 and follows a relatively active release cadence, addressing bug fixes and minor enhancements.","status":"active","version":"7.1.23","language":"en","source_language":"en","source_url":"https://github.com/lextudio/pysnmp","tags":["SNMP","network","monitoring","network management","MIB"],"install":[{"cmd":"pip install pysnmp","lang":"bash","label":"Install stable version"}],"dependencies":[{"reason":"Required by pysnmp for core functionality as part of its packaging structure.","package":"pysnmp-lextudio","optional":false},{"reason":"Optional dependency for cryptographic operations, primarily used with SNMPv3 security features.","package":"pycryptodome","optional":true}],"imports":[{"note":"The 'hlapi' (High-Level API) is the recommended way to interact with pysnmp for most common tasks (e.g., GET, SET, WALK). It abstracts away much of the low-level PDU construction.","symbol":"*","correct":"from pysnmp.hlapi import *"},{"symbol":"SnmpEngine","correct":"from pysnmp.hlapi import SnmpEngine"},{"symbol":"CommunityData","correct":"from pysnmp.hlapi import CommunityData"},{"symbol":"UdpTransportTarget","correct":"from pysnmp.hlapi import UdpTransportTarget"},{"symbol":"ContextData","correct":"from pysnmp.hlapi import ContextData"},{"symbol":"ObjectType","correct":"from pysnmp.hlapi import ObjectType"},{"symbol":"ObjectIdentity","correct":"from pysnmp.hlapi import ObjectIdentity"},{"note":"While 'cmdgen' from 'oneliner' used to be common in older versions (e.g., pysnmp4), the 'hlapi' functions like 'getCmd', 'nextCmd', 'setCmd' are the preferred and more robust way in pysnmp 5.x+.","wrong":"from pysnmp.entity.rfc3413.oneliner import cmdgen","symbol":"cmdgen","correct":"from pysnmp.hlapi import getCmd"}],"quickstart":{"code":"import os\nfrom pysnmp.hlapi import (\n    SnmpEngine, CommunityData, UdpTransportTarget,\n    ContextData, ObjectType, ObjectIdentity, getCmd\n)\n\ndef snmp_get(host, community, oid):\n    errorIndication, errorStatus, errorIndex, varBinds = next(\n        getCmd(\n            SnmpEngine(),\n            CommunityData(community, mpModel=0), # mpModel=0 for SNMPv1, 1 for SNMPv2c\n            UdpTransportTarget((host, 161)),\n            ContextData(),\n            ObjectType(ObjectIdentity(oid))\n        )\n    )\n\n    if errorIndication:\n        return f\"Error: {errorIndication}\"\n    elif errorStatus:\n        return f\"Error: {errorStatus.prettyPrint()} at {varBinds[int(errorIndex)-1] if errorIndex else '?'}\"\n    else:\n        return ', '.join([f'{x[0].prettyPrint()} = {x[1].prettyPrint()}' for x in varBinds])\n\n\n# Example Usage:\n# You can use a public SNMP demo server for testing, e.g., 'demo.snmplabs.com'\n# Make sure to replace these with actual values for production environments\nSNMP_HOST = os.environ.get('SNMP_HOST', 'demo.snmplabs.com')\nSNMP_COMMUNITY = os.environ.get('SNMP_COMMUNITY', 'public')\n# OID for sysDescr.0 (system description)\nSNMP_OID = os.environ.get('SNMP_OID', '1.3.6.1.2.1.1.1.0')\n\nif __name__ == '__main__':\n    print(f\"Attempting SNMP GET on {SNMP_HOST} for OID {SNMP_OID} with community '{SNMP_COMMUNITY}'\")\n    result = snmp_get(SNMP_HOST, SNMP_COMMUNITY, SNMP_OID)\n    print(f\"SNMP GET Result: {result}\")","lang":"python","description":"This quickstart demonstrates how to perform a basic SNMP GET request using the high-level API (`pysnmp.hlapi`). It fetches a single OID (sysDescr.0 by default) from a specified host using a community string (SNMPv2c). The example handles potential error conditions returned by the SNMP agent."},"warnings":[{"fix":"Rewrite existing code to use the `pysnmp.hlapi` or the updated low-level API constructs. Refer to the official `pysnmp` documentation for migration guides and new API examples.","message":"Major API changes when migrating from `pysnmp4` to `pysnmp` (version 5.x and later). `pysnmp4` is a completely separate package and its API is incompatible with the modern `pysnmp` library.","severity":"breaking","affected_versions":"All versions when migrating from 'pysnmp4' to 'pysnmp >= 5.0'"},{"fix":"For standard OIDs, `hlapi` often handles MIB resolution automatically. For custom MIBs, explicitly use `mibBuilder.loadModules()` and `mibBuilder.addMibSources()` to point to the correct MIB files and directories. Ensure MIB files are accessible and correctly formatted.","message":"MIB (Management Information Base) loading and management can be complex, especially for custom or non-standard MIB files. Users often struggle with configuring `mibBuilder` correctly.","severity":"gotcha","affected_versions":"All versions"},{"fix":"For simple scripts, the synchronous `hlapi` (as in the quickstart) is sufficient. For event-driven applications, explicitly use `pysnmp.hlapi.asyncio` or `pysnmp.hlapi.twisted` with their respective `SnmpEngine` implementations and ensure your application's event loop is properly integrated.","message":"pysnmp offers both synchronous and asynchronous (Twisted, asyncio) APIs. Mixing these or misunderstanding which API flavor is being used can lead to blocking issues in event-driven applications or unexpected behavior.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Always install `pysnmp` directly (`pip install pysnmp`). The package manager will automatically pull in `pysnmp-lextudio`. Avoid installing `pysnmp-lextudio` separately unless explicitly required for debugging or advanced scenarios, as it's primarily an internal component.","message":"The packaging structure for `pysnmp` (version 7.x) includes a dependency on `pysnmp-lextudio`, which can cause confusion. While `pysnmp-lextudio` is a required dependency, it often appears as a minimal, internal package.","severity":"gotcha","affected_versions":"pysnmp >= 7.0"}],"env_vars":null,"last_verified":"2026-04-11T00:00:00.000Z","next_check":"2026-07-10T00:00:00.000Z"}