From d642822709dfe0fcf69abb405c69b0c1581dc072 Mon Sep 17 00:00:00 2001 From: Trevor Merritt Date: Sun, 20 Apr 2025 19:48:31 -0400 Subject: [PATCH] parses a file passed to use as a list of targets --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/main.rs | 66 +++++++++++++++++++++++++++++++++++++++++--------- src/manager.rs | 8 +++--- 4 files changed, 60 insertions(+), 18 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 79f9fbd..84403a7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -210,7 +210,7 @@ dependencies = [ [[package]] name = "pp" -version = "0.1.0" +version = "0.1.1" dependencies = [ "ansi_term", "clap", diff --git a/Cargo.toml b/Cargo.toml index c1adc28..575a3d3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "pp" -version = "0.1.0" +version = "0.1.1" edition = "2024" [dependencies] diff --git a/src/main.rs b/src/main.rs index db4bfbf..05b26c7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,9 +1,12 @@ +use std::fs::File; +use std::io; +use std::io::BufRead; use std::net::Ipv4Addr; -use std::process::Command; +use std::path::{Path, PathBuf}; +use std::str::FromStr; use std::sync::mpsc; -use std::sync::mpsc::Sender; -use std::thread; use std::time::{Duration, SystemTime}; +use clap::Parser; use crate::manager::Manager; use crate::ping_result::PingResult; use crate::state::State; @@ -18,18 +21,57 @@ mod ping_request; const SECONDS_BETWEEN_DISPLAY: u32 = 1; const SECONDS_BETWEEN_PING: u32 = 2; +fn read_lines

(filename: P) -> io::Result>> + where P: AsRef, { + let file = File::open(filename)?; + Ok(io::BufReader::new(file).lines()) +} + +fn build_targets_from_file(filename: Option) -> Vec{ + let mut targets = vec![Ipv4Addr::new(127, 0, 0, 1), + Ipv4Addr::new(172, 24, 10, 1), + Ipv4Addr::new(1,1,1,1), + Ipv4Addr::new(8,8,8,8), + Ipv4Addr::new(216,121,247,231), + Ipv4Addr::new(216,234,202,122), + Ipv4Addr::new(24,143,184,98), + ]; + + if let Some(file) = filename { + println!("File is {:?}", file.to_str()); + if let Ok(lines) = read_lines(file) { + targets.clear(); + // Consumes the iterator, returns an (Optional) String + for line in lines.map_while(Result::ok) { + targets.push( + Ipv4Addr::from_str(&*line).unwrap() + ); + } + } + // targets = vec![]; + }; + targets +} + +/// Simple program to greet a person +#[derive(Parser, Debug)] +#[command(version, about, long_about = None)] +pub struct AppSettings { + /// File of list of hosts + #[arg(short, long)] + ping_host_file: Option, +} + fn main() { + // Get App Settings + let settings = AppSettings::parse(); + // channel to send requests to ping let (ping_response_sender, ping_response_listener) = mpsc::channel::(); - println!("Taxation is Theft"); + let file_to_read = settings.ping_host_file; - let targets = vec![Ipv4Addr::new(127, 0, 0, 1), - Ipv4Addr::new(172, 24, 10, 137), - Ipv4Addr::new(1,1,1,1), - Ipv4Addr::new(8,8,8,8), - Ipv4Addr::new(216,121,247,231) - ]; + let targets = build_targets_from_file(file_to_read); let mut state = State::default(); for current in &targets { @@ -66,7 +108,7 @@ fn main() { .expect("unable to figure out how long ago we displayed stuff"); if duration_since_last_loop.as_secs() > SECONDS_BETWEEN_DISPLAY as u64 { println!("DISPLAY LOOP"); - println!("------------"); + println!("Host \t\t | Alive \t | RTT \t\t"); for current_result in state.targets.clone() { let time_since_last_change = SystemTime::now().duration_since(current_result.last_alive_change).unwrap(); let target_string = if current_result.target.clone().to_string().len() > 8 { @@ -74,7 +116,7 @@ fn main() { } else { format!("{}\t", current_result.target) }; - println!("{}\t | {} \t | {}\t\t | Changed {}s ago", + println!("{}\t | {} \t | {}\t | Changed {}s ago", target_string, current_result.alive, current_result.last_rtt,time_since_last_change.as_secs() diff --git a/src/manager.rs b/src/manager.rs index 0696346..d121c54 100644 --- a/src/manager.rs +++ b/src/manager.rs @@ -9,8 +9,8 @@ use crate::SECONDS_BETWEEN_PING; pub struct Manager; -const WIN_PING_SUCCESS: u8 = 1; -const LIN_PING_SUCCESS: u8 = 0; +const WIN_PING_SUCCESS: i32 = 1; +const LIN_PING_SUCCESS: i32 = 0; impl Manager { pub fn spawn_manager_thread(targets: Vec, sender: Sender) { @@ -40,7 +40,7 @@ impl Manager { .arg("-w 10") .arg("-4") .output().unwrap().status.code().unwrap_or(255); - success = result == 1; + success = result == WIN_PING_SUCCESS as i32;; } #[cfg(any(target_os="linux"))] { result = Command::new("/usr/bin/ping").arg(request.target.to_string()) @@ -49,7 +49,7 @@ impl Manager { .arg("-w 10") .arg("-W 1") .output().unwrap().status.code().unwrap_or(255); - success = result == 0; + success = result == LIN_PING_SUCCESS as i32; } let ping_duration = SystemTime::now() .duration_since(start_time)