Skip to content

Commit

Permalink
Add example for list interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
luozijun committed Dec 15, 2017
1 parent 640e2fe commit c3d3ea6
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions examples/interfaces.rs
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit c3d3ea6

Please sign in to comment.