Created a nettest app with a ping test
This commit is contained in:
parent
04ebc2290f
commit
b1255c451e
@ -7,6 +7,11 @@ edition = "2021"
|
|||||||
name = "hdtest"
|
name = "hdtest"
|
||||||
path = "src/hdtest.rs"
|
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
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
clap = { version = "3.1.2", features = ["derive"] }
|
||||||
|
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));
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user