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
extern crate regex; use std::process::Command; use std::net::{IpAddr, Ipv4Addr, Ipv6Addr}; use regex::Regex; pub fn get() -> Option<IpAddr> { let output = Command::new("ifconfig") .output() .expect("failed to execute `ifconfig`"); let stdout = String::from_utf8(output.stdout).unwrap(); let re = Regex::new(r#"(?m)^.*inet (addr:)?(([0-9]*\.){3}[0-9]*).*$"#).unwrap(); for cap in re.captures_iter(&stdout) { if let Some(host) = cap.at(2) { if host != "127.0.0.1" { if let Ok(addr) = host.parse::<Ipv4Addr>() { return Some(IpAddr::V4(addr)) } if let Ok(addr) = host.parse::<Ipv6Addr>() { return Some(IpAddr::V6(addr)) } } } } None } #[cfg(test)] mod tests { #[test] fn it_works() { } }