In the previous example, we demonstrated how to request a single item using SNMP v2c
in Gufo SNMP. Now, we'll show you how to achieve the same with SNMP v3,
which offers a similar API with additional authentication options.
Despite SNMP v3's increased complexity, Gufo SNMP effectively handles all the intricacies,
making SNMP v3 operations as straightforward as v2c.
Let's modify our previous example to utilize SNMP v3.
SNMPv3 offers various authentication options, so we define mappings
between human-readable names and Gufo SNMP key wrappers to use later
in the get_user function.
While SNMP v2c relies on a simple community string for authentication,
SNMPv3 introduces the more intricate User-Based Security Model (USM).
In this model, a user typically consists of a username,
along with optional authentication and privacy options.
Gufo SNMP encapsulates these details within the User class.
To facilitate the configuration process, we define the get_user function.
This function processes command-line arguments and returns an instance of the User class.
If an authentication option is set, it must have the format <alg>:<key>,
where:
<alg> - authentication algorithm, which must be one of AUTH_ALG keys.
<key> - an authentication key.
Note
SNMPv3 introduces three forms of keys:
* Password
* Master key
* Localized key
Choose the form deliberately. Gufo SNMP supports all three forms,
which can be selected through optional parameters of the *Key classes.
This example uses the default form: a password.
Then we find a proper key class via AUTH_ALG mapping
and pass a key.
Note
All keys in Gufo SNMP are passed as bytes, so
we use .encode() method to convert from str.
Privacy settings are handled just like authentication settings. We expect
them in the fifth command-line parameter,
and then use PRIV_ALG mapping to get a proper algorithm.
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 User
instance.
Let's run our main() function.
Pass the address as the first command-line parameter, construct the user with
get_user(), and pass the OID as the third parameter.