mirror of
https://github.com/house-of-vanity/rexec.git
synced 2025-07-08 17:24:06 +00:00
Match ip to hostnames
This commit is contained in:
44
src/main.rs
44
src/main.rs
@ -6,8 +6,11 @@ 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;
|
||||||
|
use std::collections::{HashMap, HashSet};
|
||||||
use std::fs::read_to_string;
|
use std::fs::read_to_string;
|
||||||
|
use std::hash::Hash;
|
||||||
use std::net::IpAddr;
|
use std::net::IpAddr;
|
||||||
|
use std::str::FromStr;
|
||||||
|
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate log;
|
extern crate log;
|
||||||
@ -21,15 +24,18 @@ struct Args {
|
|||||||
#[arg(short, long, default_value_t = whoami::username())]
|
#[arg(short, long, default_value_t = whoami::username())]
|
||||||
username: String,
|
username: String,
|
||||||
|
|
||||||
#[arg(short, long, help = "Use known_hosts to build servers list.")]
|
#[arg(short, long = "kh", help = "Use known_hosts to build servers list")]
|
||||||
expression: String,
|
known_hosts: String,
|
||||||
|
|
||||||
#[arg(short, long, help = "Command to execute on servers.")]
|
#[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")]
|
#[arg(long, default_value_t = false, help = "Show exit code ONLY")]
|
||||||
code: bool,
|
code: bool,
|
||||||
|
|
||||||
|
#[arg(long, default_value_t = false, help = "Don't ask for confirmation")]
|
||||||
|
noconfirm: bool,
|
||||||
|
|
||||||
#[arg(short, long, default_value_t = 100)]
|
#[arg(short, long, default_value_t = 100)]
|
||||||
parallel: i32,
|
parallel: i32,
|
||||||
}
|
}
|
||||||
@ -74,7 +80,7 @@ fn main() {
|
|||||||
|
|
||||||
let known_hosts = read_known_hosts();
|
let known_hosts = read_known_hosts();
|
||||||
// Build regex
|
// Build regex
|
||||||
let re = Regex::new(&args.expression).unwrap();
|
let re = Regex::new(&args.known_hosts).unwrap();
|
||||||
// match hostnames from known_hosts to regex
|
// 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()
|
||||||
@ -86,6 +92,7 @@ fn main() {
|
|||||||
|
|
||||||
// Build MasshHostConfig hostnames list
|
// Build MasshHostConfig hostnames list
|
||||||
let mut massh_hosts: Vec<MasshHostConfig> = vec![];
|
let mut massh_hosts: Vec<MasshHostConfig> = vec![];
|
||||||
|
let mut hosts_and_ips: HashMap<IpAddr, String> = HashMap::new();
|
||||||
info!("Matched hosts:");
|
info!("Matched hosts:");
|
||||||
for host in matched_hosts.iter() {
|
for host in matched_hosts.iter() {
|
||||||
let ip = match lookup_host(&host.name) {
|
let ip = match lookup_host(&host.name) {
|
||||||
@ -96,6 +103,7 @@ fn main() {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
info!("{} [{}]", &host.name, ip);
|
info!("{} [{}]", &host.name, ip);
|
||||||
|
hosts_and_ips.insert(ip, host.name.clone());
|
||||||
massh_hosts.push(MasshHostConfig {
|
massh_hosts.push(MasshHostConfig {
|
||||||
addr: ip,
|
addr: ip,
|
||||||
auth: None,
|
auth: None,
|
||||||
@ -103,13 +111,11 @@ fn main() {
|
|||||||
user: None,
|
user: None,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// Build MasshConfig using massh_hosts vector
|
// 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: args.username,
|
||||||
default_user: "abogomyakov".to_string(),
|
|
||||||
threads: args.parallel as u64,
|
threads: args.parallel as u64,
|
||||||
timeout: 0,
|
timeout: 0,
|
||||||
hosts: massh_hosts,
|
hosts: massh_hosts,
|
||||||
@ -117,13 +123,14 @@ fn main() {
|
|||||||
let massh = MasshClient::from(&config);
|
let massh = MasshClient::from(&config);
|
||||||
|
|
||||||
// Ask for confirmation
|
// Ask for confirmation
|
||||||
if Confirm::new()
|
if args.noconfirm == true
|
||||||
.with_prompt(format!(
|
|| Confirm::new()
|
||||||
"Continue on following {} servers?",
|
.with_prompt(format!(
|
||||||
&config.hosts.len()
|
"Continue on following {} servers?",
|
||||||
))
|
&config.hosts.len()
|
||||||
.interact()
|
))
|
||||||
.unwrap()
|
.interact()
|
||||||
|
.unwrap()
|
||||||
{
|
{
|
||||||
info!("\n");
|
info!("\n");
|
||||||
info!("Run command on {} servers.", &config.hosts.len());
|
info!("Run command on {} servers.", &config.hosts.len());
|
||||||
@ -134,7 +141,12 @@ fn main() {
|
|||||||
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() {
|
||||||
info!("{}", host.yellow().bold());
|
let ip: String = host.split('@').collect::<Vec<_>>()[1]
|
||||||
|
.split(':')
|
||||||
|
.collect::<Vec<_>>()[0].to_string();
|
||||||
|
let ip = ip.parse::<IpAddr>().unwrap();
|
||||||
|
//let ip = hosts_and_ips.get(&ip).unwrap_or(&"Couldn't parse IP".yellow().bold().to_string());
|
||||||
|
info!("{}", hosts_and_ips.get(&ip).unwrap_or(&"Couldn't parse IP".yellow().bold().to_string()));
|
||||||
let output = result.unwrap();
|
let output = result.unwrap();
|
||||||
if output.exit_status == 0 {
|
if output.exit_status == 0 {
|
||||||
info!("Code {}", output.exit_status.to_string().green());
|
info!("Code {}", output.exit_status.to_string().green());
|
||||||
|
Reference in New Issue
Block a user