Dependency-free basic SNMP v1/v2/v3 client in Rust.
This is a fork of the original snmp crate which has been abandoned long time ago.
SNMP2 is a part of RoboPLC project.
New features added to the fork:
- SNMP v1 support (including v1 traps)
- SNMP v3 authentication (MD5, SHA1, SHA224, SHA256, SHA384, SHA512)
- SNMP v3 privacy (DES, AES128, AES192, AES256)
- MIBs support (requires
mibsfeature andlibnetsnmplibrary installed) - Async session (requires
tokiofeature) - Crate code has been refactored and cleaned up
- OIDs have been migrated to asn1
- Improved PDU API, added trap handling examples
Supports:
- GET
- GETNEXT
- GETBULK
- SET
- Basic SNMP v1/v2 types
- Synchronous/Asynchronous requests
- UDP transport
- MIBs (with
mibsfeature, requireslibnetsnmp) - SNMP v3 (requires
v3feature)
Examples
GET NEXT
use Duration;
use ;
let sys_descr_oid = from.unwrap;
let agent_addr = "198.51.100.123:161";
let community = b"f00b4r";
let timeout = from_secs;
let mut sess = new_v2c.unwrap;
let mut response = sess.getnext.unwrap;
if let Some = response.varbinds.next
GET BULK
use Duration;
use ;
let system_oid = from.unwrap;
let agent_addr = "[2001:db8:f00:b413::abc]:161";
let community = b"f00b4r";
let timeout = from_secs;
let non_repeaters = 0;
let max_repetitions = 7; // number of items in "system" OID
let mut sess = new_v2c.unwrap;
let response = sess.getbulk.unwrap;
for in response.varbinds
SET
use Duration;
use ;
let syscontact_oid = from.unwrap;
let contact = OctetString;
let agent_addr = "[2001:db8:f00:b413::abc]:161";
let community = b"f00b4r";
let timeout = from_secs;
let mut sess = new_v2c.unwrap;
let response = sess.set.unwrap;
assert_eq!;
for in response.varbinds
TRAPS
use UdpSocket;
use Pdu;
let socket = bind.expect;
loop
PDU to Bytes Conversion
Convert PDU structures to byte arrays for UDP communication:
use ;
use UdpSocket;
// Parse a received PDU
let received_pdu = from_bytes.unwrap;
// Convert PDU back to bytes for forwarding or storage
let bytes = received_pdu.to_bytes.unwrap;
// Send via UDP socket
let socket = bind.unwrap;
socket.send_to.unwrap;
With SNMPv3 (requires v3 feature)
When using SNMPv3, you need to provide the security context to convert the PDU to bytes:
Async session
use Duration;
use ;
async
Working with MIBs
Prepare the system
apt-get install libsnmp-dev snmp-mibs-downloader
use ;
init
.unwrap;
let snmp_oid = from.unwrap;
let name = snmp_oid.mib_name.unwrap;
assert_eq!;
let snmp_oid2 = from_mib_name.unwrap;
assert_eq!;
SNMPv3
-
Requires
v3crate feature. -
All cryptographic algorithms are provided by openssl.
-
For authentication, supports: MD5 (RFC3414), SHA1 (RFC3414) and non-standard SHA224, SHA256, SHA384, SHA512.
-
For privacy, supports: DES (RFC3414), AES128-CFB (RFC3826) and non-standard AES192-CFB, AES256-CFB. Additional/different AES modes are not supported and may require patching the crate.
Note: DES legacy encryption may be disabled in openssl by default or even not supported at all. Refer to the library documentation how to enable it.
Example
Authentication: SHA1, encryption: AES128-CFB
use ;
use Duration;
// the security parameters also keep authoritative engine ID and boot/time
// counters. these can be either set or resolved/updated automatically.
let security = new
.with_auth_protocol
.with_auth;
let mut sess =
new_v3.unwrap;
// In case if engine_id is not provided in security parameters, it is necessary
// to call init() method to send a blank unauthenticated request to the target
// to get the engine_id.
sess.init.unwrap;
loop
Building
In case of problems (e.g. with cross-rs),
add openssl with vendored feature:
cargo add openssl --features vendored
FIPS-140 support
The crate uses openssl cryptography only and becomes FIPS-140 compliant as soon
as FIPS mode is activated in openssl. Refer to the
openssl crate crate and
openssl library documentation for more details.
MSRV
1.83.0
Copyright
Copyright 2016-2018 Hroi Sigurdsson
Copyright 2024 Serhij Symonenko, Bohemia Automation Limited
Licensed under the Apache License, Version 2.0 or the MIT license, at your option. This file may not be copied, modified, or distributed except according to those terms.