Client

Struct Client 

Source
pub struct Client<T: Transport = UdpHandle> { /* private fields */ }
Expand description

SNMP client.

Generic over transport type, with UdpHandle as default.

Implementations§

Source§

impl Client<UdpHandle>

Source

pub fn builder( target: impl Into<String>, auth: impl Into<Auth>, ) -> ClientBuilder

Create a new SNMP client builder.

This is the single entry point for client construction, supporting all SNMP versions (v1, v2c, v3) through the Auth enum.

§Example
use async_snmp::{Auth, Client, Retry};
use std::time::Duration;

// Simple v2c client with default settings
let client = Client::builder("192.168.1.1:161", Auth::v2c("public"))
    .connect().await?;

// v3 client with authentication
let client = Client::builder("192.168.1.1:161",
    Auth::usm("admin").auth(async_snmp::AuthProtocol::Sha256, "password"))
    .timeout(Duration::from_secs(10))
    .retry(Retry::fixed(5, Duration::ZERO))
    .connect().await?;
Source§

impl<T: Transport> Client<T>

Source

pub fn new(transport: T, config: ClientConfig) -> Self

Create a new client with the given transport and config.

For most use cases, prefer Client::builder() which provides a more ergonomic API. Use this constructor when you need fine-grained control over transport configuration (e.g., TCP connection timeout, keepalive settings) or when using a custom Transport implementation.

Source

pub fn with_engine_cache( transport: T, config: ClientConfig, engine_cache: Arc<EngineCache>, ) -> Self

Create a new V3 client with a shared engine cache.

Source

pub fn peer_addr(&self) -> SocketAddr

Get the peer (target) address.

Returns the remote address that this client sends requests to. Named to match std::net::TcpStream::peer_addr().

Source

pub async fn get(&self, oid: &Oid) -> Result<VarBind>

GET a single OID.

Source

pub async fn get_many(&self, oids: &[Oid]) -> Result<Vec<VarBind>>

GET multiple OIDs.

If the OID list exceeds max_oids_per_request, the request is automatically split into multiple batches. Results are returned in the same order as the input OIDs.

§Example
let results = client.get_many(&[
    oid!(1, 3, 6, 1, 2, 1, 1, 1, 0),  // sysDescr
    oid!(1, 3, 6, 1, 2, 1, 1, 3, 0),  // sysUpTime
    oid!(1, 3, 6, 1, 2, 1, 1, 5, 0),  // sysName
]).await?;
Source

pub async fn get_next(&self, oid: &Oid) -> Result<VarBind>

GETNEXT for a single OID.

Source

pub async fn get_next_many(&self, oids: &[Oid]) -> Result<Vec<VarBind>>

GETNEXT for multiple OIDs.

If the OID list exceeds max_oids_per_request, the request is automatically split into multiple batches. Results are returned in the same order as the input OIDs.

§Example
let results = client.get_next_many(&[
    oid!(1, 3, 6, 1, 2, 1, 2, 2, 1, 2),  // ifDescr
    oid!(1, 3, 6, 1, 2, 1, 2, 2, 1, 3),  // ifType
]).await?;
Source

pub async fn set(&self, oid: &Oid, value: Value) -> Result<VarBind>

SET a single OID.

Source

pub async fn set_many(&self, varbinds: &[(Oid, Value)]) -> Result<Vec<VarBind>>

SET multiple OIDs.

If the varbind list exceeds max_oids_per_request, the request is automatically split into multiple batches. Results are returned in the same order as the input varbinds.

§Example
let results = client.set_many(&[
    (oid!(1, 3, 6, 1, 2, 1, 1, 5, 0), Value::from("new-hostname")),
    (oid!(1, 3, 6, 1, 2, 1, 1, 6, 0), Value::from("new-location")),
]).await?;
Source

pub async fn get_bulk( &self, oids: &[Oid], non_repeaters: i32, max_repetitions: i32, ) -> Result<Vec<VarBind>>

GETBULK request (SNMPv2c/v3 only).

