From c3d3ea6062a415da75901d70709dc6d7d43ee618 Mon Sep 17 00:00:00 2001 From: luozijun Date: Thu, 14 Dec 2017 02:02:06 +0800 Subject: [PATCH] Add example for list interfaces --- examples/interfaces.rs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 examples/interfaces.rs diff --git a/examples/interfaces.rs b/examples/interfaces.rs new file mode 100644 index 0000000000..d436a23854 --- /dev/null +++ b/examples/interfaces.rs @@ -0,0 +1,22 @@ +extern crate nix; + + +#[allow(dead_code)] +fn pretty_print(ifaddr: &nix::ifaddrs::InterfaceAddress) { + println!("{}: flags={:X}<{}>", + ifaddr.interface_name, + ifaddr.flags.bits(), + format!("{:?}", ifaddr.flags).replace("IFF_", "").replace(" | ", ",")); + println!(" address : {}", ifaddr.address.map(|a| format!("{}", a)).unwrap_or("N/A".to_string())); + println!(" netmask : {}", ifaddr.netmask.map(|a| format!("{}", a)).unwrap_or("N/A".to_string())); + println!(" broadcast : {}", ifaddr.broadcast.map(|a| format!("{}", a)).unwrap_or("N/A".to_string())); + println!(" destination: {}", ifaddr.destination.map(|a| format!("{}", a)).unwrap_or("N/A".to_string())); +} + +fn main (){ + let addrs = nix::ifaddrs::getifaddrs().unwrap(); + for ifaddr in addrs { + println!("{:?}", ifaddr); + // pretty_print(&ifaddr); + } +}