mirror of
https://github.com/house-of-vanity/rexec.git
synced 2025-07-08 09:14:08 +00:00
Compare commits
23 Commits
Author | SHA1 | Date | |
---|---|---|---|
1eb9c5c17d | |||
70136e9074 | |||
2334c1a8f3 | |||
da6ae2ce37 | |||
e2cc36adf1 | |||
080ce8984a | |||
b360e29666 | |||
1d26a9c6be | |||
48e7e2a4e4 | |||
aa0a4e8bd2 | |||
554b68b51e | |||
c206ddaef2 | |||
e620348834 | |||
dc1a67fe01 | |||
1d208149f5 | |||
093157b6ef | |||
04da0fb97c | |||
967d9c0e7d | |||
eb0fd40d9f | |||
d0bbb29078 | |||
5a388853c5 | |||
41bf46c408 | |||
a2b05a7fa6 |
2
.github/workflows/release.yml
vendored
2
.github/workflows/release.yml
vendored
@ -67,7 +67,7 @@ jobs:
|
|||||||
|
|
||||||
|
|
||||||
release:
|
release:
|
||||||
name: Release binary
|
name: Making New Release ^_^
|
||||||
needs:
|
needs:
|
||||||
- build
|
- build
|
||||||
- build_arch
|
- build_arch
|
||||||
|
2
Cargo.lock
generated
2
Cargo.lock
generated
@ -1066,7 +1066,7 @@ checksum = "436b050e76ed2903236f032a59761c1eb99e1b0aead2c257922771dab1fc8c78"
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "rexec"
|
name = "rexec"
|
||||||
version = "1.0.6"
|
version = "1.0.9"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"brace-expand",
|
"brace-expand",
|
||||||
"clap 4.3.4",
|
"clap 4.3.4",
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "rexec"
|
name = "rexec"
|
||||||
version = "1.0.7"
|
version = "1.0.9"
|
||||||
readme = "https://github.com/house-of-vanity/rexec#readme"
|
readme = "https://github.com/house-of-vanity/rexec#readme"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
description = "Parallel SSH executor"
|
description = "Parallel SSH executor"
|
||||||
|
62
README.md
62
README.md
@ -1,14 +1,20 @@
|
|||||||
# rexec
|
# rexec
|
||||||
Parallel SSH executor in Rust. It can read servers from `~/.ssh/known_hosts`
|
**Parallel SSH executor** in Rust with static binary. It can read servers from `~/.ssh/known_hosts`
|
||||||
|
|
||||||
or even expand servers from bash expanshion line `cassandra.{prod,dev}.example.com`
|
|
||||||
|
|
||||||
|
or even expand servers from bash expanshion line `cassandra-[1:5].{prod,dev}.example.com`
|
||||||
|
|
||||||
[](https://github.com/house-of-vanity/rexec/actions/workflows/release.yml)
|
[](https://github.com/house-of-vanity/rexec/actions/workflows/release.yml)
|
||||||
|
---
|
||||||
|
|
||||||
|
## Usage
|
||||||
```shell
|
```shell
|
||||||
[ab@test]$ ./rexec -k admin-gce-sc.* --help
|
_|_|_|_| _| _| _|_|_|_| _|_|_|
|
||||||
|
_| _|_| _| _| _| _| _|
|
||||||
|
_|_| _|_|_| _| _|_|_| _|
|
||||||
|
_| _| _| _| _| _|
|
||||||
|
_| _|_|_|_| _| _| _|_|_|_| _|_|_|
|
||||||
|
|
||||||
|
|
||||||
Parallel SSH executor in Rust
|
Parallel SSH executor in Rust
|
||||||
|
|
||||||
Usage: rexec [OPTIONS] --expression <EXPRESSION> --command <COMMAND>
|
Usage: rexec [OPTIONS] --expression <EXPRESSION> --command <COMMAND>
|
||||||
@ -24,9 +30,44 @@ Options:
|
|||||||
-h, --help Print help
|
-h, --help Print help
|
||||||
-V, --version Print version
|
-V, --version Print version
|
||||||
```
|
```
|
||||||
|
---
|
||||||
|
|
||||||
|
## Examples
|
||||||
|
```shell
|
||||||
|
[ab@test]$ rexec -e 'cassandra-gce-sc-[1:3].bbs-prod.*' -u ab -c 'df -h /srv/cassandra'
|
||||||
|
[INFO ] Using string expansion to build server list.
|
||||||
|
[INFO ] Matched hosts:
|
||||||
|
[INFO ] cassandra-gce-sc-1.bbs-prod.* [35.221.18.35]
|
||||||
|
[INFO ] cassandra-gce-sc-2.bbs-prod.* [35.212.13.174]
|
||||||
|
[INFO ] cassandra-gce-sc-3.bbs-prod.* [35.211.213.123]
|
||||||
|
Continue on following 3 servers? y
|
||||||
|
[INFO ]
|
||||||
|
|
||||||
|
[INFO ] Run command on 3 servers.
|
||||||
|
[INFO ]
|
||||||
|
|
||||||
|
[INFO ] cassandra-gce-sc-2.bbs-prod.*
|
||||||
|
Code 0
|
||||||
|
STDOUT:
|
||||||
|
Filesystem Size Used Avail Use% Mounted on
|
||||||
|
/dev/mapper/storage-cassandra 1.0T 613G 411G 60% /srv/cassandra
|
||||||
|
|
||||||
|
[INFO ] cassandra-gce-sc-1.bbs-prod.*
|
||||||
|
Code 0
|
||||||
|
STDOUT:
|
||||||
|
Filesystem Size Used Avail Use% Mounted on
|
||||||
|
/dev/mapper/storage-cassandra 1.0T 594G 430G 59% /srv/cassandra
|
||||||
|
|
||||||
|
[INFO ] cassandra-gce-sc-3.bbs-prod.*
|
||||||
|
Code 0
|
||||||
|
STDOUT:
|
||||||
|
Filesystem Size Used Avail Use% Mounted on
|
||||||
|
/dev/mapper/storage-cassandra 1.0T 523G 502G 52% /srv/cassandra
|
||||||
|
```
|
||||||
|
---
|
||||||
|
|
||||||
```shell
|
```shell
|
||||||
[ab@test debug]$ ./rexec -u ab -k -c uptime -e admin.* -f
|
[ab@test]$ ./rexec -u ab -k -c uptime -e admin.* -f
|
||||||
[INFO ] Matched hosts:
|
[INFO ] Matched hosts:
|
||||||
[INFO ] admin-gce-sc-1.lca-prod.** [35.211.27.195]
|
[INFO ] admin-gce-sc-1.lca-prod.** [35.211.27.195]
|
||||||
[INFO ] admin-gce-sc-1.mmk-prod.** [35.211.79.202]
|
[INFO ] admin-gce-sc-1.mmk-prod.** [35.211.79.202]
|
||||||
@ -36,27 +77,18 @@ Options:
|
|||||||
|
|
||||||
[INFO ] Run command on 3 servers.
|
[INFO ] Run command on 3 servers.
|
||||||
|
|
||||||
|
|
||||||
[INFO ] admin.gnb-prod.**
|
[INFO ] admin.gnb-prod.**
|
||||||
Code 0
|
Code 0
|
||||||
STDOUT:
|
STDOUT:
|
||||||
23:31:21 up 294 days, 14:14, 0 users, load average: 0.53, 0.64, 0.52
|
23:31:21 up 294 days, 14:14, 0 users, load average: 0.53, 0.64, 0.52
|
||||||
|
|
||||||
STDERR:
|
|
||||||
|
|
||||||
[INFO ] admin.abe-prod.**
|
[INFO ] admin.abe-prod.**
|
||||||
Code 0
|
Code 0
|
||||||
STDOUT:
|
STDOUT:
|
||||||
23:31:22 up 154 days, 9:24, 0 users, load average: 0.31, 0.25, 0.18
|
23:31:22 up 154 days, 9:24, 0 users, load average: 0.31, 0.25, 0.18
|
||||||
|
|
||||||
STDERR:
|
|
||||||
|
|
||||||
[INFO ] admin-gce-be-1.toy-prod.**
|
[INFO ] admin-gce-be-1.toy-prod.**
|
||||||
Code 0
|
Code 0
|
||||||
STDOUT:
|
STDOUT:
|
||||||
23:31:22 up 98 days, 6:20, 0 users, load average: 0.88, 0.74, 0.80
|
23:31:22 up 98 days, 6:20, 0 users, load average: 0.88, 0.74, 0.80
|
||||||
|
|
||||||
STDERR:
|
|
||||||
no server running on /tmp/tmux-1019/default
|
|
||||||
open terminal failed: not a terminal
|
|
||||||
```
|
```
|
||||||
|
92
src/main.rs
92
src/main.rs
@ -9,7 +9,6 @@ use std::process;
|
|||||||
|
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
use colored::*;
|
use colored::*;
|
||||||
//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 itertools::Itertools;
|
||||||
@ -35,9 +34,10 @@ struct Args {
|
|||||||
#[arg(
|
#[arg(
|
||||||
short,
|
short,
|
||||||
long,
|
long,
|
||||||
|
num_args = 1..,
|
||||||
help = "Expression to build server list. List and range expansion are supported. Example: 'web-[1:12]-io-{prod,dev}'"
|
help = "Expression to build server list. List and range expansion are supported. Example: 'web-[1:12]-io-{prod,dev}'"
|
||||||
)]
|
)]
|
||||||
expression: String,
|
expression: Vec<String>,
|
||||||
|
|
||||||
#[arg(short, long, help = "Command to execute on servers")]
|
#[arg(short, long, help = "Command to execute on servers")]
|
||||||
command: String,
|
command: String,
|
||||||
@ -157,22 +157,30 @@ fn main() {
|
|||||||
let hosts = if args.known_hosts {
|
let hosts = if args.known_hosts {
|
||||||
info!("Using ~/.ssh/known_hosts to build server list.");
|
info!("Using ~/.ssh/known_hosts to build server list.");
|
||||||
let known_hosts = read_known_hosts();
|
let known_hosts = read_known_hosts();
|
||||||
// Build regex
|
let mut all_hosts = Vec::new();
|
||||||
let re = match Regex::new(&args.expression) {
|
for expression in args.expression.iter() {
|
||||||
Ok(result) => result,
|
let re = match Regex::new(expression) {
|
||||||
Err(e) => {
|
Ok(result) => result,
|
||||||
error!("Error parsing regex. {}", e);
|
Err(e) => {
|
||||||
process::exit(1);
|
error!("Error parsing regex. {}", e);
|
||||||
}
|
process::exit(1);
|
||||||
};
|
}
|
||||||
// match hostnames from known_hosts to regex
|
};
|
||||||
known_hosts
|
let matched: Vec<Host> = known_hosts
|
||||||
.into_iter()
|
.clone()
|
||||||
.filter(|r| re.is_match(&r.name.clone()))
|
.into_iter()
|
||||||
.collect()
|
.filter(|r| re.is_match(&r.name.clone()))
|
||||||
|
.collect();
|
||||||
|
all_hosts.extend(matched);
|
||||||
|
}
|
||||||
|
all_hosts
|
||||||
} else {
|
} else {
|
||||||
info!("Using string expansion to build server list.");
|
info!("Using string expansion to build server list.");
|
||||||
expand_string(&args.expression)
|
let mut all_hosts = Vec::new();
|
||||||
|
for expression in args.expression.iter() {
|
||||||
|
all_hosts.extend(expand_string(expression));
|
||||||
|
}
|
||||||
|
all_hosts
|
||||||
};
|
};
|
||||||
|
|
||||||
// Dedup hosts from known_hosts file
|
// Dedup hosts from known_hosts file
|
||||||
@ -234,9 +242,7 @@ fn main() {
|
|||||||
_ => unreachable!(),
|
_ => unreachable!(),
|
||||||
})
|
})
|
||||||
{
|
{
|
||||||
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.
|
// Run a command on all the configured hosts.
|
||||||
// Receive the result of the command for each host and print its output.
|
// Receive the result of the command for each host and print its output.
|
||||||
@ -248,8 +254,8 @@ fn main() {
|
|||||||
.collect::<Vec<_>>()[0]
|
.collect::<Vec<_>>()[0]
|
||||||
.to_string();
|
.to_string();
|
||||||
let ip = ip.parse::<IpAddr>().unwrap();
|
let ip = ip.parse::<IpAddr>().unwrap();
|
||||||
info!(
|
println!(
|
||||||
"{}",
|
"\n{}",
|
||||||
hosts_and_ips
|
hosts_and_ips
|
||||||
.get(&ip)
|
.get(&ip)
|
||||||
.unwrap_or(&"Couldn't parse IP".to_string())
|
.unwrap_or(&"Couldn't parse IP".to_string())
|
||||||
@ -265,14 +271,50 @@ fn main() {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
if output.exit_status == 0 {
|
let code_string = if output.exit_status == 0 {
|
||||||
println!("Code {}", output.exit_status);
|
format!("{}", output.exit_status.to_string().green())
|
||||||
} else {
|
} else {
|
||||||
error!("Code {}", output.exit_status);
|
format!("{}", output.exit_status.to_string().red())
|
||||||
};
|
};
|
||||||
|
println!(
|
||||||
|
"{}",
|
||||||
|
format!(
|
||||||
|
"Exit code [{}] | std out/err [{}/{}] bytes",
|
||||||
|
code_string,
|
||||||
|
output.stdout.len(),
|
||||||
|
output.stderr.len()
|
||||||
|
)
|
||||||
|
.bold()
|
||||||
|
);
|
||||||
|
|
||||||
if !args.code {
|
if !args.code {
|
||||||
println!("STDOUT:\n{}", String::from_utf8(output.stdout).unwrap());
|
match String::from_utf8(output.stdout) {
|
||||||
println!("STDERR:\n{}", String::from_utf8(output.stderr).unwrap());
|
Ok(stdout) => match stdout.as_str() {
|
||||||
|
"" => {}
|
||||||
|
_ => {
|
||||||
|
let prefix = if output.exit_status != 0 {
|
||||||
|
format!("{}", "║".cyan())
|
||||||
|
} else {
|
||||||
|
format!("{}", "║".green())
|
||||||
|
};
|
||||||
|
for line in stdout.lines() {
|
||||||
|
println!("{} {}", prefix, line);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
Err(_) => {}
|
||||||
|
}
|
||||||
|
match String::from_utf8(output.stderr) {
|
||||||
|
Ok(stderr) => match stderr.as_str() {
|
||||||
|
"" => {}
|
||||||
|
_ => {
|
||||||
|
for line in stderr.lines() {
|
||||||
|
println!("{} {}", "║".red(), line);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
Err(_) => {}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
Reference in New Issue
Block a user