From ef1d3dde9c0624121c0ee62c347ed2ef0475dd8c Mon Sep 17 00:00:00 2001 From: Trevor Merritt Date: Mon, 14 Apr 2025 09:37:28 -0400 Subject: [PATCH] works at console for linux --- belleville.txt | 4 ++-- lindsay.txt | 4 ++-- src/main.rs | 64 +++++++++++++++++++++++++------------------------- 3 files changed, 36 insertions(+), 36 deletions(-) diff --git a/belleville.txt b/belleville.txt index 5842b7b..b64d39e 100644 --- a/belleville.txt +++ b/belleville.txt @@ -1,6 +1,6 @@ 10.3.100.1 -10.13.31.1 -10.13.32.1 +10.11.31.3 +10.12.32.1 10.11.21.1 10.12.22.1 8.8.8.8 diff --git a/lindsay.txt b/lindsay.txt index 2ee2b63..a407568 100644 --- a/lindsay.txt +++ b/lindsay.txt @@ -1,7 +1,7 @@ 10.2.100.1 10.11.21.1 10.12.22.1 -10.13.31.1 -10.13.32.1 +10.11.31.3 +10.12.32.1 8.8.8.8 1.1.1.1 diff --git a/src/main.rs b/src/main.rs index bef7ea9..4e14319 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,17 +12,19 @@ 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 + pub alive: bool, } + #[derive(Clone, Debug)] struct PingResult { pub address: Ipv4Addr, - pub rtt: u128 + pub rtt: u128, } impl HistoricHost { @@ -40,7 +42,7 @@ impl HistoricHost { } 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 + num_results, running_total, average_ping_time ); } @@ -65,10 +67,10 @@ impl HistoricHost { match result { 0 => { self.history.push(ping_duration.as_millis()) - }, + } 1 => { println!("Error ping to {}", self.name); - }, + } _ => { println!("General Ping Error."); } @@ -79,7 +81,7 @@ impl HistoricHost { #[derive(Parser, Debug)] #[command(version, about, long_about = None)] struct PPArgs { - pub targets: Option + pub targets: Option, } @@ -92,7 +94,6 @@ fn read_lines

(filename: P) -> io::Result>> } fn main() { - env_logger::init(); let args = PPArgs::parse(); @@ -128,9 +129,9 @@ fn main() { hosts.push( HistoricHost { name: target.to_string(), - address: Ipv4Addr::from_str(&target).unwrap_or(Ipv4Addr::new(127,0,0,1)), + address: Ipv4Addr::from_str(&target).unwrap_or(Ipv4Addr::new(127, 0, 0, 1)), history: vec![], - alive: false + alive: false, } ) } @@ -142,25 +143,25 @@ fn main() { 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(); + 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); - } + 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)); } }); @@ -176,11 +177,10 @@ fn main() { 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())); - + // 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); + // debug!("[DT] Recieved a ping result from {} with time of {}", addr, rtt); println!("{} responding.", Green.paint(addr.to_string())); } } @@ -190,7 +190,7 @@ fn main() { info!("Keep Alive Thread Starting."); let _ = thread::spawn(move || { - let mut counter = 0 ; + let mut counter = 0; loop { thread::sleep(Duration::from_millis(1000)); counter += 1;