Skip to content

Gufo SNMP Example: GetBulk Request

We have mastered the iteration of the MIB view in our getnext example. SNMP v2 also offers more effective approach - the GetBulk request. Gufo SNMP hides all implementation difference and the interface to the GetBulk requests is almost identical to the GetNext one.

getbulk.py
import sys

from gufo.snmp.sync_client import SnmpSession


def main(addr: str, community: str, oid: str) -> None:
    with SnmpSession(addr=addr, community=community) as session:
        for k, v in session.getbulk(oid):
            print(f"{k}: {v}")


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

Let's see the details.

getbulk.py
import sys

from gufo.snmp.sync_client import SnmpSession


def main(addr: str, community: str, oid: str) -> None:
    with SnmpSession(addr=addr, community=community) as session:
        for k, v in session.getbulk(oid):
            print(f"{k}: {v}")


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.

getbulk.py
import sys

from gufo.snmp.sync_client import SnmpSession


def main(addr: str, community: str, oid: str) -> None:
    with SnmpSession(addr=addr, community=community) as session:
        for k, v in session.getbulk(oid):
            print(f"{k}: {v}")


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

SnmpSession object holds all necessary API. We're using a synchronous version from gufo.snmp.sync_client.

getbulk.py
import sys

from gufo.snmp.sync_client import SnmpSession


def main(addr: str, community: str, oid: str) -> None:
    with SnmpSession(addr=addr, community=community) as session:
        for k, v in session.getbulk(oid):
            print(f"{k}: {v}")


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

We define our main function and expect the following arguments:

  • Address of the agent.
  • SNMP community to authorize.
  • Base OID to query.
getbulk.py
import sys

from gufo.snmp.sync_client import SnmpSession


def main(addr: str, community: str, oid: str) -> None:
    with SnmpSession(addr=addr, community=community) as session:
        for k, v in session.getbulk(oid):
            print(f"{k}: {v}")


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 context manager using the 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.

getbulk.py
import sys

from gufo.snmp.sync_client import SnmpSession


def main(addr: str, community: str, oid: str) -> None:
    with SnmpSession(addr=addr, community=community) as session:
        for k, v in session.getbulk(oid):
            print(f"{k}: {v}")


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

We use SnmpSession.getbulk() function to iterate within base OID. The function is an iterator yielding pairs of (OID, value), so we use for construction to iterate over the values. See SnmpSession.getbulk() reference for further details.

getbulk.py
import sys

from gufo.snmp.sync_client import SnmpSession


def main(addr: str, community: str, oid: str) -> None:
    with SnmpSession(addr=addr, community=community) as session:
        for k, v in session.getbulk(oid):
            print(f"{k}: {v}")


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.

getbulk.py
import sys

from gufo.snmp.sync_client import SnmpSession


def main(addr: str, community: str, oid: str) -> None:
    with SnmpSession(addr=addr, community=community) as session:
        for k, v in session.getbulk(oid):
            print(f"{k}: {v}")


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

Lets run our main() function and pass first command-line parameters as address, community, and oid.

Running

Let's check our script. Run example as:

$ python3 examples/sync/getbulk.py 127.0.0.1 public 1.3.6.1.2.1.1
1.3.6.1.2.1.1.1.0: b'Linux d280d3a0a307 5.15.49-linuxkit #1 SMP Tue Sep 13 07:51:46 UTC 2022 x86_64'
1.3.6.1.2.1.1.2.0: 1.3.6.1.4.1.8072.3.2.10
1.3.6.1.2.1.1.3.0: 36567296
1.3.6.1.2.1.1.4.0: b'test <test@example.com>'
1.3.6.1.2.1.1.5.0: b'd280d3a0a307'
1.3.6.1.2.1.1.6.0: b'Gufo SNMP Test'
1.3.6.1.2.1.1.7.0: 72
...