Skip to main content

A user-friendly SNMP library

Project description

Introduction

I have rewritten this library entirely since version 0.1.7, the last published version. The idea behind versions 0.1.x was to support SNMPv1 first, and then add support for SNMPv2c and SNMPv3 later. I decided some time ago that it would probably be better to take the opposite approach. This latest version (which I hope to soon release as v0.2.0) currently supports only SNMPv3. I plan to eventually add support for SNMPv1 and SNMPV2c, and I believe the current design is flexible enough that that should be pretty straightforward. This library is only suited to perform the role of a "manager" (more specifically, it can act as a Command Generator, not a Notification Receiver). One day it may support other roles.

Usage Overview

The conceptual model for using this library is to create a single Engine instance, and then call the Engine.Manager() factory function to instantiate a manager object for each remote SNMP engine. Each manager object defines methods for each type of request (Get, GetNext, Set, and GetBulk). By default, each request will block until a response has been received, in which case the return value will be a ResponsePDU instance. The manager can also be configured to return a request handle object without blocking (or it can be configured for a single request). This allows multiple requests to be in-flight at once. To access the response, call the wait() method of the request handle, which may block, and which returns a ResponsePDU.

User-based security is managed through the Engine object. The addUser method allows you to specify an authentication protocol, authentication secret, privacy protocol, and privacy secret for each user name. There is a possibility, however, that you may have multiple agents that use the same user name with different security configurations. To account for this possibility, the addUser function accepts a "namespace" parameter. The default namespace is identified by the empty string (""), but you may provide alternate credentials for a duplicate user name by providing a unique namespace identifier string. The namespace identifier must also be provided to the Manager() factory function, so that it can select the correct credentials for the remote engine that it manages.

There are plenty of other features to cover in some of the lower level classes. Thorough documentation is one of my main requirements before I am willing to bump the version to 1.0.0. For now, I will just mention that the snmp.types.OID type is meant to make it simple to interpret object indices, both to simplify "walk" operations, and to make it easy to correlate related MIB objects that use the same index. For example, you can create an OID object for ifDescr by calling OID.parse(".1.3.6.1.2.1.2.2.1.2"). Then, to process the result of a GetNext operation, call the extractIndex() method of the OID object for the variable binding as follows:

try:
    index = varbind.name.extractIndex(ifDescr, Integer)
except OID.BadPrefix:
    # object is not an instance of ifDescr
except OID.IndexDecodeError:
    # the OID could not be properly parsed using
    # the type(s) you provided to extractIndex
else:
    print(f"ifDescr.{index.value} = \"{varbind.value.data.decode()}\"")

The output should look like this (assume interface 1 is named "loopback"):

ifDescr.1 = "loopback"

Working Example

from snmp.engine import Engine
from snmp.security.usm.auth import *
from snmp.security.usm.priv import *
from snmp.types import *

sysDescr = OID.parse("1.3.6.1.2.1.1.1")
ifDescr = OID.parse("1.3.6.1.2.1.2.2.1.2")

# autowait=False will cause each request to return a handle rather than blocking
with Engine(autowait=False) as engine:
    engine.addUser(
        "sample-user",
        authProtocol=HmacSha256,
        authSecret=b"sample-auth-secret",
        privProtocol=Aes128Cfb,
        privSecret=b"sample-priv-secret",
    )

    # you can use autowait=True/False when creating a Manager
    hostA = engine.Manager("192.168.0.1")
    hostB = engine.Manager("192.168.0.2", autowait=True)

    # you can use wait=True/False on any single request as well
    requestA = hostA.get(sysDescr.extend(0))
    requestB = hostB.getBulk(ifDescr, maxRepetitions=4, wait=False)

    responseA = requestA.wait()
    varbind = responseA.variableBindings[0]
    print(f"{varbind.name} = \"{varbind.value.data.decode()}\"")

    responseB = requestB.wait()

    # quasi-infinite loop with a safety valve
    for i in range(100):
        for varbind in responseB.variableBindings:
            oid, value = varbind

            try:
                ifIndex = oid.extractIndex(ifDescr, Integer)
            except OID.BadPrefix:
                break

            print(f"ifDescr.{ifIndex.value} = \"{value.data.decode()}\"")
        else:
            responseB = hostB.getBulk(oid, maxRepetitions=4)
            continue

        break

Installation Notes

The USM privacy module depends on OpenSSL. Windows wheels use statically linked libraries so you should be able to install python-snmp without the need to install OpenSSL. If a wheel is not available for your platform, you will need the following environment variables to allow pip to build the sdist:

set CL="-I<path-to-OpenSSL>\include"
set LINK="/LIBPATH:<path-to-OpenSSL>\lib"

It is also possible to use python-snmp without OpenSSL if you do not need to use the USM privacy features. However, you will need to tweak setup.py to get it to install. Clone this directory, remove the cffi_modules argument to setup in setup.py, and then call pip install <path-to-clone>.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

