parses a file passed to use as a list of targets
This commit is contained in:
parent
1742874c46
commit
d642822709
2
Cargo.lock
generated
2
Cargo.lock
generated
@ -210,7 +210,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "pp"
|
||||
version = "0.1.0"
|
||||
version = "0.1.1"
|
||||
dependencies = [
|
||||
"ansi_term",
|
||||
"clap",
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "pp"
|
||||
version = "0.1.0"
|
||||
version = "0.1.1"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
|
||||
66
src/main.rs
66
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<P>(filename: P) -> io::Result<io::Lines<io::BufReader<File>>>
|
||||
where P: AsRef<Path>, {
|
||||
let file = File::open(filename)?;
|
||||
Ok(io::BufReader::new(file).lines())
|
||||
}
|
||||
|
||||
fn build_targets_from_file(filename: Option<PathBuf>) -> Vec<Ipv4Addr>{
|
||||
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<PathBuf>,
|
||||
}
|
||||
|
||||
fn main() {
|
||||
// Get App Settings
|
||||
let settings = AppSettings::parse();
|
||||
|
||||
// channel to send requests to ping
|
||||
let (ping_response_sender, ping_response_listener) = mpsc::channel::<PingResult>();
|
||||
|
||||
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()
|
||||
|
||||
@ -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<Ipv4Addr>, sender: Sender<PingResult>) {
|
||||
@ -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)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user