mirror of
https://github.com/house-of-vanity/rexec.git
synced 2025-07-07 08:54:06 +00:00
Added dedup of known_hosts
This commit is contained in:
16
Cargo.lock
generated
16
Cargo.lock
generated
@ -289,6 +289,12 @@ dependencies = [
|
|||||||
"windows-sys 0.48.0",
|
"windows-sys 0.48.0",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "either"
|
||||||
|
version = "1.8.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "7fcaabb2fef8c910e7f4c7ce9f67a1283a1715879a7c230ca9d6d1ae31f16d91"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "encode_unicode"
|
name = "encode_unicode"
|
||||||
version = "0.3.6"
|
version = "0.3.6"
|
||||||
@ -630,6 +636,15 @@ dependencies = [
|
|||||||
"windows-sys 0.48.0",
|
"windows-sys 0.48.0",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "itertools"
|
||||||
|
version = "0.10.5"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "b0fd2260e829bddf4cb6ea802289de2f86d6a7a690192fbe91b3f46e0f2c8473"
|
||||||
|
dependencies = [
|
||||||
|
"either",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "itoa"
|
name = "itoa"
|
||||||
version = "1.0.6"
|
version = "1.0.6"
|
||||||
@ -1029,6 +1044,7 @@ dependencies = [
|
|||||||
"dialoguer",
|
"dialoguer",
|
||||||
"dns-lookup",
|
"dns-lookup",
|
||||||
"env_logger",
|
"env_logger",
|
||||||
|
"itertools",
|
||||||
"lazy-st",
|
"lazy-st",
|
||||||
"log",
|
"log",
|
||||||
"massh",
|
"massh",
|
||||||
|
@ -16,3 +16,4 @@ regex = "1"
|
|||||||
clap = { version = "4.3.4", features = ["derive"] }
|
clap = { version = "4.3.4", features = ["derive"] }
|
||||||
colored = "2.0.0"
|
colored = "2.0.0"
|
||||||
dialoguer = "0.10.4"
|
dialoguer = "0.10.4"
|
||||||
|
itertools = "0.10.5"
|
||||||
|
61
src/main.rs
61
src/main.rs
@ -2,6 +2,7 @@ use colored::*;
|
|||||||
use dialoguer::Confirm;
|
use dialoguer::Confirm;
|
||||||
use dns_lookup::lookup_host;
|
use dns_lookup::lookup_host;
|
||||||
use env_logger::Env;
|
use env_logger::Env;
|
||||||
|
use itertools::Itertools;
|
||||||
use log::{error, info};
|
use log::{error, info};
|
||||||
use massh::{MasshClient, MasshConfig, MasshHostConfig, SshAuth};
|
use massh::{MasshClient, MasshConfig, MasshHostConfig, SshAuth};
|
||||||
use regex::Regex;
|
use regex::Regex;
|
||||||
@ -13,33 +14,40 @@ extern crate log;
|
|||||||
|
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
|
|
||||||
/// Simple program to greet a person
|
// parse args
|
||||||
#[derive(Parser, Debug)]
|
#[derive(Parser, Debug)]
|
||||||
#[command(author, version, about, long_about = None)]
|
#[command(author, version, about, long_about = None)]
|
||||||
struct Args {
|
struct Args {
|
||||||
/// Name of the person to greet
|
|
||||||
#[arg(short, long, default_value_t = whoami::username())]
|
#[arg(short, long, default_value_t = whoami::username())]
|
||||||
username: String,
|
username: String,
|
||||||
|
|
||||||
#[arg(short, long)]
|
#[arg(short, long, help = "Use known_hosts to build servers list.")]
|
||||||
pattern: String,
|
expression: String,
|
||||||
|
|
||||||
#[arg(short, long)]
|
#[arg(short, long, help = "Command to execute on servers.")]
|
||||||
command: String,
|
command: String,
|
||||||
|
|
||||||
|
#[arg(short, long, default_value_t = false, help = "Show exit code ONLY")]
|
||||||
|
code: bool,
|
||||||
|
|
||||||
|
#[arg(short, long, default_value_t = 100)]
|
||||||
|
parallel: i32,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Default)]
|
// Represent line from known_hosts file
|
||||||
|
#[derive(Debug, Default, Clone, PartialEq, Eq, Hash)]
|
||||||
struct KnownHost {
|
struct KnownHost {
|
||||||
name: String,
|
name: String,
|
||||||
ip: Option<IpAddr>,
|
ip: Option<IpAddr>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl KnownHost {
|
// impl KnownHost {
|
||||||
fn new(name: String) -> KnownHost {
|
// fn new(name: String) -> KnownHost {
|
||||||
KnownHost { name, ip: None }
|
// KnownHost { name, ip: None }
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
|
||||||
|
// Read known_hosts file
|
||||||
fn read_known_hosts() -> Vec<KnownHost> {
|
fn read_known_hosts() -> Vec<KnownHost> {
|
||||||
let mut result: Vec<KnownHost> = Vec::new();
|
let mut result: Vec<KnownHost> = Vec::new();
|
||||||
|
|
||||||
@ -49,10 +57,6 @@ fn read_known_hosts() -> Vec<KnownHost> {
|
|||||||
{
|
{
|
||||||
let line = line.split(" ").collect::<Vec<&str>>();
|
let line = line.split(" ").collect::<Vec<&str>>();
|
||||||
let hostname = line[0];
|
let hostname = line[0];
|
||||||
// let ip = match lookup_host(hostname) {
|
|
||||||
// Ok(ip) => ip[0],
|
|
||||||
// Err(_) => continue
|
|
||||||
// };
|
|
||||||
result.push(KnownHost {
|
result.push(KnownHost {
|
||||||
name: hostname.to_string(),
|
name: hostname.to_string(),
|
||||||
ip: None,
|
ip: None,
|
||||||
@ -69,13 +73,18 @@ fn main() {
|
|||||||
let args = Args::parse();
|
let args = Args::parse();
|
||||||
|
|
||||||
let known_hosts = read_known_hosts();
|
let known_hosts = read_known_hosts();
|
||||||
let re = Regex::new(&args.pattern).unwrap();
|
// Build regex
|
||||||
|
let re = Regex::new(&args.expression).unwrap();
|
||||||
|
// match hostnames from known_hosts to regex
|
||||||
let mut matched_hosts: Vec<KnownHost> = known_hosts
|
let mut matched_hosts: Vec<KnownHost> = known_hosts
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.filter(|r| re.is_match(&r.name.clone()))
|
.filter(|r| re.is_match(&r.name.clone()))
|
||||||
//.filter(|a| a.access.iter().any(|b| b.users.contains(&user)))
|
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
|
// Dedup hosts from known_hosts file
|
||||||
|
let mut matched_hosts: Vec<_> = matched_hosts.into_iter().unique().collect();
|
||||||
|
|
||||||
|
// Build MasshHostConfig hostnames list
|
||||||
let mut massh_hosts: Vec<MasshHostConfig> = vec![];
|
let mut massh_hosts: Vec<MasshHostConfig> = vec![];
|
||||||
info!("Matched hosts:");
|
info!("Matched hosts:");
|
||||||
for host in matched_hosts.iter() {
|
for host in matched_hosts.iter() {
|
||||||
@ -95,19 +104,19 @@ fn main() {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Build MasshConfig using massh_hosts vector
|
||||||
let config = MasshConfig {
|
let config = MasshConfig {
|
||||||
default_auth: SshAuth::Agent,
|
default_auth: SshAuth::Agent,
|
||||||
default_port: 22,
|
default_port: 22,
|
||||||
//default_user: whoami::username(),
|
//default_user: whoami::username(),
|
||||||
default_user: "abogomyakov".to_string(),
|
default_user: "abogomyakov".to_string(),
|
||||||
threads: 10,
|
threads: args.parallel as u64,
|
||||||
timeout: 0,
|
timeout: 0,
|
||||||
hosts: massh_hosts,
|
hosts: massh_hosts,
|
||||||
};
|
};
|
||||||
let massh = MasshClient::from(&config);
|
let massh = MasshClient::from(&config);
|
||||||
|
|
||||||
// Receive the result of the command for each host and print its output.
|
// Ask for confirmation
|
||||||
|
|
||||||
if Confirm::new()
|
if Confirm::new()
|
||||||
.with_prompt(format!(
|
.with_prompt(format!(
|
||||||
"Continue on following {} servers?",
|
"Continue on following {} servers?",
|
||||||
@ -118,7 +127,10 @@ fn main() {
|
|||||||
{
|
{
|
||||||
info!("\n");
|
info!("\n");
|
||||||
info!("Run command on {} servers.", &config.hosts.len());
|
info!("Run command on {} servers.", &config.hosts.len());
|
||||||
info!("\n"); // Run a command on all the configured hosts.
|
info!("\n");
|
||||||
|
|
||||||
|
// Run a command on all the configured hosts.
|
||||||
|
// Receive the result of the command for each host and print its output.
|
||||||
let rx = massh.execute(args.command);
|
let rx = massh.execute(args.command);
|
||||||
|
|
||||||
while let Ok((host, result)) = rx.recv() {
|
while let Ok((host, result)) = rx.recv() {
|
||||||
@ -129,9 +141,10 @@ fn main() {
|
|||||||
} else {
|
} else {
|
||||||
info!("Code {}", output.exit_status.to_string().red());
|
info!("Code {}", output.exit_status.to_string().red());
|
||||||
};
|
};
|
||||||
|
if !args.code {
|
||||||
info!("STDOUT:\n{}", String::from_utf8(output.stdout).unwrap());
|
info!("STDOUT:\n{}", String::from_utf8(output.stdout).unwrap());
|
||||||
info!("STDERR:\n{}", String::from_utf8(output.stderr).unwrap());
|
info!("STDERR:\n{}", String::from_utf8(output.stderr).unwrap());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
warn!("Stopped");
|
warn!("Stopped");
|
||||||
|
Reference in New Issue
Block a user