Created a nettest app with a ping test

This commit is contained in:
2022-02-24 23:02:54 -05:00
parent 04ebc2290f
commit b1255c451e
2 changed files with 51 additions and 0 deletions

46
src/nettest.rs Normal file
View File

@ -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));
}