2023-06-16 14:31:16 +03:00
|
|
|
use colored::*;
|
|
|
|
use dialoguer::Confirm;
|
2023-06-15 16:30:49 +03:00
|
|
|
use dns_lookup::lookup_host;
|
2023-06-16 14:31:16 +03:00
|
|
|
use env_logger::Env;
|
2023-06-16 17:38:56 +03:00
|
|
|
use itertools::Itertools;
|
2023-06-16 14:31:16 +03:00
|
|
|
use log::{error, info};
|
|
|
|
use massh::{MasshClient, MasshConfig, MasshHostConfig, SshAuth};
|
|
|
|
use regex::Regex;
|
|
|
|
use std::fs::read_to_string;
|
|
|
|
use std::net::IpAddr;
|
|
|
|
|
|
|
|
#[macro_use]
|
|
|
|
extern crate log;
|
|
|
|
|
|
|
|
use clap::Parser;
|
|
|
|
|
2023-06-16 17:38:56 +03:00
|
|
|
// parse args
|
2023-06-16 14:31:16 +03:00
|
|
|
#[derive(Parser, Debug)]
|
|
|
|
#[command(author, version, about, long_about = None)]
|
|
|
|
struct Args {
|
|
|
|
#[arg(short, long, default_value_t = whoami::username())]
|
|
|
|
username: String,
|
|
|
|
|
2023-06-16 17:38:56 +03:00
|
|
|
#[arg(short, long, help = "Use known_hosts to build servers list.")]
|
|
|
|
expression: String,
|
2023-06-16 14:31:16 +03:00
|
|
|
|
2023-06-16 17:38:56 +03:00
|
|
|
#[arg(short, long, help = "Command to execute on servers.")]
|
2023-06-16 14:31:16 +03:00
|
|
|
command: String,
|
2023-06-16 17:38:56 +03:00
|
|
|
|
|
|
|
#[arg(short, long, default_value_t = false, help = "Show exit code ONLY")]
|
|
|
|
code: bool,
|
|
|
|
|
|
|
|
#[arg(short, long, default_value_t = 100)]
|
|
|
|
parallel: i32,
|
2023-06-16 14:31:16 +03:00
|
|
|
}
|
|
|
|
|
2023-06-16 17:38:56 +03:00
|
|
|
// Represent line from known_hosts file
|
|
|
|
#[derive(Debug, Default, Clone, PartialEq, Eq, Hash)]
|
2023-06-16 14:31:16 +03:00
|
|
|
struct KnownHost {
|
|
|
|
name: String,
|
|
|
|
ip: Option<IpAddr>,
|
|
|
|
}
|
|
|
|
|
2023-06-16 17:38:56 +03:00
|
|
|
// impl KnownHost {
|
|
|
|
// fn new(name: String) -> KnownHost {
|
|
|
|
// KnownHost { name, ip: None }
|
|
|
|
// }
|
|
|
|
// }
|
2023-06-15 16:30:49 +03:00
|
|
|
|
2023-06-16 17:38:56 +03:00
|
|
|
// Read known_hosts file
|
2023-06-16 14:31:16 +03:00
|
|
|
fn read_known_hosts() -> Vec<KnownHost> {
|
|
|
|
let mut result: Vec<KnownHost> = Vec::new();
|
|
|
|
|
|
|
|
for line in read_to_string(format!("/home/{}/.ssh/known_hosts", whoami::username()))
|
|
|
|
.unwrap()
|
|
|
|
.lines()
|
|
|
|
{
|
|
|
|
let line = line.split(" ").collect::<Vec<&str>>();
|
|
|
|
let hostname = line[0];
|
|
|
|
result.push(KnownHost {
|
|
|
|
name: hostname.to_string(),
|
|
|
|
ip: None,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
result
|
|
|
|
}
|
2023-06-15 17:58:54 +03:00
|
|
|
|
2023-06-15 16:30:49 +03:00
|
|
|
fn main() {
|
2023-06-16 14:31:16 +03:00
|
|
|
env_logger::Builder::from_env(Env::default().default_filter_or("info"))
|
|
|
|
.format_timestamp(None)
|
|
|
|
.format_target(false)
|
|
|
|
.init();
|
|
|
|
let args = Args::parse();
|
|
|
|
|
|
|
|
let known_hosts = read_known_hosts();
|
2023-06-16 17:38:56 +03:00
|
|
|
// Build regex
|
|
|
|
let re = Regex::new(&args.expression).unwrap();
|
|
|
|
// match hostnames from known_hosts to regex
|
2023-06-16 14:31:16 +03:00
|
|
|
let mut matched_hosts: Vec<KnownHost> = known_hosts
|
|
|
|
.into_iter()
|
|
|
|
.filter(|r| re.is_match(&r.name.clone()))
|
|
|
|
.collect();
|
|
|
|
|
2023-06-16 17:38:56 +03:00
|
|
|
// Dedup hosts from known_hosts file
|
|
|
|
let mut matched_hosts: Vec<_> = matched_hosts.into_iter().unique().collect();
|
|
|
|
|
|
|
|
// Build MasshHostConfig hostnames list
|
2023-06-16 14:31:16 +03:00
|
|
|
let mut massh_hosts: Vec<MasshHostConfig> = vec![];
|
|
|
|
info!("Matched hosts:");
|
|
|
|
for host in matched_hosts.iter() {
|
|
|
|
let ip = match lookup_host(&host.name) {
|
|
|
|
Ok(ip) => ip[0],
|
|
|
|
Err(_) => {
|
|
|
|
error!("{} couldn't ve resolved.", &host.name.red());
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
info!("{} [{}]", &host.name, ip);
|
|
|
|
massh_hosts.push(MasshHostConfig {
|
|
|
|
addr: ip,
|
|
|
|
auth: None,
|
|
|
|
port: None,
|
|
|
|
user: None,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-06-16 17:38:56 +03:00
|
|
|
// Build MasshConfig using massh_hosts vector
|
2023-06-15 16:30:49 +03:00
|
|
|
let config = MasshConfig {
|
|
|
|
default_auth: SshAuth::Agent,
|
|
|
|
default_port: 22,
|
2023-06-16 14:31:16 +03:00
|
|
|
//default_user: whoami::username(),
|
2023-06-15 16:30:49 +03:00
|
|
|
default_user: "abogomyakov".to_string(),
|
2023-06-16 17:38:56 +03:00
|
|
|
threads: args.parallel as u64,
|
2023-06-15 16:30:49 +03:00
|
|
|
timeout: 0,
|
2023-06-16 14:31:16 +03:00
|
|
|
hosts: massh_hosts,
|
2023-06-15 16:30:49 +03:00
|
|
|
};
|
|
|
|
let massh = MasshClient::from(&config);
|
|
|
|
|
2023-06-16 17:38:56 +03:00
|
|
|
// Ask for confirmation
|
2023-06-16 14:31:16 +03:00
|
|
|
if Confirm::new()
|
|
|
|
.with_prompt(format!(
|
|
|
|
"Continue on following {} servers?",
|
|
|
|
&config.hosts.len()
|
|
|
|
))
|
|
|
|
.interact()
|
|
|
|
.unwrap()
|
|
|
|
{
|
|
|
|
info!("\n");
|
|
|
|
info!("Run command on {} servers.", &config.hosts.len());
|
2023-06-16 17:38:56 +03:00
|
|
|
info!("\n");
|
|
|
|
|
|
|
|
// Run a command on all the configured hosts.
|
|
|
|
// Receive the result of the command for each host and print its output.
|
2023-06-16 14:31:16 +03:00
|
|
|
let rx = massh.execute(args.command);
|
2023-06-15 16:30:49 +03:00
|
|
|
|
2023-06-16 14:31:16 +03:00
|
|
|
while let Ok((host, result)) = rx.recv() {
|
|
|
|
info!("{}", host.yellow().bold());
|
|
|
|
let output = result.unwrap();
|
|
|
|
if output.exit_status == 0 {
|
|
|
|
info!("Code {}", output.exit_status.to_string().green());
|
|
|
|
} else {
|
|
|
|
info!("Code {}", output.exit_status.to_string().red());
|
|
|
|
};
|
2023-06-16 17:38:56 +03:00
|
|
|
if !args.code {
|
|
|
|
info!("STDOUT:\n{}", String::from_utf8(output.stdout).unwrap());
|
|
|
|
info!("STDERR:\n{}", String::from_utf8(output.stderr).unwrap());
|
|
|
|
}
|
2023-06-16 14:31:16 +03:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
warn!("Stopped");
|
2023-06-15 16:30:49 +03:00
|
|
|
}
|
|
|
|
}
|