60 lines
1.7 KiB
Rust
60 lines
1.7 KiB
Rust
use std::io::prelude::*;
|
|
use std::net::TcpStream;
|
|
use ssh2::Session;
|
|
use clap::Parser;
|
|
use emojis_rs::*;
|
|
|
|
//const USER: &str = "aaronh";
|
|
//const EDI: &str = "100.94.173.5:22";
|
|
|
|
#[derive(Parser, Debug)]
|
|
#[command(version, about, long_about = None)]
|
|
struct Args {
|
|
#[arg(short, long)]
|
|
username: String,
|
|
|
|
#[arg(short, long)]
|
|
ip: String,
|
|
}
|
|
|
|
fn main() {
|
|
let args = Args::parse();
|
|
|
|
let tcp = TcpStream::connect(args.ip).expect("Failed to connect to SSH server");
|
|
let mut sess = Session::new().expect("Failed to create SSH session");
|
|
sess.set_tcp_stream(tcp);
|
|
sess.handshake().expect("SSH handshake failed");
|
|
|
|
sess.userauth_agent(&args.username).expect("SSH authentication failed");
|
|
|
|
if sess.authenticated() {
|
|
println!("Authentication successful! {EMOJI_CHECK}");
|
|
} else {
|
|
println!("Authentication failed! {EMOJI_CROSS}");
|
|
return;
|
|
}
|
|
|
|
println!("-------------------------");
|
|
|
|
for service in ["gollum", "wastebin", "hydra-server"] {
|
|
check_service_status(&sess, service);
|
|
}
|
|
}
|
|
|
|
fn check_service_status(sess: &Session, service: &str) {
|
|
let mut channel = sess.channel_session().expect("Failed to open SSH channel");
|
|
channel.exec(&format!("systemctl is-active {service}")).expect("Failed to run systemctl");
|
|
|
|
let mut output = String::new();
|
|
channel.read_to_string(&mut output).expect("Failed to read channel output");
|
|
channel.wait_close().expect("Failed to close SSH channel");
|
|
|
|
let exit_code = channel.exit_status().expect("Failed to get exit status");
|
|
|
|
if exit_code == 0 {
|
|
println!("{service} service is active {EMOJI_CHECK}");
|
|
} else {
|
|
println!("{service} service is inactive {EMOJI_CROSS} (exit code: {exit_code})");
|
|
}
|
|
}
|
|
|