use async_snmp::cli::args::{CommonArgs, OutputArgs, V3Args, ValueType};
#[cfg(feature = "mib")]
use async_snmp::cli::output::VarBindFormatter;
use async_snmp::cli::output::{
OperationType, OutputContext, RequestInfo, build_security_info, write_error,
write_verbose_request, write_verbose_response,
};
use async_snmp::{Client, Oid, Value, VarBind};
use clap::Parser;
use std::process::ExitCode;
use std::time::Instant;
#[derive(Debug, Parser)]
#[command(name = "asnmp-set", version, about, verbatim_doc_comment)]
struct Args {
#[command(flatten)]
common: CommonArgs,
#[command(flatten)]
v3: V3Args,
#[command(flatten)]
output: OutputArgs,
#[cfg(feature = "mib")]
#[command(flatten)]
mib: async_snmp::cli::mib_cli::MibArgs,
#[arg(required = true, value_name = "OID TYPE VALUE", num_args = 3..)]
varbinds: Vec<String>,
}
struct SetVarbind {
oid: Oid,
value: Value,
}
fn parse_varbinds(
args: &[String],
resolve_oid: impl Fn(&str) -> Result<Oid, String>,
) -> Result<Vec<SetVarbind>, String> {
if !args.len().is_multiple_of(3) {
return Err("arguments must be OID TYPE VALUE triplets".into());
}
let mut varbinds = Vec::new();
for chunk in args.chunks(3) {
let oid_str = &chunk[0];
let type_str = &chunk[1];
let value_str = &chunk[2];
let oid = resolve_oid(oid_str)?;
let value_type: ValueType = type_str.parse().map_err(|_| {
format!(
"invalid type specifier '{}'; use i, u, s, x, o, a, t, c, or C",
type_str
)
})?;
let value = value_type.parse_value(value_str)?;
varbinds.push(SetVarbind { oid, value });
}
Ok(varbinds)
}
#[cfg_attr(feature = "rt-multi-thread", tokio::main)]
#[cfg_attr(
not(feature = "rt-multi-thread"),
tokio::main(flavor = "current_thread")
)]
async fn main() -> ExitCode {
let args = Args::parse();
args.output.init_tracing();
if let Err(e) = args.v3.validate() {
eprintln!("Error: {}", e);
return ExitCode::FAILURE;
}
let target = &args.common.target;
#[cfg(feature = "mib")]
let mib = match args.mib.load().await {
Ok(mib) => mib,
Err(e) => {
eprintln!("Error: {}", e);
return ExitCode::FAILURE;
}
};
let varbinds = match parse_varbinds(&args.varbinds, |s| {
#[cfg(feature = "mib")]
{
async_snmp::cli::mib_cli::resolve_oid_arg(mib.as_ref(), s)
}
#[cfg(not(feature = "mib"))]
{
async_snmp::cli::hints::parse_oid(s)
}
}) {
Ok(vb) => vb,
Err(e) => {
eprintln!("Error: {}", e);
return ExitCode::FAILURE;
}
};
let version = args.common.effective_version(&args.v3);
if args.output.verbose {
let oids: Vec<_> = varbinds.iter().map(|vb| vb.oid.clone()).collect();
let request_info = RequestInfo {
target: target.as_str(),
version: version.into(),
security: build_security_info(&args.v3, &args.common),
operation: OperationType::Set,
oids,
};
write_verbose_request(&request_info);
}
let start = Instant::now();
let result = run_set(target.as_str(), &args, varbinds).await;
let elapsed = start.elapsed();
match result {
Ok(result_varbinds) => {
if args.output.verbose {
write_verbose_response(&result_varbinds, elapsed, !args.output.no_hints);
}
let mut output_ctx = OutputContext::from_args(&args.output);
#[cfg(feature = "mib")]
if let Some(m) = &mib {
output_ctx.formatter = Some(m as &dyn VarBindFormatter);
}
if let Err(e) = output_ctx.write_results(
target.as_str(),
version.into(),
&result_varbinds,
args.output.elapsed(elapsed),
None,
) {
eprintln!("Error writing output: {}", e);
return ExitCode::FAILURE;
}
ExitCode::SUCCESS
}
Err(e) => {
write_error(&e);
ExitCode::FAILURE
}
}
}
async fn run_set(
target: &str,
args: &Args,
varbinds: Vec<SetVarbind>,
) -> async_snmp::Result<Vec<VarBind>> {
let auth = args
.v3
.auth(&args.common)
.map_err(|e| async_snmp::Error::Config(e.to_string().into()))?;
let client = Client::builder(target, auth)
.timeout(args.common.timeout_duration())
.retry(args.common.retry_config())
.connect()
.await?;
let pairs: Vec<(Oid, Value)> = varbinds.into_iter().map(|vb| (vb.oid, vb.value)).collect();
if pairs.len() == 1 {
let (oid, value) = pairs.into_iter().next().unwrap();
client.set(&oid, value).await.map(|vb| vec![vb])
} else {
client.set_many(&pairs).await
}
}