4 Commits
1.0.2 ... 1.0.3

Author SHA1 Message Date
e071381e03 Format Rust code using rustfmt 2023-07-25 16:20:32 +00:00
ad2d36bb35 Fixed string expansion 2023-07-25 19:18:45 +03:00
4f1657c37c Fixed string expansion 2023-07-25 19:17:32 +03:00
a28d1a3a38 WIP 2023-07-03 18:51:06 +03:00
3 changed files with 46 additions and 50 deletions

18
Cargo.lock generated
View File

@ -1045,15 +1045,15 @@ checksum = "436b050e76ed2903236f032a59761c1eb99e1b0aead2c257922771dab1fc8c78"
name = "rexec" name = "rexec"
version = "1.0.2" version = "1.0.2"
dependencies = [ dependencies = [
"brace-expand", "brace-expand",
"clap 4.3.4", "clap 4.3.4",
"colored", "colored",
"dialoguer", "dialoguer",
"dns-lookup", "dns-lookup",
"env_logger", "env_logger",
"itertools", "itertools",
"lazy-st", "lazy-st",
"log", "log",
"massh", "massh",
"regex", "regex",
"whoami", "whoami",

View File

@ -1,6 +1,6 @@
[package] [package]
name = "rexec" name = "rexec"
version = "1.0.2" version = "1.0.3"
edition = "2021" edition = "2021"
description = "Parallel SSH executor" description = "Parallel SSH executor"
repository = "https://github.com/house-of-vanity/rexec" repository = "https://github.com/house-of-vanity/rexec"

View File

@ -17,7 +17,6 @@ use log::{error, info};
use massh::{MasshClient, MasshConfig, MasshHostConfig, SshAuth}; use massh::{MasshClient, MasshConfig, MasshHostConfig, SshAuth};
use regex::Regex; use regex::Regex;
// Define args // Define args
#[derive(Parser, Debug)] #[derive(Parser, Debug)]
#[command(author = "AB ab@hexor.ru", version, about = "Parallel SSH executor in Rust", long_about = None)] #[command(author = "AB ab@hexor.ru", version, about = "Parallel SSH executor in Rust", long_about = None)]
@ -28,7 +27,11 @@ struct Args {
#[arg(short, long, help = "Use known_hosts to build servers list")] #[arg(short, long, help = "Use known_hosts to build servers list")]
known_hosts: bool, known_hosts: bool,
#[arg(short, long, help = "Expression to build server list. List and range expansion available. Example: 'web-[1:12]-io-{prod,dev}'")] #[arg(
short,
long,
help = "Expression to build server list. List and range expansion available. Example: 'web-[1:12]-io-{prod,dev}'"
)]
expression: String, expression: String,
#[arg(short, long, help = "Command to execute on servers")] #[arg(short, long, help = "Command to execute on servers")]
@ -74,48 +77,41 @@ fn read_known_hosts() -> Vec<Host> {
result result
} }
fn expand_string(string: String) -> Vec<Host> { fn expand_range(start: i32, end: i32) -> Vec<String> {
let mut result: Vec<String> = Vec::new(); (start..=end).map(|i| i.to_string()).collect()
let mut _result: Vec<String> = Vec::new(); }
fn expand_list(list: &str) -> Vec<String> {
list.split(',').map(|s| s.to_string()).collect()
}
fn expand_string(s: &str) -> Vec<Host> {
let mut hosts: Vec<Host> = Vec::new(); let mut hosts: Vec<Host> = Vec::new();
let mut result = vec![s.to_string()];
if let Some(open_bracket_index) = string.find('[') { while let Some(r) = result.iter().find(|s| s.contains('[')) {
if let Some(close_bracket_index) = string.find(']') { let r = r.clone();
let prefix = &string[..open_bracket_index]; let start = r.find('[').unwrap();
let range = &string[open_bracket_index + 1..close_bracket_index]; let end = r.find(']').unwrap();
let postfix = &string[close_bracket_index + 1..]; let colon = r.find(':').unwrap();
let low = r[start + 1..colon].parse::<i32>().unwrap();
let parts: Vec<&str> = range.split(':').collect(); let high = r[colon + 1..end].parse::<i32>().unwrap();
result.retain(|s| s != &r);
if parts.len() == 2 { for val in expand_range(low, high) {
if let Ok(start) = parts[0].parse::<u32>() { let new_str = format!("{}{}{}", &r[..start], val, &r[end + 1..]);
if let Ok(end) = parts[1].parse::<u32>() { result.push(new_str);
for num in start..=end {
_result.push(format!("{}{}{}", prefix, num, postfix));
}
}
}
}
} }
} else {
_result.push(String::from(string));
} }
for string in _result { while let Some(r) = result.iter().find(|s| s.contains('{')) {
if let Some(open_brace_index) = string.find('{') { let r = r.clone();
if let Some(close_brace_index) = string.find('}') { let start = r.find('{').unwrap();
let prefix = &string[..open_brace_index]; let end = r.find('}').unwrap();
let list = &string[open_brace_index + 1..close_brace_index]; let list = &r[start + 1..end];
let postfix = &string[close_brace_index + 1..]; result.retain(|s| s != &r);
for val in expand_list(list) {
let items: Vec<&str> = list.split(',').collect(); let new_str = format!("{}{}{}", &r[..start], val, &r[end + 1..]);
result.push(new_str);
for item in items {
result.push(format!("{}{}{}", prefix, item, postfix));
}
}
} else {
result.push(String::from(string));
} }
} }
@ -153,7 +149,7 @@ fn main() {
.collect() .collect()
} else { } else {
info!("Using string expansion to build server list."); info!("Using string expansion to build server list.");
expand_string(args.expression) expand_string(&args.expression)
}; };
// Dedup hosts from known_hosts file // Dedup hosts from known_hosts file