Skip to main content

A Python library for SNMP

Project description

SNMP Library for Python

Become a Sponsor PyPI PyPI Downloads Python Versions GitHub license

This is a pure-Python, open source and free implementation of v1/v2c/v3 SNMP engine distributed under 2-clause BSD license.

The PySNMP project was initially sponsored by a PSF grant. Thank you!

This repo is derived from Ilya Etingof's project etingof/pysnmp, but LeXtudio Inc. has taken over the entire PySNMP ecosystem, including the library, documentation, and the website.

Ilya sadly passed away on 10-Aug-2022. Announcement here. His work is still of great use to the Python community and he will be missed.

Features

  • Complete SNMPv1/v2c and SNMPv3 support
  • SMI framework for resolving MIB information and implementing SMI Managed Objects
  • Complete SNMP entity implementation
  • USM Extended Security Options support (3DES, 192/256-bit AES encryption)
  • Extensible network transports framework (UDP/IPv4, UDP/IPv6)
  • Asynchronous socket-based IO API support
  • Asyncio integration
  • PySMI integration for dynamic MIB compilation
  • Built-in instrumentation exposing protocol engine operations
  • Python eggs and py2exe friendly
  • 100% Python, works with Python 3.8+
  • MT-safe (if SnmpEngine is thread-local)

Features, specific to SNMPv3 model include:

  • USM authentication (MD5/SHA-1/SHA-2) and privacy (DES/AES) protocols (RFC3414, RFC7860)
  • View-based access control to use with any SNMP model (RFC3415)
  • Built-in SNMP proxy PDU converter for building multi-lingual SNMP entities (RFC2576)
  • Remote SNMP engine configuration
  • Optional SNMP engine discovery
  • Shipped with standard SNMP applications (RFC3413)

Download & Install

The PySNMP software is freely available for download from PyPI and GitHub.

Just run:

$ pip install pysnmp

To download and install PySNMP along with its dependencies:

  • PyASN1
  • PySMI (required for MIB services only)
  • Optional pysnmpcrypto package whenever strong SNMPv3 encryption is desired

Besides the library, command-line SNMP utilities written in pure-Python could be installed via:

$ pip install snmpclitools

and used in the very similar manner as conventional Net-SNMP tools:

$ snmpget.py -v3 -l authPriv -u usr-md5-des -A authkey1 -X privkey1 demo.pysnmp.com sysDescr.0
SNMPv2-MIB::sysDescr.0 = STRING: Linux zeus 4.8.6.5-smp #2 SMP Sun Nov 13 14:58:11 CDT 2016 i686

Examples

PySNMP is designed in a layered fashion. Top-level and easiest to use API is known as hlapi. Here's a quick example on how to SNMP GET:

from pysnmp.hlapi.v3arch.asyncio import *
from pysnmp.smi.rfc1902 import ObjectIdentity, ObjectType

with Slim(1) as slim:
    errorIndication, errorStatus, errorIndex, varBinds = await slim.get(
        'public',
        'demo.pysnmp.com',
        161,
        ObjectType(ObjectIdentity("SNMPv2-MIB", "sysDescr", 0)),
    )

    if errorIndication:
        print(errorIndication)
    elif errorStatus:
        print(
            "{} at {}".format(
                errorStatus.prettyPrint(),
                errorIndex and varBinds[int(errorIndex) - 1][0] or "?",
            )
        )
    else:
        for varBind in varBinds:
            print(" = ".join([x.prettyPrint() for x in varBind]))

This is how to send SNMP TRAP:

from pysnmp.hlapi.v3arch.asyncio import *


snmpEngine = SnmpEngine()
errorIndication, errorStatus, errorIndex, varBinds = await sendNotification(
    snmpEngine,
    CommunityData('public', mpModel=0),
    UdpTransportTarget(('demo.pysnmp.com', 162)),
    ContextData(),
    "trap",
    NotificationType(ObjectIdentity("1.3.6.1.6.3.1.1.5.2")).addVarBinds(
        ("1.3.6.1.6.3.1.1.4.3.0", "1.3.6.1.4.1.20408.4.1.1.2"),
        ("1.3.6.1.2.1.1.1.0", OctetString("my system")),
    ),
)

if errorIndication:
    print(errorIndication)

snmpEngine.closeDispatcher()

We maintain publicly available SNMP Agent and TRAP sink at demo.pysnmp.com. You are welcome to use it while experimenting with whatever SNMP software you deal with.

$ python3 examples/hlapi/asyncio/manager/cmdgen/usm-sha-aes128.py
SNMPv2-MIB::sysDescr.0 = SunOS zeus.pysnmp.com 4.1.3_U1 1 sun4m
$
$ python3 examples/hlapi/asyncio/agent/ntforg/v3-inform.py
SNMPv2-MIB::sysUpTime.0 = 0
SNMPv2-MIB::snmpTrapOID.0 = SNMPv2-MIB::warmStart
SNMPv2-MIB::sysName.0 = system name

Other than that, PySNMP is capable to automatically fetch and use required MIBs from HTTP sites or local directories. You could configure any MIB source available to you (including this one) for that purpose.

For more sample scripts please refer to examples section at PySNMP web site.

Documentation

Library documentation can be found at the PySNMP docs site.

If something does not work as expected, please learn the support options.

Pull requests are appreciated! ;-)

Copyright (c) 1999-2020, Ilya Etingof. Copyright (c) 2022-2024, LeXtudio Inc. Copyright (c) 1999-2024, Other PySNMP contributors. All rights reserved.

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

pysnmp-7.0.4.tar.gz (430.0 kB view details)

Uploaded Source

Built Distribution

pysnmp-7.0.4-py3-none-any.whl (283.9 kB view details)

Uploaded Python 3

File details

Details for the file pysnmp-7.0.4.tar.gz.

File metadata

  • Download URL: pysnmp-7.0.4.tar.gz
  • Upload date:
  • Size: 430.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.8.16 Darwin/24.0.0

File hashes

Hashes for pysnmp-7.0.4.tar.gz
Algorithm Hash digest
SHA256 50490c57fc0f31a45f447cf6945990601f2e7fef096e8a75a285e0b42730f9c7
MD5 87e933292b2003d755a57ad51be239c0
BLAKE2b-256 99b5af77e0c3a208872099d9e251ba24bcd54112514ca641e1b93d3daadb7446

See more details on using hashes here.

File details

Details for the file pysnmp-7.0.4-py3-none-any.whl.

File metadata

  • Download URL: pysnmp-7.0.4-py3-none-any.whl
  • Upload date:
  • Size: 283.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.8.16 Darwin/24.0.0

File hashes

Hashes for pysnmp-7.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 c0cbf4781772c0346c7d9e5059e4af6ae2d2d84b5fd61a8afea4815d90db2181
MD5 13ba3af0025407392f5a19c9586422c1
BLAKE2b-256 b662ab6d126346deb2b9008cfa87041daf7b7983f4f093251c7c8ab6499f15eb

See more details on using hashes here.

Supported by

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