default-net
provides a cross-platform API for network interface and gateway.
Add default-net
to your dependencies
toml:Cargo.toml
[dependencies]
default-net = "0.8.1"
The following example retrieves and displays information about the default network interface. ```rust use default_net;
fn main(){ match defaultnet::getdefaultinterface() { Ok(defaultinterface) => { println!("Default Interface"); println!("\tIndex: {}", defaultinterface.index); println!("\tName: {}", defaultinterface.name); println!("\tDescription: {:?}", defaultinterface.description); if let Some(macaddr) = defaultinterface.macaddr { println!("\tMAC: {}", macaddr); }else{ println!("\tMAC: (Failed to get mac address)"); } println!("\tIPv4: {:?}", defaultinterface.ipv4); println!("\tIPv6: {:?}", defaultinterface.ipv6); if let Some(gateway) = defaultinterface.gateway { println!("Default Gateway"); println!("\tMAC: {}", gateway.macaddr); println!("\tIP: {}", gateway.ipaddr); }else { println!("Default Gateway: (Not found)"); } }, Err(e) => { println!("{}", e); }, } } ```
For more details, see examples or doc.