From df7e881d64fdfb8430e4feccc94480821001b6ad Mon Sep 17 00:00:00 2001 From: pws1453 Date: Fri, 14 Apr 2023 16:45:12 -0400 Subject: [PATCH] LGTM --- Cargo.lock | 315 +++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 12 ++ output/127.0.0.1.txt | 28 ++++ src/example.yaml | 6 + src/main.rs | 94 +++++++++++++ 5 files changed, 455 insertions(+) create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 output/127.0.0.1.txt create mode 100644 src/example.yaml create mode 100644 src/main.rs diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..fb6b5e2 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,315 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "autocfg" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" + +[[package]] +name = "bitflags" +version = "1.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" + +[[package]] +name = "cc" +version = "1.0.79" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "50d30906286121d95be3d479533b458f87493b30a4b5f79a607db8f5d11aa91f" + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "hashbrown" +version = "0.12.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888" + +[[package]] +name = "indexmap" +version = "1.9.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bd070e393353796e801d209ad339e89596eb4c8d430d18ede6a1cced8fafbd99" +dependencies = [ + "autocfg", + "hashbrown", +] + +[[package]] +name = "instant" +version = "0.1.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a5bbe824c507c5da5956355e86a746d82e0e1464f65d862cc5e71da70e94b2c" +dependencies = [ + "cfg-if", +] + +[[package]] +name = "itoa" +version = "1.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "453ad9f582a441959e5f0d088b02ce04cfe8d51a8eaf077f12ac6d3e94164ca6" + +[[package]] +name = "libc" +version = "0.2.141" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3304a64d199bb964be99741b7a14d26972741915b3649639149b2479bb46f4b5" + +[[package]] +name = "libssh2-sys" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2dc8a030b787e2119a731f1951d6a773e2280c660f8ec4b0f5e1505a386e71ee" +dependencies = [ + "cc", + "libc", + "libz-sys", + "openssl-sys", + "pkg-config", + "vcpkg", +] + +[[package]] +name = "libz-sys" +version = "1.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9702761c3935f8cc2f101793272e202c72b99da8f4224a19ddcf1279a6450bbf" +dependencies = [ + "cc", + "libc", + "pkg-config", + "vcpkg", +] + +[[package]] +name = "lock_api" +version = "0.4.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "435011366fe56583b16cf956f9df0095b405b82d76425bc8981c0e22e60ec4df" +dependencies = [ + "autocfg", + "scopeguard", +] + +[[package]] +name = "openssl-sys" +version = "0.9.85" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0d3d193fb1488ad46ffe3aaabc912cc931d02ee8518fe2959aea8ef52718b0c0" +dependencies = [ + "cc", + "libc", + "pkg-config", + "vcpkg", +] + +[[package]] +name = "parking_lot" +version = "0.11.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7d17b78036a60663b797adeaee46f5c9dfebb86948d1255007a1d6be0271ff99" +dependencies = [ + "instant", + "lock_api", + "parking_lot_core", +] + +[[package]] +name = "parking_lot_core" +version = "0.8.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "60a2cfe6f0ad2bfc16aefa463b497d5c7a5ecd44a23efa72aa342d90177356dc" +dependencies = [ + "cfg-if", + "instant", + "libc", + "redox_syscall", + "smallvec", + "winapi", +] + +[[package]] +name = "pkg-config" +version = "0.3.26" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6ac9a59f73473f1b8d852421e59e64809f025994837ef743615c6d0c5b305160" + +[[package]] +name = "proc-macro2" +version = "1.0.56" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b63bdb0cd06f1f4dedf69b254734f9b45af66e4a031e42a7480257d9898b435" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.26" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4424af4bf778aae2051a77b60283332f386554255d722233d09fbfc7e30da2fc" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "redox_syscall" +version = "0.2.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fb5a58c1855b4b6819d59012155603f0b22ad30cad752600aadfcb695265519a" +dependencies = [ + "bitflags", +] + +[[package]] +name = "rpassword" +version = "7.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6678cf63ab3491898c0d021b493c94c9b221d91295294a2a5746eacbe5928322" +dependencies = [ + "libc", + "rtoolbox", + "winapi", +] + +[[package]] +name = "rtoolbox" +version = "0.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "034e22c514f5c0cb8a10ff341b9b048b5ceb21591f31c8f44c43b960f9b3524a" +dependencies = [ + "libc", + "winapi", +] + +[[package]] +name = "rust-ssh" +version = "0.1.0" +dependencies = [ + "rpassword", + "serde", + "serde_yaml", + "ssh2", +] + +[[package]] +name = "ryu" +version = "1.0.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f91339c0467de62360649f8d3e185ca8de4224ff281f66000de5eb2a77a79041" + +[[package]] +name = "scopeguard" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd" + +[[package]] +name = "serde" +version = "1.0.160" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bb2f3770c8bce3bcda7e149193a069a0f4365bda1fa5cd88e03bca26afc1216c" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.160" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "291a097c63d8497e00160b166a967a4a79c64f3facdd01cbd7502231688d77df" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "serde_yaml" +version = "0.9.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9d684e3ec7de3bf5466b32bd75303ac16f0736426e5a4e0d6e489559ce1249c" +dependencies = [ + "indexmap", + "itoa", + "ryu", + "serde", + "unsafe-libyaml", +] + +[[package]] +name = "smallvec" +version = "1.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a507befe795404456341dfab10cef66ead4c041f62b8b11bbb92bffe5d0953e0" + +[[package]] +name = "ssh2" +version = "0.9.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e7fe461910559f6d5604c3731d00d2aafc4a83d1665922e280f42f9a168d5455" +dependencies = [ + "bitflags", + "libc", + "libssh2-sys", + "parking_lot", +] + +[[package]] +name = "syn" +version = "2.0.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a34fcf3e8b60f57e6a14301a2e916d323af98b0ea63c599441eec8558660c822" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "unicode-ident" +version = "1.0.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e5464a87b239f13a63a501f2701565754bae92d243d4bb7eb12f6d57d2269bf4" + +[[package]] +name = "unsafe-libyaml" +version = "0.2.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1865806a559042e51ab5414598446a5871b561d21b6764f2eabb0dd481d880a6" + +[[package]] +name = "vcpkg" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426" + +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..9adbd82 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "rust-ssh" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +ssh2 = "0.9.4" +rpassword = "7.2.0" +serde = {version = "1.0.160", features = ["derive"] } +serde_yaml = "0.9.21" \ No newline at end of file diff --git a/output/127.0.0.1.txt b/output/127.0.0.1.txt new file mode 100644 index 0000000..223fb82 --- /dev/null +++ b/output/127.0.0.1.txt @@ -0,0 +1,28 @@ +Android +CloudFlair +CloudPeler +code +Desktop +Documents +Downloads +freshtomato-RT-AC66U_RT-AC6x-2022.6-AIO-64K +index.html +index.html.1 +index.html?videoId=6278997721001 +Music +nextcloud-password +oof.txt +OSX-KVM +OVAs +patout.csv +Pictures +Public +README.md +RobloxPlayerLauncher_d3d11.log +RobloxPlayerLauncher.dxvk-cache +sonarr +Templates +Transport_for_NSW_IoT_Insights_for_Public_Transport-6278997721001.mp4 +user.txt +Videos +VirtualBox VMs diff --git a/src/example.yaml b/src/example.yaml new file mode 100644 index 0000000..01ca19a --- /dev/null +++ b/src/example.yaml @@ -0,0 +1,6 @@ +groups: + - name: goodhosts + hosts: + - 127.0.0.1 + commands: + - ls \ No newline at end of file diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..e1a64c7 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,94 @@ +use std::net::TcpStream; +use std::io; +use ssh2::Session; +use rpassword::prompt_password; +use serde::{Deserialize, Serialize}; +use serde_yaml::from_str; +use std::fs::{self, File}; +use std::io::prelude::*; + +#[derive(Debug, Deserialize, Serialize)] +struct Group { + hosts: Vec, + commands: Vec, +} + +#[derive(Debug, Deserialize, Serialize)] +struct Inventory { + groups: Vec, +} + +fn main() { + let username = read_username(); + let password = read_password(); + + let inventory_file = read_inventory_file(); + let inventory = from_str::(&inventory_file).unwrap(); + + // Create the output directory if it doesn't exist + let output_dir = "output"; + fs::create_dir_all(output_dir).unwrap(); + + for group in inventory.groups { + println!("Executing commands on group {:?}", group.hosts); + for host in group.hosts { + let tcp = TcpStream::connect(format!("{}:22", host)).unwrap(); + let mut sess = Session::new().unwrap(); + sess.set_tcp_stream(tcp); + sess.handshake().unwrap(); + + match sess.userauth_password(&username, &password) { + Ok(_) => { + for command in &group.commands { + let output = execute_command(&mut sess, command); + let output_filename = format!("{}/{}.txt", output_dir, host); + let mut file = File::create(&output_filename).unwrap(); + file.write_all(output.as_bytes()).unwrap(); + + println!("Output saved to {}", output_filename); + } + }, + Err(err) => { + eprintln!("Failed to authenticate to {}: {}", host, err); + } + } + } + } +} + +fn read_username() -> String { + print!("Username: "); + let _ = io::stdout().flush(); + let mut username = String::new(); + let stdin = io::stdin(); + stdin.read_line(&mut username).unwrap(); + username.trim().to_string() +} + +fn read_password() -> String { + prompt_password("Enter password: ").unwrap() +} + +fn read_inventory_file() -> String { + print!("Enter inventory file path: "); + let _ = io::stdout().flush(); + let mut path = String::new(); + let stdin = io::stdin(); + stdin.read_line(&mut path).unwrap(); + let path = path.trim(); + + let mut file = std::fs::File::open(path).unwrap(); + let mut inventory = String::new(); + file.read_to_string(&mut inventory).unwrap(); + inventory +} + +fn execute_command(sess: &mut Session, command: &str) -> String { + let mut channel = sess.channel_session().unwrap(); + channel.exec(command).unwrap(); + + let mut output = String::new(); + channel.read_to_string(&mut output).unwrap(); + channel.wait_close().unwrap(); + output +}