From b1255c451e7b31fef1a6e066de6eaff36d1c6769 Mon Sep 17 00:00:00 2001 From: Gregory Ballantine Date: Thu, 24 Feb 2022 23:02:54 -0500 Subject: [PATCH] Created a nettest app with a ping test --- Cargo.toml | 5 +++++ src/nettest.rs | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 src/nettest.rs diff --git a/Cargo.toml b/Cargo.toml index 5ac0304..67495ba 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,6 +7,11 @@ edition = "2021" name = "hdtest" path = "src/hdtest.rs" +[[bin]] +name = "nettest" +path = "src/nettest.rs" + # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +clap = { version = "3.1.2", features = ["derive"] } diff --git a/src/nettest.rs b/src/nettest.rs new file mode 100644 index 0000000..9acbd4b --- /dev/null +++ b/src/nettest.rs @@ -0,0 +1,46 @@ +use clap::{Parser, Subcommand}; +use std::process; + +#[derive(Parser)] +#[clap(name = "Bit Goblin Network Tester", author, version, about = "Network testing app.", long_about = None)] +#[clap(propagate_version = true)] +struct Cli { + #[clap(subcommand)] + command: Commands, +} + +#[derive(Subcommand)] +enum Commands { + // ping subcommand + Ping { + #[clap(default_value_t = String::from("8.8.8.8"))] + host: String + }, +} + +fn main() { + let cli = Cli::parse(); + + // map subcommands back to the main command + match &cli.command { + Commands::Ping { host } => ping_host(host) + } +} + +// ping a host +fn ping_host(host: &str) { + println!("Pinging host {}", host); + + // run the ping command with 100 pings + let output = process::Command::new("ping") + .arg(host) + .arg("-c 100") + .output() + .expect("Failed to execute command"); + + // check that the command succeeded + assert!(output.status.success()); + + // print out the ping results from stdout + println!("{}", String::from_utf8_lossy(&output.stdout)); +}