Created a nettest app with a ping test
This commit is contained in:
46
src/nettest.rs
Normal file
46
src/nettest.rs
Normal 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));
|
||||
}
|
Reference in New Issue
Block a user