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.v3arch.asyncio import *
    
    
    async def run():
        snmpEngine = SnmpEngine()
    
        iterator = get_cmd(
            snmpEngine,
            CommunityData("public", mpModel=0),
            await UdpTransportTarget.create(("demo.pysnmp.com", 161)),
            ContextData(),
            ObjectType(ObjectIdentity("SNMPv2-MIB", "sysDescr", 0)),
        )
    
        errorIndication, errorStatus, errorIndex, varBinds = await iterator
    
        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]))
    
        snmpEngine.close_dispatcher()
    
    
    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.v3arch.asyncio import *
    
    
    async def run():
        snmpEngine = SnmpEngine()
        errorIndication, errorStatus, errorIndex, varBinds = await send_notification(
            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"))
            .load_mibs("SNMPv2-MIB")
            .add_varbinds(
                ("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.close_dispatcher()
    
    
    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.

Next Steps

Now that you have successfully finished initial tests of PySNMP, you can move on to Common Operations with High-Level API to learn more API of this package, or play with more complex examples in the Samples section.

And whenever you want to refer to the SNMP protocol itself, you can visit the PySNMP Homepage.