Skip to content

SNMPv3 Engine ID Discovery

SNMP v3 introduces the concept of the Engine ID, a unique identifier for each SNMP agent in the network.

Gufo SNMP automatically performs Engine ID discovery as needed. However, you can retrieve the actual value for various purposes, such as inventory management or performance optimization, and use it to skip the discovery step later.

engine-id-discovery.py
import asyncio
import sys

from gufo.snmp import SnmpSession, User


async def main(addr: str, user_name: str) -> None:
    async with SnmpSession(addr=addr, user=User(user_name)) as session:
        engine_id = session.get_engine_id()
        print(engine_id.hex())


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

Let's see the details.

engine-id-discovery.py
import asyncio
import sys

from gufo.snmp import SnmpSession, User


async def main(addr: str, user_name: str) -> None:
    async with SnmpSession(addr=addr, user=User(user_name)) as session:
        engine_id = session.get_engine_id()
        print(engine_id.hex())


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

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().

engine-id-discovery.py
import asyncio
import sys

from gufo.snmp import SnmpSession, User


async def main(addr: str, user_name: str) -> None:
    async with SnmpSession(addr=addr, user=User(user_name)) as session:
        engine_id = session.get_engine_id()
        print(engine_id.hex())


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

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.

engine-id-discovery.py
import asyncio
import sys

from gufo.snmp import SnmpSession, User


async def main(addr: str, user_name: str) -> None:
    async with SnmpSession(addr=addr, user=User(user_name)) as session:
        engine_id = session.get_engine_id()
        print(engine_id.hex())


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

SnmpSession object holds all necessary API, so import it from gufo.snmp. For SNMPv3 we also need an User class.

engine-id-discovery.py
import asyncio
import sys

from gufo.snmp import SnmpSession, User


async def main(addr: str, user_name: str) -> None:
    async with SnmpSession(addr=addr, user=User(user_name)) as session:
        engine_id = session.get_engine_id()
        print(engine_id.hex())


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

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.
  • Valid user name.
engine-id-discovery.py
import asyncio
import sys

from gufo.snmp import SnmpSession, User


async def main(addr: str, user_name: str) -> None:
    async with SnmpSession(addr=addr, user=User(user_name)) as session:
        engine_id = session.get_engine_id()
        print(engine_id.hex())


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

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 create SNMPv3 user with default settings.

Note

To perform Engine ID discovery, the only mandatory parameter is the username. Authentication and privacy settings can be left at their default values.

engine-id-discovery.py
import asyncio
import sys

from gufo.snmp import SnmpSession, User


async def main(addr: str, user_name: str) -> None:
    async with SnmpSession(addr=addr, user=User(user_name)) as session:
        engine_id = session.get_engine_id()
        print(engine_id.hex())


asyncio.run(main(sys.argv[1], sys.argv[2]))
SnmpSession.get_engine_id() returns discovered Engine Id as bytes.

engine-id-discovery.py
import asyncio
import sys

from gufo.snmp import SnmpSession, User


async def main(addr: str, user_name: str) -> None:
    async with SnmpSession(addr=addr, user=User(user_name)) as session:
        engine_id = session.get_engine_id()
        print(engine_id.hex())


asyncio.run(main(sys.argv[1], sys.argv[2]))
Now, we print the collected Engine ID. Since it is of bytes type, we convert the output to hexadecimal form, which is commonly used in network equipment configuration.

engine-id-discovery.py
import asyncio
import sys

from gufo.snmp import SnmpSession, User


async def main(addr: str, user_name: str) -> None:
    async with SnmpSession(addr=addr, user=User(user_name)) as session:
        engine_id = session.get_engine_id()
        print(engine_id.hex())


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

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/engine-id-discovery.py 127.0.0.1 user1
8000b85c03ec02732921c0