Skip to content

Gufo SNMP Example: Single Item Get Request

Get is one of the basic SNMP operations allowing to query of the agent for one or more management keys. Let's consider the situation of getting the single key.

get.py
import asyncio
import sys

from gufo.snmp import SnmpSession


async def main(addr: str, community: str, oid: str) -> None:
    async with SnmpSession(addr=addr, community=community) as session:
        r = await session.get(oid)
        print(r)


asyncio.run(main(sys.argv[1], sys.argv[2], sys.argv[3]))

Let's see the details.

get.py
import asyncio
import sys

from gufo.snmp import SnmpSession


async def main(addr: str, community: str, oid: str) -> None:
    async with SnmpSession(addr=addr, community=community) as session:
        r = await session.get(oid)
        print(r)


asyncio.run(main(sys.argv[1], sys.argv[2], sys.argv[3]))
Gufo SNMP is an async library. In our case we should run the client from our synchronous script, so we need to import asyncio to use asyncio.run().

get.py
import asyncio
import sys

from gufo.snmp import SnmpSession


async def main(addr: str, community: str, oid: str) -> None:
    async with SnmpSession(addr=addr, community=community) as session:
        r = await session.get(oid)
        print(r)


asyncio.run(main(sys.argv[1], sys.argv[2], sys.argv[3]))
Import sys module to parse the CLI argument.

Warning

We use sys.argv only for demonstration purposes. Use argsparse or alternatives in real-world applications.

get.py
import asyncio
import sys

from gufo.snmp import SnmpSession


async def main(addr: str, community: str, oid: str) -> None:
    async with SnmpSession(addr=addr, community=community) as session:
        r = await session.get(oid)
        print(r)


asyncio.run(main(sys.argv[1], sys.argv[2], sys.argv[3]))

SnmpSession object holds all necessary API, so import it from gufo.snmp.

get.py
import asyncio
import sys

from gufo.snmp import SnmpSession


async def main(addr: str, community: str, oid: str) -> None:
    async with SnmpSession(addr=addr, community=community) as session:
        r = await session.get(oid)
        print(r)


asyncio.run(main(sys.argv[1], sys.argv[2], sys.argv[3]))

Asynchronous code must be executed in the asynchronous functions or coroutines. So we define our function as async. We expect the following arguments:

  • Address of the agent.
  • SNMP community to authorize.
  • OID to query.
get.py
import asyncio
import sys

from gufo.snmp import SnmpSession


async def main(addr: str, community: str, oid: str) -> None:
    async with SnmpSession(addr=addr, community=community) as session:
        r = await session.get(oid)
        print(r)


asyncio.run(main(sys.argv[1], sys.argv[2], sys.argv[3]))

First, we need to create SnmpSession object which wraps the client's session. The SnmpSession may be used as an instance directly or operated as async context manager with the async with clause. When used as a context manager, the client automatically closes all connections on the exit of context, so its lifetime is defined explicitly.

SnmpSession constructor offers lots of configuration variables for fine-tuning. Refer to the SnmpSession reference for further details. In our example, we set the agent's address and SNMP community to the given values.

get.py
import asyncio
import sys

from gufo.snmp import SnmpSession


async def main(addr: str, community: str, oid: str) -> None:
    async with SnmpSession(addr=addr, community=community) as session:
        r = await session.get(oid)
        print(r)


asyncio.run(main(sys.argv[1], sys.argv[2], sys.argv[3]))

We use SnmpSession.get() function to query OID. The function is asynchronous and must be awaited. See SnmpSession.get() reference for further details.

get.py
import asyncio
import sys

from gufo.snmp import SnmpSession


async def main(addr: str, community: str, oid: str) -> None:
    async with SnmpSession(addr=addr, community=community) as session:
        r = await session.get(oid)
        print(r)


asyncio.run(main(sys.argv[1], sys.argv[2], sys.argv[3]))

It is up to the application how to deal with the result. In our example we just print it.

get.py
import asyncio
import sys

from gufo.snmp import SnmpSession


async def main(addr: str, community: str, oid: str) -> None:
    async with SnmpSession(addr=addr, community=community) as session:
        r = await session.get(oid)
        print(r)


asyncio.run(main(sys.argv[1], sys.argv[2], sys.argv[3]))

Lets run our asynchronous main() function via asyncio.run and pass first command-line parameters as address, community, and OID.

Running

Let's check our script. Run example as:

$ python3 examples/async/get.py 127.0.0.1 public 1.3.6.1.2.1.1.6.0
Gufo SNMP Test