This commit is contained in:
2026-07-07 17:19:25 +03:00
parent 14c0009030
commit fbef11dfe7
4 changed files with 74 additions and 10 deletions

View File

@@ -14,6 +14,7 @@ use std::{
sync::{Arc, Mutex},
};
use anyhow::bail;
use mikrotik_rs::{Command, CommandBuilder, Event, MikrotikDevice};
use regex::Regex;
@@ -112,23 +113,36 @@ impl MtCommanderInternal {
host: &ConfigHost,
cmd: &str,
) -> anyhow::Result<Vec<HashMap<String, Option<String>>>> {
let dev = MikrotikDevice::connect(
format!("{}:{}", &host.ip, host.port(&self.cfg())),
let addr = format!("{}:{}", &host.ip, host.port(&self.cfg()));
let dev_res = MikrotikDevice::connect(
&addr,
&host.username(self.cfg()),
Some(&host.password(self.cfg())),
)
.await?;
.await;
let dev = match dev_res {
Ok(dev) => {
log::info!("({addr}) Connected successfully!");
dev
},
Err(e) => {
log::error!("Failed to connect to {addr}: {e}");
bail!(e);
}
};
let mut rx = dev
.send_command(CommandBuilder::new().command(cmd).build())
.await?;
let mut res = Vec::new();
let mut size = 0;
while let Some(event) = rx.recv().await {
match event {
Event::Reply { response, .. } => {
Event::Reply { response, tag: _ } => {
log::debug!("({}) {:?}", &host.ip, response.attributes);
size += response.attributes.values().flatten().map(|v| v.len()).sum::<usize>();
res.push(response.attributes);
}
Event::Done { .. } => {
@@ -140,6 +154,7 @@ impl MtCommanderInternal {
other => println!("{other:?}"),
}
}
log::info!("({addr}) Command finished, read {size} bytes");
Ok(res)
}
}