From eb70637c14ea251bba7d8c11b407a8af18ad054a Mon Sep 17 00:00:00 2001 From: deadjakk Date: Sun, 21 Feb 2021 20:06:03 -0600 Subject: [PATCH] added uptime src file --- README.md | 16 +++++++++------- src/main.rs | 2 +- src/oneliners.rs | 22 ++++++++++++++++++++++ 3 files changed, 32 insertions(+), 8 deletions(-) create mode 100644 src/oneliners.rs diff --git a/README.md b/README.md index 66d9f42..fa4f6b9 100644 --- a/README.md +++ b/README.md @@ -25,18 +25,20 @@ USAGE: rice_stats [FLAGS] [OPTIONS] FLAGS: - -a, --apt This flag will cause the program to run `apt update -y && apt upgrade` updating the apt repository - but NOT the packages and then output the parsed number of available updates REQUIRES SUID bit to be - set on this binary via: sudo chown && sudo chmod u+s or alternatively - this binary can be run as an elevated user - -h, --help Prints help information - -u, --usage Return the current network usage for the primary interface - -V, --version Prints version information + -a, --apt This flag will cause the program to run `apt update -y && apt upgrade` updating the apt + repository but NOT the packages and then output the parsed number of available updates REQUIRES + SUID bit to be set on this binary via: sudo chown && sudo chmod u+s + or alternatively this binary can be run as an elevated user + -h, --help Prints help information + -s, --sys-uptime Return the system uptime in days + -u, --usage Return the current network usage for the primary interface + -V, --version Prints version information OPTIONS: -i, --interface Override the interface that is used for several commands. If this is not provided an attempt will be made to find the primary interface based on the presence of an ip and then traffic rate + ``` ## Usage diff --git a/src/main.rs b/src/main.rs index 762b1ff..f4c3aa2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -53,7 +53,7 @@ fn main() { if args.sys_uptime == true { if let Err(e) = get_uptime(&mut output_line){ - eprintln!("Error received attempting show_updates: {}",e); + eprintln!("Error received attempting to get uptime: {}",e); } } print!("{}",output_line); diff --git a/src/oneliners.rs b/src/oneliners.rs new file mode 100644 index 0000000..ca0e443 --- /dev/null +++ b/src/oneliners.rs @@ -0,0 +1,22 @@ +pub use std::{fmt,error::Error,process::Command}; +use rice_stats::{run_command}; + +pub fn get_uptime (output_line: &mut String) -> Result<(),Box> { + let mut command_to_parse = String::new(); + run_command("uptime", &mut command_to_parse)?; + + let pattern = regex::Regex::new("\\d+? days")?; + let p_match = pattern.captures(&command_to_parse); + match p_match { + Some(v) => { + output_line.push_str("Uptime: "); + output_line.push_str(&v[0]); + output_line.push_str(" "); + return Ok(()); + }, + None => { + return Ok(()); + } + } +} + -- 2.45.2