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 privacy option is set, we consider it has format of <alg>:<key>,
where:
<alg> - authentication algorithm, which must be one of AUTH_ALG keys.
<key> - an authentication key.
Note
SNMPv3 intoduces 3 form of keys:
* Password
* Master key
* Localized key
Such a variety often introduces a mess and you need
to have a clear meaning of which of key you really passing.
Gufo SNMP supports all three forms of keys which may
be specified as additional optional parameters for
*Key classes. We use default settings (password)
for this example.
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.
The privacy settings are handled just like as the authentication
ones. We expect privacy settings in 6-th command-line parameter,
and then use PRIV_ALG mapping to get a proper algorithm.
Just like a privacy settings, None value means no encryption.
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.