Skip to content

Instantly share code, notes, and snippets.

@STashakkori
Created September 25, 2023 19:03
Show Gist options
  • Save STashakkori/b25f4a8d3e11ce929b891f5f040227b7 to your computer and use it in GitHub Desktop.
Save STashakkori/b25f4a8d3e11ce929b891f5f040227b7 to your computer and use it in GitHub Desktop.
Hardware querying using available packages in Linux
This excerpt from Salvum shows how to query hardware on Ubuntu using packages either provided or available via APT.
Binaries were packaged and shipped with Salvum in ext/hw/<program_name> and then passed the appropriate arguments.
This is not how you'll invoke and use in your case but gives an idea of how its done.
/*********************************** hw ***********************************/
pub struct Hw { event: Event }
impl Eventable for Hw {
fn on_run(&self, args: Vec<String>) -> String {
if main_info::is_bg() || main_info::get_file_redirect() {
print::print_custom("Hw currently does not support background or file redirection.\n\nComing soon.\n","orange");
return String::from("");
}
if args.len() < 1 || args.len() > 1 { return self.event.usage.clone(); }
if args[0].eq("serial") { // Print SCSI information
let output = match Command::new("ext/hw/lser").output() {
Ok(o) => o,
Err(err) => {
print::print_custom(&format!("Lser command failed. Error : {}",err),"orange");
return String::from("");
}
};
let out = String::from_utf8_lossy(&output.stdout);
print::print_custom(&out,"magenta");
}
if args[0].eq("all"){ // Print a hardware information summary
match run_console_command(Command::new("ext/hw/neofetch").arg("--off")) {
Ok(_) => (),
Err(err) => {
print::println(&format!("Neofetch command failed. Error : {}",err));
return String::from("");
}
};
match run_console_command(Command::new("inxi").args(vec!["-Fxz","-v8","-c","11"])) {
Ok(_) => (),
Err(err) => {
print::print_custom(&format!("inxi ommand failed. Error : {}",err),"orange");
return String::from("");
}
};
print::print_custom_uncapped("{}","gold");
match run_console_command(Command::new("ext/hw/lshw").arg("-short")) {
Ok(_) => (),
Err(err) => {
print::print_custom(&format!("lshw command failed. Error : {}",err),"orange");
return String::from("");
}
};
print::print_custom("","reset");
}
if args[0].eq("cpu") { // Print CPU information
print::print_custom_uncapped("","orange");
match run_console_command(&mut Command::new("ext/hw/lscpu")) {
Ok(_) => (),
Err(err) => {
print::print_custom(&format!("lscpu command failed. Error : {}",err),"orange");
return String::from("");
}
};
print::print_custom_uncapped("","grey");
match run_console_command(&mut Command::new("cpu-info")) {
Ok(_) => (),
Err(err) => {
print::print_custom(&format!("cpu-info command failed. Error : {}",err),"orange");
return String::from("");
}
};
print::print_custom_uncapped("","gold");
match run_console_command(&mut Command::new("ext/hw/procinfo")) {
Ok(_) => (),
Err(err) => {
print::print_custom(&format!("procinfo command failed. Error : {}",err),"orange");
return String::from("");
}
};
let output = match Command::new("dmidecode").args(vec!["-t","processor"]).output() {
Ok(out) => out,
Err(err) => {
print::print_custom(&format!("dmidecode command failed. Error : {}",err),"orange");
return String::from("");
}
};
let out = String::from_utf8_lossy(&output.stdout);
print::print_custom(&out,"brightgreen");
return String::from("");
}
if args[0].eq("cache") { // Print Cache information
print::print_custom_uncapped("","gold");
match run_console_command(&mut Command::new("cache-info")) {
Ok(_) => (),
Err(err) => {
print::print_custom(&format!("cache-info command failed. Error : {}",err),"orange");
return String::from("");
}
};
let output = match Command::new("dmidecode").args(vec!["-t","cache"]).output() {
Ok(out) => out,
Err(err) => {
print::print_custom(&format!("Command failed. Error : {}",err),"orange");
return String::from("");
}
};
let out = String::from_utf8_lossy(&output.stdout);
print::print_custom(&out,"brightgreen");
}
if args[0].eq("mem") || args[0].eq("memory") { // Print Memory information
match run_console_command(&mut Command::new("ext/hw/mprober").args(vec!["memory","-l"])) {
Ok(_) => (),
Err(err) => {
print::print_custom(&format!("Command failed. Error : {}",err),"orange");
return String::from("");
}
};
print::print_custom_uncapped("","grey");
match run_console_command(&mut Command::new("ext/hw/free").args(vec!["-htl"])) {
Ok(_) => (),
Err(err) => {
print::print_custom(&format!("Command failed. Error : {}",err),"orange");
return String::from("");
}
};
print::print_custom_uncapped("","gold");
match run_console_command(&mut Command::new("ext/hw/lshw").args(vec!["-short", "-C", "memory"])) {
Ok(_) => (),
Err(err) => {
print::print_custom(&format!("Command failed. Error : {}",err),"orange");
return String::from("");
}
};
print::print_custom_uncapped("","orange");
match run_console_command(&mut Command::new("ext/hw/lsmem").args(vec!["--summary=always"])) {
Ok(_) => (),
Err(err) => {
print::print_custom(&format!("Command failed. Error : {}",err),"orange");
return String::from("");
}
};
let output = match Command::new("dmidecode").args(vec!["-t","memory"]).output() {
Ok(out) => out,
Err(err) => {
print::print_custom(&format!("Command failed. Error : {}",err),"orange");
return String::from("");
}
};
let out = String::from_utf8_lossy(&output.stdout);
print::print_custom(&out,"brightgreen");
}
if args[0].eq("disk") { // Print CPU information
print::print_custom_uncapped("","grey");
match run_console_command(&mut Command::new("ext/hw/lshw").args(vec!["-short", "-C" ,"memory"])) {
Ok(_) => (),
Err(err) => {
print::print_custom(&format!("Command failed. Error : {}",err),"orange");
return String::from("");
}
};
print::print_custom_uncapped("","gold");
match run_console_command(&mut Command::new("ext/hw/lsblk").args(vec!["-fpz","-o","name,uuid,fstype,fsavail,fsuse%,mountpoint,zoned"])) {
Ok(_) => (),
Err(err) => {
print::print_custom(&format!("Command failed. Error : {}",err),"orange");
return String::from("");
}
};
print::print_custom_uncapped("","neongreen");
match run_console_command(&mut Command::new("ext/hw/df").args(vec!["-Th"])) {
Ok(_) => (),
Err(err) => {
print::print_custom(&format!("Command failed. Error : {}",err),"orange");
return String::from("");
}
};
print::print_custom("","reset");
/*
match run_console_command(&mut Command::new("ext/hw/mprober").args(vec!["volume","-l"])) {
Ok(_) => (),
Err(err) => {
print::print_custom(&format!("Command failed. Error : {}",err),"orange");
return String::from("");
}
};
*/
}
if args[0].eq("pci") { // Print PCI information
print::print_custom_uncapped("","orange");
match run_console_command(&mut Command::new("lspci")) {
Ok(_) => (),
Err(err) => {
print::print_custom(&format!("Command failed. Error : {}",err),"orange");
return String::from("");
}
};
print::print_custom("---------------------------------------------------------------\n","grey");
print::print_custom_uncapped("","gold");
match run_console_command(&mut Command::new("lspci").arg("-t")) {
Ok(_) => (),
Err(err) => {
print::print_custom(&format!("Command failed. Error : {}",err),"orange");
return String::from("");
}
};
print::print_custom("---------------------------------------------------------------\n","grey");
print::print_custom_uncapped("","purple");
match run_console_command(&mut Command::new("dmidecode").args(vec!["-t","slot"])) {
Ok(_) => (),
Err(err) => {
print::print_custom(&format!("Command failed. Error : {}",err),"orange");
return String::from("");
}
};
print::print_custom("","reset");
}
if args[0].eq("scsi") { // Print SCSI information
print::print_custom_uncapped("","gold");
let output = match Command::new("ext/hw/lsscsi").arg("-ldis").output() {
Ok(o) => o,
Err(err) => {
print::print_custom(&format!("Command failed. Error : {}",err),"orange");
return String::from("");
}
};
let out = String::from_utf8_lossy(&output.stdout);
print::print_custom(&out,"brightgreen");
}
if args[0].eq("usb") { // Print USB information
print::print_custom_uncapped("","gold");
match run_console_command(&mut Command::new("lsusb")) {
Ok(_) => (),
Err(err) => {
print::print_custom(&format!("Command failed. Error : {}",err),"orange");
return String::from("");
}
};
print::print_custom("--------------------------------------------------------------------\n","grey");
let output = match Command::new("lsusb").arg("-t").output() {
Ok(o) => o,
Err(err) => {
print::print_custom(&format!("Command failed. Error : {}",err),"orange");
return String::from("");
}
};
let out = String::from_utf8_lossy(&output.stdout);
print::print_custom(&out,"brightgreen");
}
return String::from("");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment