PySNMP

repository·main·Indexed 41 days ago

https://github.com/lextudio/pysnmp

A pure-Python, open-source implementation of the SNMP (v1, v2c, and v3) protocol engines. Version 7.1.27 supports asynchronous IO via asyncio, dynamic MIB compilation through PySMI, and USM security for SNMPv3. It provides high-level APIs (v1arch and v3arch) for operations such as GET, SET, WALK, and sending TRAP/INFORM notifications.

Tokens
31K
Snippets
117
Records
148
Agent score
98%

What's inside PySNMP

  1. Overview of PySNMP features

    main

    PySNMP is a pure-Python implementation of the SNMP v1, v2c, and v3 engines.

    Core Capabilities:

    • Protocol Support: Complete SNMPv1/v2c and SNMPv3 support based on IETF RFC standards.
    • SMI Framework: Resolves MIB information and implements SMI Managed Objects.
    • Transports: Extensible network transports supporting UDP/IPv4 and UDP/IPv6.
    • Async IO: Supports asynchronous socket-based IO via asyncio integration.
    • MIB Management: Integration with PySMI for dynamic MIB compilation.
    • Thread Safety: MT-safe if SnmpEngine is used as thread-local.

    SNMPv3 Specific Features:

    • USM Security: Authentication (MD5, SHA-1, SHA-2) and privacy (DES, AES) protocols (RFC3414, RFC7860).
    • Access Control: View-based access control (VACM) for any SNMP model (RFC3415).
    • Proxying: Built-in SNMP proxy PDU converter for multi-lingual SNMP entities (RFC2576).
    • Engine Management: Remote SNMP engine configuration and optional engine discovery.
  2. Overview of PySNMP capabilities

    main

    PySNMP is a cross-platform, pure-Python SNMP engine implementation. It is designed to handle complex SNMP requirements and can operate in several roles:

    • Agent: Acts as an SNMP agent.
    • Manager: Acts as an SNMP manager.
    • Proxy: Acts as an SNMP proxy.

    It supports SNMP v1, v2c, and v3 protocol versions over IPv4, IPv6, and other network transports. It is recommended for new applications and for migrating from obsolete PySNMP releases. It requires Python 3.10+.

  3. Current limitations and future roadmap of PySNMP

    main

    PySNMP is a mature library, but it has several areas of ongoing development and known limitations that users should be aware of:

    Transport Limitations

    • Socket Support: The transport subsystem currently only supports datagram-type network sockets (UDP-over-IPv4 and UDP-over-IPv6). It does not yet support stream-oriented protocols like TCP-over-IPv4, TCP-over-IPv6, or SSL/TLS transports.

    MIB and SMI Handling

    • MIB Parsing: Historically, PySNMP required an off-line third-party tool to convert ASN.1-based MIB texts into Python modules. This has been addressed by the integration of the PySMI project.
    • Reverse MIB Index: There is currently no automated mechanism for locating and loading MIB files based on arbitrary OIDs to provide human-friendly names or table indices automatically.
    • SMI Implementation: The current SMI (Structure of Management Information) uses live Python objects. While flexible, it is not yet natively integrated with DBMS-based Management Instrumentation.

    Agent-Side Functionality

    • Production Readiness: PySNMP does not provide a complete or production-grade SNMP agent implementation. Agent-side APIs and examples are provided for reference only, with no guarantee of completeness or ongoing feature development. Agent functionality is primarily maintained for unit tests and compatibility with snmpsim.
  4. Using PySNMP with asyncio

    main
    PySNMP provides native asyncio-backed interfaces designed to work with Python's asyncio module. While asyncio can functionally replace the older asynchronous I/O libraries used in original PySNMP versions, using PySNMP's built-in asyncio interfaces is recommended if you need to embed an SNMP stack into an existing asyncio-based application. This simplifies integration by handling the complexities of coroutines and I/O multiplexing. All standard SNMP functionality (RFC 3413) remains available through these asynchronous interfaces.
  5. Choose the right API for your SNMP application type

    main

    Depending on whether you are building a Manager (client) or an Agent (server), you should target different components:

    For SNMP Managers (Clients)

    • Command Generator: Use this to send requests (fetching or modifying variables, walking MIB trees).
    • Notification Receiver: Use this to listen for and process incoming SNMP notifications (traps).

    For SNMP Agents (Servers)

    • Command Responder: Use this to implement an agent that responds to incoming SNMP requests.
    • Notification Originator: Use this to send SNMP notifications (traps) from your agent to a manager.

    For MIB Operations

    • MIB Access: Use SMI (Structure of Management Information) services to browse MIB trees or implement custom MIB objects.
  6. Use the high-level asyncio API (v3arch)

    main
    PySNMP provides a layered architecture. For modern applications, use the v3arch asyncio API. This architecture is designed for SNMP v3 and supports asynchronous operations using Python's asyncio module. It includes a Command Generator for operations like GET, SET, and WALK, and a Notification Originator for sending TRAP/INFORM messages.
  7. Create an SNMP Engine with SnmpEngine

    main

    The SnmpEngine class is the central, umbrella object for all PySNMP operations. An application can run multiple independent SNMP engines by creating multiple SnmpEngine instances. Each engine has a unique identifier used in protocol operations.

    Important: SnmpEngine allocates significant resources. You must call the close_dispatcher() method when you are finished with the engine to release these resources.

    from pysnmp.hlapi.v3arch.asyncio import *
    
    # Initialize the engine
    engine = SnmpEngine()
    
    # ... perform operations ...
    
    # Clean up resources
    engine.close_dispatcher()
  8. How to use plain-text (ASN.1) MIBs with PySNMP

    main

    Starting from PySNMP 4.3.x, you can use plain-text (ASN.1) MIBs by allowing PySNMP to automatically parse them using the PySMI tool. When PySNMP calls PySMI, the resulting PySNMP-formatted MIB is cached in $HOME/.pysnmp/mibs/ by default.

    You can configure the MIB compiler to search for these plain-text MIBs in multiple local or remote locations (such as https://mibs.pysnmp.com/asn1/@mib@).

    from pysnmp import hlapi
    from pysnmp.smi import compiler
    import os
    
    engine = hlapi.Engine()
    builder = engine.get_mib_builder()
    compiler.add_mib_compiler(builder, sources=[
        '/usr/share/snmp/mibs',
        os.path.expanduser('~/.snmp/mibs'),
        'https://mibs.pysnmp.com/asn1/@mib@',
    ])
  9. Understand PySNMP High-Level API architectures

    main

    PySNMP provides two distinct high-level API architectures based on your requirements:

    • v3arch: A complete implementation of the SNMPv3 architecture. It supports all SNMP versions (v1, v2c, and v3) and is the recommended choice for modern applications.
    • v1arch: A simplified architecture primarily designed for SNMPv1 and SNMPv2c operations, offering less complexity for basic tasks.

    This tutorial focuses on the v3arch asyncio-based API.

  10. Why you cannot share a single SnmpEngine across multiple threads

    main
    A SnmpEngine instance is bound to the specific asyncio event loop that first issues a network request through it. When using asyncio.run(), the event loop is closed and destroyed upon completion. If you attempt to reuse the same SnmpEngine in a subsequent asyncio.run() call (even in a different thread), the engine's internal AsyncioDispatcher will still hold a reference to the old, closed event loop. This causes requests to hang indefinitely because they are being dispatched to a loop that no longer exists.
  11. PySNMP dependencies and feature enablement

    main

    PySNMP's functionality depends on the presence of specific packages in your environment:

    • pyasn1: A required dependency.
    • pysmi: If this package is installed, MIB services are enabled.
    • cryptography: If this package is installed, strong SNMPv3 encryption is enabled.
  12. Use the high-level v1arch asyncio API

    main
    The v1arch asyncio API is a simplified version of the v3arch API, intended specifically for users who only need to perform SNMP v1 and v2c operations without the complexity of the full SNMP v3 stack. It requires an SnmpDispatcher object, which represents the asynchronous I/O event loop and holds caches.