Skip to content

Commit c3d3ea6

Browse files
author
luozijun
committed
Add example for list interfaces
1 parent 640e2fe commit c3d3ea6

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

examples/interfaces.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
extern crate nix;
2+
3+
4+
#[allow(dead_code)]
5+
fn pretty_print(ifaddr: &nix::ifaddrs::InterfaceAddress) {
6+
println!("{}: flags={:X}<{}>",
7+
ifaddr.interface_name,
8+
ifaddr.flags.bits(),
9+
format!("{:?}", ifaddr.flags).replace("IFF_", "").replace(" | ", ","));
10+
println!(" address : {}", ifaddr.address.map(|a| format!("{}", a)).unwrap_or("N/A".to_string()));
11+
println!(" netmask : {}", ifaddr.netmask.map(|a| format!("{}", a)).unwrap_or("N/A".to_string()));
12+
println!(" broadcast : {}", ifaddr.broadcast.map(|a| format!("{}", a)).unwrap_or("N/A".to_string()));
13+
println!(" destination: {}", ifaddr.destination.map(|a| format!("{}", a)).unwrap_or("N/A".to_string()));
14+
}
15+
16+
fn main (){
17+
let addrs = nix::ifaddrs::getifaddrs().unwrap();
18+
for ifaddr in addrs {
19+
println!("{:?}", ifaddr);
20+
// pretty_print(&ifaddr);
21+
}
22+
}

0 commit comments

Comments
 (0)