Efficiently retrieves multiple variable bindings in a single request. GETBULK splits the requested OIDs into two groups:

  • Non-repeaters (first N OIDs): Each gets a single GETNEXT, returning one value per OID. Use for scalar values like sysUpTime.0.
  • Repeaters (remaining OIDs): Each gets up to max_repetitions GETNEXTs, returning multiple values per OID. Use for walking table columns.
§Arguments
  • oids - OIDs to retrieve
  • non_repeaters - How many OIDs (from the start) are non-repeating
  • max_repetitions - Maximum rows to return for each repeating OID
§Example
// Get sysUpTime (non-repeater) plus 10 interface descriptions (repeater)
let results = client.get_bulk(
    &[oid!(1, 3, 6, 1, 2, 1, 1, 3, 0), oid!(1, 3, 6, 1, 2, 1, 2, 2, 1, 2)],
    1,  // first OID is non-repeating
    10, // get up to 10 values for the second OID
).await?;
// Results: [sysUpTime value, ifDescr.1, ifDescr.2, ..., ifDescr.10]
Source

pub fn walk(&self, oid: Oid) -> Result<WalkStream<T>>
where T: 'static,

Walk an OID subtree.

Auto-selects the optimal walk method based on SNMP version and WalkMode:

  • WalkMode::Auto (default): Uses GETNEXT for V1, GETBULK for V2c/V3
  • WalkMode::GetNext: Always uses GETNEXT
  • WalkMode::GetBulk: Always uses GETBULK (fails on V1)

Returns an async stream that yields each variable binding in the subtree. The walk terminates when an OID outside the subtree is encountered or when EndOfMibView is returned.

Uses the client’s configured oid_ordering, max_walk_results, and max_repetitions (for GETBULK) settings.

§Example
// Auto-selects GETBULK for V2c/V3, GETNEXT for V1
let results = client.walk(oid!(1, 3, 6, 1, 2, 1, 1))?.collect().await?;
Source

pub fn walk_getnext(&self, oid: Oid) -> Walk<T>
where T: 'static,

Walk an OID subtree using GETNEXT.

This method always uses GETNEXT regardless of the client’s WalkMode configuration. For auto-selection based on version and mode, use walk() instead.

Returns an async stream that yields each variable binding in the subtree. The walk terminates when an OID outside the subtree is encountered or when EndOfMibView is returned.

Uses the client’s configured oid_ordering and max_walk_results settings.

§Example
// Force GETNEXT even for V2c/V3 clients
let results = client.walk_getnext(oid!(1, 3, 6, 1, 2, 1, 1)).collect().await?;
Source

pub fn bulk_walk(&self, oid: Oid, max_repetitions: i32) -> BulkWalk<T>
where T: 'static,

Walk an OID subtree using GETBULK (more efficient than GETNEXT).

Returns an async stream that yields each variable binding in the subtree. Uses GETBULK internally with non_repeaters=0, fetching max_repetitions values per request for efficient table traversal.

Uses the client’s configured oid_ordering and max_walk_results settings.

§Arguments
  • oid - The base OID of the subtree to walk
  • max_repetitions - How many OIDs to fetch per request
§Example
// Walk the interfaces table efficiently
let walk = client.bulk_walk(oid!(1, 3, 6, 1, 2, 1, 2, 2), 25);
// Process with futures StreamExt
Source

pub fn bulk_walk_default(&self, oid: Oid) -> BulkWalk<T>
where T: 'static,

Walk an OID subtree using the client’s configured max_repetitions.

This is a convenience method that uses the client’s max_repetitions setting (default: 25) instead of requiring it as a parameter.

§Example
// Walk using configured max_repetitions
let walk = client.bulk_walk_default(oid!(1, 3, 6, 1, 2, 1, 2, 2));
// Process with futures StreamExt

Trait Implementations§

Source§

impl<T: Clone + Transport> Clone for Client<T>

Source§

fn clone(&self) -> Client<T>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

Auto Trait Implementations§

§

impl<T> Freeze for Client<T>

§

impl<T> RefUnwindSafe for Client<T>
where T: RefUnwindSafe,

§

impl<T> Send for Client<T>

§

impl<T> Sync for Client<T>

§

impl<T> Unpin for Client<T>

§

impl<T> UnwindSafe for Client<T>
where T: RefUnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more