Home

Awesome

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

Features, specific to SNMPv3 model include:

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:

Make sure you check out other sibling projects of PySNMP on the home page.

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.v1arch.asyncio import *

import asyncio


async def run():
    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]))

asyncio.run(run())

This is how to send SNMP TRAP:

from pysnmp.hlapi.v3arch.asyncio import *

import asyncio


async def run():
    snmpEngine = SnmpEngine()
    errorIndication, errorStatus, errorIndex, varBinds = await sendNotification(
        snmpEngine,
        CommunityData('public', mpModel=0),
        await UdpTransportTarget.create(('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()

asyncio.run(run())

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.

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.