Add printing, commands work

This commit is contained in:
2026-07-03 18:45:33 +03:00
parent 16615d32c1
commit 6537c41972
8 changed files with 402 additions and 63 deletions

View File

@@ -29,7 +29,7 @@ impl MtCommander {
pub async fn run_command_on_hosts(
hosts: &[ConfigHost],
command: &str,
) -> anyhow::Result<HashMap<ConfigHost, String>> {
) -> anyhow::Result<HashMap<ConfigHost, Vec<HashMap<String, Option<String>>>>> {
let mut res_map = HashMap::new();
for host in hosts {
let res = Self::run_command_on_host(host, command).await?;
@@ -37,12 +37,15 @@ impl MtCommander {
}
Ok(res_map)
}
pub async fn run_command_on_host(host: &ConfigHost, command: &str) -> anyhow::Result<String> {
pub async fn run_command_on_host(
host: &ConfigHost,
command: &str,
) -> anyhow::Result<Vec<HashMap<String, Option<String>>>> {
let mtc = MTC.lock().map_err(|e| anyhow::anyhow!(e.to_string()))?;
mtc.run_command(host, &command).await
}
pub async fn run_command_on_hosts_and_parse_re(
/*pub async fn run_command_on_hosts_and_parse_re(
hosts: &[ConfigHost],
command: &str,
parse_re: &Regex,
@@ -53,9 +56,9 @@ impl MtCommander {
res_map.insert(host.clone(), res);
}
Ok(res_map)
}
}*/
pub async fn run_command_on_host_and_parse_re(
/*pub async fn run_command_on_host_and_parse_re(
host: &ConfigHost,
command: &str,
parse_re: &Regex,
@@ -67,7 +70,7 @@ impl MtCommander {
.map(|s| s.as_str().to_string())
.collect();
Ok(out)
}
}*/
}
impl MtCommanderInternal {
@@ -95,9 +98,13 @@ impl MtCommanderInternal {
tokio::spawn(watcher());
Ok(())
}
async fn run_command(&self, host: &ConfigHost, cmd: &str) -> anyhow::Result<String> {
async fn run_command(
&self,
host: &ConfigHost,
cmd: &str,
) -> anyhow::Result<Vec<HashMap<String, Option<String>>>> {
let dev = MikrotikDevice::connect(
format!("{}", &host.ip),
format!("{}:{}", &host.ip, host.port(&self.cfg())),
&host.username(self.cfg()),
Some(&host.password(self.cfg())),
)
@@ -107,14 +114,17 @@ impl MtCommanderInternal {
.send_command(CommandBuilder::new().command(cmd).build())
.await?;
let mut res = String::new();
let mut res = Vec::new();
while let Some(event) = rx.recv().await {
match event {
Event::Reply { response, .. } => {
log::debug!("({}) {:?}", &host.ip, response.attributes);
res.push(response.attributes);
}
Event::Done { .. } => {
break;
}
Event::Done { .. } => break,
Event::Trap { response, .. } => {
log::error!("({}) {:?}", &host.ip, response.message);
}