snmp-0.2.0.tar.gz (37.6 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

snmp-0.2.0-cp311-cp311-win_amd64.whl (60.0 kB view details)

Uploaded CPython 3.11Windows x86-64

snmp-0.2.0-cp310-cp310-win_amd64.whl (60.0 kB view details)

Uploaded CPython 3.10Windows x86-64

snmp-0.2.0-cp39-cp39-win_amd64.whl (60.0 kB view details)

Uploaded CPython 3.9Windows x86-64

snmp-0.2.0-cp38-cp38-win_amd64.whl (60.0 kB view details)

Uploaded CPython 3.8Windows x86-64

snmp-0.2.0-cp37-cp37m-win_amd64.whl (60.0 kB view details)

Uploaded CPython 3.7mWindows x86-64

snmp-0.2.0-cp36-cp36m-win_amd64.whl (60.5 kB view details)

Uploaded CPython 3.6mWindows x86-64

File details

Details for the file snmp-0.2.0.tar.gz.

File metadata

  • Download URL: snmp-0.2.0.tar.gz
  • Upload date:
  • Size: 37.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.7.13

File hashes

Hashes for snmp-0.2.0.tar.gz
Algorithm Hash digest
SHA256 9b7a8b1e7b674d18d96ef2bdbd15c56f985d0e2bd42d245ccf5b8d3b0cfa5f78
MD5 721e74de06bc9c70fa8d081d8dec0310
BLAKE2b-256 2f909f9f933da1a8848c5d1a11815d47ec45cf836790836fe224304cb9d782b2

See more details on using hashes here.

File details

Details for the file snmp-0.2.0-cp311-cp311-win_amd64.whl.

File metadata

  • Download URL: snmp-0.2.0-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 60.0 kB
  • Tags: CPython 3.11, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.7.13

File hashes

Hashes for snmp-0.2.0-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 90526a9cd9b331d97cd762fed6732c52bf5cbd42825f5054f0c8f43efaad3162
MD5 46b6f3fe7fa7e8456af472aed671d13b
BLAKE2b-256 77c420ce297192799f633f86b620d9f1e8758a6123ec16454e3bf078f41a4438

See more details on using hashes here.

File details

Details for the file snmp-0.2.0-cp310-cp310-win_amd64.whl.

File metadata

  • Download URL: snmp-0.2.0-cp310-cp310-win_amd64.whl
  • Upload date:
  • Size: 60.0 kB
  • Tags: CPython 3.10, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.7.13

File hashes

Hashes for snmp-0.2.0-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 8314654ab4678263001769088c517d4fb455566bdddf6531347e72a6d8914d92
MD5 6bdbce01c480cc5cb4b8d7ad6a868171
BLAKE2b-256 501b555545a71825dd8ae177803be47fc6446cbe4bd9f458ef5f29f1e2dd2f33

See more details on using hashes here.

File details

Details for the file snmp-0.2.0-cp39-cp39-win_amd64.whl.

File metadata

  • Download URL: snmp-0.2.0-cp39-cp39-win_amd64.whl
  • Upload date:
  • Size: 60.0 kB
  • Tags: CPython 3.9, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.7.13

File hashes

Hashes for snmp-0.2.0-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 ae1fa891011d604fcfec4ff72cab2477f01f629fa5c1698a6fdd8e6b39f89406
MD5 0f4eab375cdfdd995adf7943ff970e0f
BLAKE2b-256 11644c817e88854b104cb1baa47e4addf926228f14824dc8625db206fe6199b2

See more details on using hashes here.

File details

Details for the file snmp-0.2.0-cp38-cp38-win_amd64.whl.

File metadata

  • Download URL: snmp-0.2.0-cp38-cp38-win_amd64.whl
  • Upload date:
  • Size: 60.0 kB
  • Tags: CPython 3.8, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.7.13

File hashes

Hashes for snmp-0.2.0-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 0ca925f1db3b37f2b2e6e06cfd7a8696daf755d615f2d24023becb0f56e8cc8d
MD5 0fdc555715cadef34d595ddb3a2da414
BLAKE2b-256 649b7ed63d4fce4ff88d394d766f748341c192241b4e4ad7ee56ad70bbc914df

See more details on using hashes here.

File details

Details for the file snmp-0.2.0-cp37-cp37m-win_amd64.whl.

File metadata

  • Download URL: snmp-0.2.0-cp37-cp37m-win_amd64.whl
  • Upload date:
  • Size: 60.0 kB
  • Tags: CPython 3.7m, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.7.13

File hashes

Hashes for snmp-0.2.0-cp37-cp37m-win_amd64.whl
Algorithm Hash digest
SHA256 abc777cf01cbf3839e0355207e4c73a1aff77d6015afd42e6c25fc7c6b8f69ef
MD5 68aeda20f5cbf40ba66457a74b945637
BLAKE2b-256 9783772362ca27ca608ce89715e29eca766a96df3a3df4ef959740b70eeaca61

See more details on using hashes here.

File details

Details for the file snmp-0.2.0-cp36-cp36m-win_amd64.whl.

File metadata

  • Download URL: snmp-0.2.0-cp36-cp36m-win_amd64.whl
  • Upload date:
  • Size: 60.5 kB
  • Tags: CPython 3.6m, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.7.13

File hashes

Hashes for snmp-0.2.0-cp36-cp36m-win_amd64.whl
Algorithm Hash digest
SHA256 ddbf52742a3085f1ac424b945139d3324d6e586dac1c637751f69eed404cc9d5
MD5 fa9ff40c079c1dfb7029149107f36cc9
BLAKE2b-256 1606f44e3ca22853dde05c64102c4835d1cad9856c5738ff7c22c86e0b68b7c6

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page