Quick Start#

Once you decide to test out PySNMP library on your Linux/Windows/macOS system, you should start to prepare a test field folder and configure the Python environment.

Set Up Test Field Folder#

First, it is recommended that you use pyenv to manage different Python versions on this machine. If you are using Windows, you can use pyenv-win.

Next, we assume you are now on macOS/Linux, and the following commands initialize a folder for us,

$ cd ~
$ mkdir test-field
$ cd test-field
$ pyenv local 3.12
$ pip install pipenv
$ pipenv install pysnmp
$ pipenv run pip list

Here we created a virtual environment using pipenv for this folder, and installed pysnmp so that you can move on with the following sections.

The final command should print out the dependencies and you should be able to see pysnmp version 6.0+ there.

Note

If you haven’t installed Python 3.12 with pyenv, you should execute pyenv install 3.12.

To delete the virtual environment for this folder, you can use

$ pipenv --rm

It is common that you use another virtual environment tool, such as venv, poetry, or conda. Just make sure you use the equivalent commands to set up the virtual environment for testing.

It is highly recommended that you use a Python virtual environment, as it makes dependency management and troubleshooting much easier.

Fetch SNMP Variable#

Next, let’s write some test script and play with PySNMP manager side operations.

  1. Create a Python script in the test field folder, such as v1-get.py.

  2. Cut and paste the following contents below into this file,

    import asyncio
    from pysnmp.hlapi.asyncio.slim import Slim
    from pysnmp.smi.rfc1902 import ObjectIdentity, ObjectType
    
    
    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())
    

    Download script.

  3. Execute this script. If everything works as it should you will get the following on your console:

    $ pipenv run python v1-get.py
    ...
    SNMPv2-MIB::sysDescr."0" = SunOS zeus.pysnmp.com 4.1.3_U1 1 sun4m
    >>>
    

Here you can see SNMP v1 GET operation can be easily done with the Slim class. Other operations in SNMP v1 and v2c can be done in similar manner. To execute SNMP v3 operations, however, requires more complex code.

The test agent we use is hosted at demo.pysnmp.com.

Send SNMP TRAP#

Similarly we can perform agent side operations with PySNMP.

  1. Create a script file default-v1-trap.py.

  2. Cut and paste the following contents below into this file,

    import asyncio
    from pysnmp.hlapi.asyncio import *
    
    
    async def run():
        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()
    
    
    asyncio.run(run())
    

    Download script.

  3. Execute this script.

    $ pipenv run python default-v1-trap.py
    

Because this sends out an SNMP v1 TRAP message, we know that no response will be received.

The notification receiver the receives this message is hosted at demo.pysnmp.com.