From c50857f2100b3546232fac459d46106c0747da29 Mon Sep 17 00:00:00 2001 From: Trevor Merritt Date: Sun, 13 Apr 2025 12:10:52 -0400 Subject: [PATCH] Initial commit --- Cargo.lock | 410 +++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 10 ++ belleville.txt | 7 + lindsay.txt | 7 + src/main.rs | 214 ++++++++++++++++++++++++++ 5 files changed, 648 insertions(+) create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 belleville.txt create mode 100644 lindsay.txt create mode 100644 src/main.rs diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..79f9fbd --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,410 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "aho-corasick" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916" +dependencies = [ + "memchr", +] + +[[package]] +name = "ansi_term" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d52a9bb7ec0cf484c551830a7ce27bd20d67eac647e1befb56b0be4ee39a55d2" +dependencies = [ + "winapi", +] + +[[package]] +name = "anstream" +version = "0.6.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8acc5369981196006228e28809f761875c0327210a891e941f4c683b3a99529b" +dependencies = [ + "anstyle", + "anstyle-parse", + "anstyle-query", + "anstyle-wincon", + "colorchoice", + "is_terminal_polyfill", + "utf8parse", +] + +[[package]] +name = "anstyle" +version = "1.0.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "55cc3b69f167a1ef2e161439aa98aed94e6028e5f9a59be9a6ffb47aef1651f9" + +[[package]] +name = "anstyle-parse" +version = "0.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3b2d16507662817a6a20a9ea92df6652ee4f94f914589377d69f3b21bc5798a9" +dependencies = [ + "utf8parse", +] + +[[package]] +name = "anstyle-query" +version = "1.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "79947af37f4177cfead1110013d678905c37501914fba0efea834c3fe9a8d60c" +dependencies = [ + "windows-sys", +] + +[[package]] +name = "anstyle-wincon" +version = "3.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ca3534e77181a9cc07539ad51f2141fe32f6c3ffd4df76db8ad92346b003ae4e" +dependencies = [ + "anstyle", + "once_cell", + "windows-sys", +] + +[[package]] +name = "clap" +version = "4.5.36" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2df961d8c8a0d08aa9945718ccf584145eee3f3aa06cddbeac12933781102e04" +dependencies = [ + "clap_builder", + "clap_derive", +] + +[[package]] +name = "clap_builder" +version = "4.5.36" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "132dbda40fb6753878316a489d5a1242a8ef2f0d9e47ba01c951ea8aa7d013a5" +dependencies = [ + "anstream", + "anstyle", + "clap_lex", + "strsim", +] + +[[package]] +name = "clap_derive" +version = "4.5.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09176aae279615badda0765c0c0b3f6ed53f4709118af73cf4655d85d1530cd7" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "clap_lex" +version = "0.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f46ad14479a25103f283c0f10005961cf086d8dc42205bb44c46ac563475dca6" + +[[package]] +name = "colorchoice" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b63caa9aa9397e2d9480a9b13673856c78d8ac123288526c37d7839f2a86990" + +[[package]] +name = "env_filter" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "186e05a59d4c50738528153b83b0b0194d3a29507dfec16eccd4b342903397d0" +dependencies = [ + "log", + "regex", +] + +[[package]] +name = "env_logger" +version = "0.11.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "13c863f0904021b108aa8b2f55046443e6b1ebde8fd4a15c399893aae4fa069f" +dependencies = [ + "anstream", + "anstyle", + "env_filter", + "jiff", + "log", +] + +[[package]] +name = "heck" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" + +[[package]] +name = "is_terminal_polyfill" +version = "1.70.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf" + +[[package]] +name = "jiff" +version = "0.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1f33145a5cbea837164362c7bd596106eb7c5198f97d1ba6f6ebb3223952e488" +dependencies = [ + "jiff-static", + "log", + "portable-atomic", + "portable-atomic-util", + "serde", +] + +[[package]] +name = "jiff-static" +version = "0.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "43ce13c40ec6956157a3635d97a1ee2df323b263f09ea14165131289cb0f5c19" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "log" +version = "0.4.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "13dc2df351e3202783a1fe0d44375f7295ffb4049267b0f3018346dc122a1d94" + +[[package]] +name = "memchr" +version = "2.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" + +[[package]] +name = "once_cell" +version = "1.21.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "42f5e15c9953c5e4ccceeb2e7382a716482c34515315f7b03532b8b4e8393d2d" + +[[package]] +name = "portable-atomic" +version = "1.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "350e9b48cbc6b0e028b0473b114454c6316e57336ee184ceab6e53f72c178b3e" + +[[package]] +name = "portable-atomic-util" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d8a2f0d8d040d7848a709caf78912debcc3f33ee4b3cac47d73d1e1069e83507" +dependencies = [ + "portable-atomic", +] + +[[package]] +name = "pp" +version = "0.1.0" +dependencies = [ + "ansi_term", + "clap", + "env_logger", + "log", +] + +[[package]] +name = "proc-macro2" +version = "1.0.94" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a31971752e70b8b2686d7e46ec17fb38dad4051d94024c88df49b667caea9c84" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.40" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1885c039570dc00dcb4ff087a89e185fd56bae234ddc7f056a945bf36467248d" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "regex" +version = "1.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b544ef1b4eac5dc2db33ea63606ae9ffcfac26c1416a2806ae0bf5f56b201191" +dependencies = [ + "aho-corasick", + "memchr", + "regex-automata", + "regex-syntax", +] + +[[package]] +name = "regex-automata" +version = "0.4.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "809e8dc61f6de73b46c85f4c96486310fe304c434cfa43669d7b40f711150908" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c" + +[[package]] +name = "serde" +version = "1.0.219" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5f0e2c6ed6606019b4e29e69dbaba95b11854410e5347d525002456dbbb786b6" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.219" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b0276cf7f2c73365f7157c8123c21cd9a50fbbd844757af28ca1f5925fc2a00" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "strsim" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" + +[[package]] +name = "syn" +version = "2.0.100" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b09a44accad81e1ba1cd74a32461ba89dee89095ba17b32f5d03683b1b1fc2a0" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "unicode-ident" +version = "1.0.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5a5f39404a5da50712a4c1eecf25e90dd62b613502b7e925fd4e4d19b5c96512" + +[[package]] +name = "utf8parse" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" + +[[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" + +[[package]] +name = "windows-sys" +version = "0.59.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e38bc4d79ed67fd075bcc251a1c39b32a1776bbe92e5bef1f0bf1f8c531853b" +dependencies = [ + "windows-targets", +] + +[[package]] +name = "windows-targets" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" +dependencies = [ + "windows_aarch64_gnullvm", + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_gnullvm", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" + +[[package]] +name = "windows_i686_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b" + +[[package]] +name = "windows_i686_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" + +[[package]] +name = "windows_i686_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..a9511ba --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "pp" +version = "0.1.0" +edition = "2024" + +[dependencies] +env_logger = "0.11" +log = "0.4" +ansi_term = "0.12" +clap = { version = "4.5.36", features = ["derive"] } diff --git a/belleville.txt b/belleville.txt new file mode 100644 index 0000000..5842b7b --- /dev/null +++ b/belleville.txt @@ -0,0 +1,7 @@ +10.3.100.1 +10.13.31.1 +10.13.32.1 +10.11.21.1 +10.12.22.1 +8.8.8.8 +1.1.1.1 diff --git a/lindsay.txt b/lindsay.txt new file mode 100644 index 0000000..2ee2b63 --- /dev/null +++ b/lindsay.txt @@ -0,0 +1,7 @@ +10.2.100.1 +10.11.21.1 +10.12.22.1 +10.13.31.1 +10.13.32.1 +8.8.8.8 +1.1.1.1 diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..bef7ea9 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,214 @@ +use std::fs::File; +use std::net::Ipv4Addr; +use std::path::{Path, PathBuf}; +use std::process::Command; +use std::str::FromStr; +use std::string::ToString; +use std::sync::mpsc; +use std::sync::mpsc::Sender; +use std::{io, thread}; +use std::io::BufRead; +use std::time::{Duration, SystemTime}; +use ansi_term::Colour::{Green, Red}; +use clap::Parser; +use log::{debug, error, log_enabled, info, Level}; +#[derive(Clone)] +struct HistoricHost { + pub name: String, + pub address: Ipv4Addr, + pub history: Vec, + pub alive: bool +} +#[derive(Clone, Debug)] +struct PingResult { + pub address: Ipv4Addr, + pub rtt: u128 +} + +impl HistoricHost { + pub fn dump_state(&self) { + let working_history = self.history.clone(); + if working_history.len() == 0 { + println!("No Results for {} yet.", self.name); + return; + } + + let num_results = working_history.len(); + let mut running_total = 0; + for result in working_history { + running_total += result; + } + let average_ping_time = running_total.div_ceil(num_results as u128); + println!("Counted {} results, total time {} with average {}", + num_results, running_total, average_ping_time + ); + } + + pub fn get_ping_time_for(target: Ipv4Addr) -> (i32, Duration) { + let start_time = SystemTime::now(); + let result = Command::new("/usr/bin/ping").arg(target.to_string()) + .arg("-c 1") + .arg("-4") + .arg("-w 10") + .arg("-W 1") + .output().unwrap().status.code().unwrap_or(255); + let ping_duration = SystemTime::now().duration_since(start_time).unwrap_or(Duration::from_secs(600)); + (result, ping_duration) + } + + pub fn get_ping_time(&self) -> (i32, Duration) { + HistoricHost::get_ping_time_for(self.address) + } + + pub fn ping(&mut self) { + let (result, ping_duration) = self.get_ping_time(); + match result { + 0 => { + self.history.push(ping_duration.as_millis()) + }, + 1 => { + println!("Error ping to {}", self.name); + }, + _ => { + println!("General Ping Error."); + } + } + } +} + +#[derive(Parser, Debug)] +#[command(version, about, long_about = None)] +struct PPArgs { + pub targets: Option +} + + +// The output is wrapped in a Result to allow matching on errors. +// Returns an Iterator to the Reader of the lines of the file. +fn read_lines

(filename: P) -> io::Result>> + where P: AsRef, { + let file = File::open(filename)?; + Ok(io::BufReader::new(file).lines()) +} + +fn main() { + + env_logger::init(); + + let args = PPArgs::parse(); + + let targets = match args.targets { + Some(file) => { + println!("Loading hosts from {}", file.to_str().unwrap_or("no file.")); + let mut working = vec![]; + if let Ok(lines) = read_lines(file) { + // Consumes the iterator, returns an (Optional) String + for line in lines.map_while(Result::ok) { + println!("{}", line); + working.push(line.to_string()) + } + } + working.to_vec() + } + None => { + println!("Default Hosts used."); + vec!["127.0.0.1".to_string(), "1.1.1.1".to_string(), "8.8.8.8".to_string()] + } + }; + + let (sender, receiver) = mpsc::channel::(); + + // sender gets cloned and sent to each pinger + + let mut hosts = vec![]; + + info!("Building hosts list..."); + // build hosts list... + for target in targets { + hosts.push( + HistoricHost { + name: target.to_string(), + address: Ipv4Addr::from_str(&target).unwrap_or(Ipv4Addr::new(127,0,0,1)), + history: vec![], + alive: false + } + ) + } + info!("Completed building with {} hosts.", hosts.len()); + + info!("Starting Manager Thread"); + // Manager Thread + let my_hosts = hosts.clone(); + let _ = thread::spawn(move || { + loop { + println!("\x1b[2J --------------"); + for current_target in my_hosts.clone() { + // debug!("[MT] Preparing to send off ping request to {}", current_target.address.to_string()); + let start_time = SystemTime::now(); + + let result = Command::new("/usr/bin/ping").arg(current_target.address.to_string()) + .arg("-c 1") + .arg("-4") + .arg("-w 10") + .arg("-W 1") + .output().unwrap().status.code().unwrap_or(255); + let ping_duration = SystemTime::now().duration_since(start_time).unwrap_or(Duration::from_secs(600)); + let to_send = if result == 0 { + PingResult { address: current_target.address, rtt: ping_duration.as_millis() } + } else { + PingResult { address: current_target.address, rtt: u128::MAX } + }; + let my_sender = sender.clone(); + let _ = my_sender.send(to_send); + } + thread::sleep(Duration::from_secs(2)); + } + }); + + info!("Manager Thread Started"); + + info!("Starting Data Thread"); + // Data Thread + let _ = thread::spawn(move || { + debug!("[DT] STARTING WITH {} HOSTS", hosts.len()); + loop { + if let Ok(rx) = receiver.recv_timeout(Duration::from_micros(1000)) { + let addr = rx.clone().address; + let rtt = rx.clone().rtt; + if rtt == u128::MAX { + // error!("[DT] No reply from {}", addr); + println!("{} not responding", Red.paint(addr.to_string())); + + } else { + // debug!("[DT] Recieved a ping result from {} with time of {}", addr, rtt); + println!("{} responding.", Green.paint(addr.to_string())); + } + } + } + }); + info!("Data Thread Started"); + + info!("Keep Alive Thread Starting."); + let _ = thread::spawn(move || { + let mut counter = 0 ; + loop { + thread::sleep(Duration::from_millis(1000)); + counter += 1; + if counter % 1000 == 0 { + info!("Keep Alive Counter at {}", counter); + } + } + }).join(); + + // + // let _ = thread::spawn(move || { + // loop { + // println!("-- Status Start -- "); + // for host in &hosts.clone() { + // host.dump_state(); + // } + // println!("-- Status Done -- "); + // thread::sleep(Duration::from_secs(1)); + // } + // }).join(); +}