lisuerhg
This commit is contained in:
@@ -0,0 +1,126 @@
|
||||
use core::panic;
|
||||
use std::{
|
||||
collections::HashMap,
|
||||
hash::Hash,
|
||||
sync::{Arc, Mutex},
|
||||
};
|
||||
|
||||
use mikrotik_rs::{Command, CommandBuilder, Event, MikrotikDevice};
|
||||
use regex::Regex;
|
||||
|
||||
use crate::config::{Config, ConfigHost};
|
||||
|
||||
lazy_static::lazy_static! {
|
||||
static ref MTC: Arc<Mutex<MtCommanderInternal>> = MtCommanderInternal::new_mutex();
|
||||
}
|
||||
|
||||
pub struct MtCommander;
|
||||
struct MtCommanderInternal {
|
||||
cfg: Option<Config>,
|
||||
}
|
||||
|
||||
impl MtCommander {
|
||||
pub fn init(cfg: Config) -> anyhow::Result<()> {
|
||||
let mut mtc = MTC.lock().map_err(|e| anyhow::anyhow!(e.to_string()))?;
|
||||
mtc.cfg = Some(cfg);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn run_command_on_hosts(
|
||||
hosts: &[ConfigHost],
|
||||
command: &str,
|
||||
) -> anyhow::Result<HashMap<ConfigHost, String>> {
|
||||
let mut res_map = HashMap::new();
|
||||
for host in hosts {
|
||||
let res = Self::run_command_on_host(host, command).await?;
|
||||
res_map.insert(host.clone(), res);
|
||||
}
|
||||
Ok(res_map)
|
||||
}
|
||||
pub async fn run_command_on_host(host: &ConfigHost, command: &str) -> anyhow::Result<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(
|
||||
hosts: &[ConfigHost],
|
||||
command: &str,
|
||||
parse_re: &Regex,
|
||||
) -> anyhow::Result<HashMap<ConfigHost, Vec<String>>> {
|
||||
let mut res_map = HashMap::new();
|
||||
for host in hosts {
|
||||
let res = Self::run_command_on_host_and_parse_re(host, command, parse_re).await?;
|
||||
res_map.insert(host.clone(), res);
|
||||
}
|
||||
Ok(res_map)
|
||||
}
|
||||
|
||||
pub async fn run_command_on_host_and_parse_re(
|
||||
host: &ConfigHost,
|
||||
command: &str,
|
||||
parse_re: &Regex,
|
||||
) -> anyhow::Result<Vec<String>> {
|
||||
let mtc = MTC.lock().map_err(|e| anyhow::anyhow!(e.to_string()))?;
|
||||
let res = mtc.run_command(host, &command).await?;
|
||||
let out = parse_re
|
||||
.find_iter(&res)
|
||||
.map(|s| s.as_str().to_string())
|
||||
.collect();
|
||||
Ok(out)
|
||||
}
|
||||
}
|
||||
|
||||
impl MtCommanderInternal {
|
||||
fn new(cfg: Config) -> Self {
|
||||
Self { cfg: Some(cfg) }
|
||||
}
|
||||
fn new_mutex() -> Arc<Mutex<Self>> {
|
||||
Arc::new(Mutex::new(Self { cfg: None }))
|
||||
}
|
||||
fn init(&mut self, cfg: Config) -> anyhow::Result<()> {
|
||||
self.cfg = Some(cfg);
|
||||
self.spawn_watcher_thread()?;
|
||||
Ok(())
|
||||
}
|
||||
fn cfg(&self) -> &Config {
|
||||
if let Some(cfg) = &self.cfg {
|
||||
cfg
|
||||
} else {
|
||||
panic!();
|
||||
}
|
||||
}
|
||||
fn spawn_watcher_thread(&mut self) -> anyhow::Result<()> {
|
||||
async fn watcher() {}
|
||||
|
||||
tokio::spawn(watcher());
|
||||
Ok(())
|
||||
}
|
||||
async fn run_command(&self, host: &ConfigHost, cmd: &str) -> anyhow::Result<String> {
|
||||
let dev = MikrotikDevice::connect(
|
||||
format!("{}", &host.ip),
|
||||
&host.username(self.cfg()),
|
||||
Some(&host.password(self.cfg())),
|
||||
)
|
||||
.await?;
|
||||
|
||||
let mut rx = dev
|
||||
.send_command(CommandBuilder::new().command(cmd).build())
|
||||
.await?;
|
||||
|
||||
let mut res = String::new();
|
||||
|
||||
while let Some(event) = rx.recv().await {
|
||||
match event {
|
||||
Event::Reply { response, .. } => {
|
||||
log::debug!("({}) {:?}", &host.ip, response.attributes);
|
||||
}
|
||||
Event::Done { .. } => break,
|
||||
Event::Trap { response, .. } => {
|
||||
log::error!("({}) {:?}", &host.ip, response.message);
|
||||
}
|
||||
other => println!("{other:?}"),
|
||||
}
|
||||
}
|
||||
Ok(res)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user