monitoring kit
add net selection
bendn 9 months ago
parent 13d779d · commit 4527393
-rw-r--r--network/src/net.rs25
-rw-r--r--network/src/ping.rs2
2 files changed, 19 insertions, 8 deletions
diff --git a/network/src/net.rs b/network/src/net.rs
index 3a8286d..ee67512 100644
--- a/network/src/net.rs
+++ b/network/src/net.rs
@@ -17,21 +17,32 @@ use termion::screen::IntoAlternateScreen;
use termion::{async_stdin, clear, cursor, style};
#[implicit_fn::implicit_fn]
fn main() -> Result<()> {
- let up = match std::env::args().nth(1) {
+ let file = match std::env::args().nth(1) {
None => {
- eprintln!("no args!");
+ let fs = std::fs::read_dir("/sys/class/net")?
+ .filter_map(Result::ok)
+ .filter_map(_.file_name().into_string().ok())
+ .collect::<Vec<_>>();
+ eprintln!("no network specified. must be one of {fs:?};\nnet [INTERFACE] [u/d]");
exit(1)
}
- Some("dl" | "download" | "d" | "down") => false,
- Some("up" | "upload") => true,
+ Some(x) => x,
+ };
+ let up = match std::env::args().nth(2) {
+ None => {
+ eprintln!("no args!\n;net [i] [up/down]");
+ exit(1)
+ }
+ Some("dl" | "download" | "r" | "d" | "down") => false,
+ Some("u" | "t" | "up" | "upload") => true,
Some(x) => {
- println!("{x} is not an arg, requires net");
+ println!("{x} is not an arg, requires up/down");
exit(1)
}
};
- let file = std::fs::read_dir("/sys/class/net/")?
+ let file = std::fs::read_dir("/sys/class/net")?
.filter_map(Result::ok)
- .find(|x| x.file_name().as_bytes().starts_with(b"lo").not())
+ .find(_.file_name().to_string_lossy().starts_with(&file))
.ok_or(anyhow!("no network"))?;
let pciid = read(file.path().join("device").join("uevent"))?
diff --git a/network/src/ping.rs b/network/src/ping.rs
index 1eb9ee9..3754529 100644
--- a/network/src/ping.rs
+++ b/network/src/ping.rs
@@ -24,7 +24,7 @@ use termion::raw::IntoRawMode;
use termion::screen::IntoAlternateScreen;
use termion::{async_stdin, clear, cursor, style};
fn ping(x: IpAddr) -> Result<u32> {
- match ping_rs::send_ping(&x, Duration::from_secs(60), b"", None) {
+ match ping_rs::send_ping(&x, Duration::from_secs(2), b"", None) {
Rok(x) => Ok(x.rtt),
Err(PingError::TimedOut | PingError::IoPending) => Ok(!0),
Err(x) => bail!("{x:?}"),