1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
use std::process::Command; use std::net::{IpAddr, Ipv4Addr, Ipv6Addr}; pub fn get() -> Option<IpAddr> { let output = Command::new("hostname") .args(&["-I"]) .output() .expect("failed to execute `hostname`"); let stdout = String::from_utf8(output.stdout).unwrap(); let ips: Vec<&str> = stdout.trim().split(" ").collect::<Vec<&str>>(); let first = ips.first(); match first{ Some(first) => { if !first.is_empty(){ if let Ok(addr) = first.parse::<Ipv4Addr>() { return Some(IpAddr::V4(addr)) } else if let Ok(addr) = first.parse::<Ipv6Addr>() { return Some(IpAddr::V6(addr)) } else{ None } }else{ None } } None => None } } #[cfg(test)] mod tests { use super::*; #[test] fn it_works() { let res = get(); println!("res: {:?}", res); assert!(res.is_some()); } }