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 (enable
crypto-opensslorcrypto-rustfeature)
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 (enable crypto-openssl or crypto-rust)
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 enabling one of the features:
crypto-opensslorcrypto-rust.crypto-openssl: uses OpenSSL for hashing/HMAC and symmetric encryption.crypto-rust: uses pure Rust crypto crates for hashing/HMAC and encryption.
-
Cryptographic algorithms are provided by the selected backend:
crypto-openssl: opensslcrypto-rust: pure Rust crates Rust Crypto: (md-5,sha1,sha2,hmac,aes,des, etc.)
-
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: For crypto-openssl, DES legacy encryption may be disabled in OpenSSL by default
or not supported at all. Refer to the library documentation how to enable it.
Note: if both crypto-openssl and crypto-rust features are enabled,
crypto-openssl will have higher priority and will be used as the crypto
backend.
Feature selection examples
Pure Rust backend:
cargo add snmp2 --features crypto-rust
OpenSSL backend (Windows-friendly vendored build):
cargo add snmp2 --features "crypto-openssl,openssl/vendored"
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 (crypto-openssl)
When using the crypto-openssl backend, in case of problems (e.g. with
cross-rs), add openssl with vendored feature:
cargo add openssl --features vendored
FIPS-140 support (crypto-openssl)
When using the crypto-openssl backend, the crate 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.
The crypto-rust backend does not rely on OpenSSL and is not FIPS-certified.
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.