pub fn list_interfaces() -> Result<Vec<Interface>, IoError>Expand description
List all available network interfaces on the system.
Examples found in repository?
examples/list_interfaces.rs (line 9)
8fn main() {
9 let ifaces = list_interfaces().expect("failed to list interfaces");
10 println!("Found {} interface(s):\n", ifaces.len());
11 println!("{:<20} {:<8} {:<20} {:<8} {:?}", "Name", "Up", "MAC", "Loop", "IPs");
12 println!("{}", "-".repeat(80));
13 for iface in &ifaces {
14 let mac = iface.mac_address
15 .map(|m| format!("{:02x}:{:02x}:{:02x}:{:02x}:{:02x}:{:02x}", m[0], m[1], m[2], m[3], m[4], m[5]))
16 .unwrap_or_else(|| "-".into());
17 let ips: Vec<_> = iface.ips.iter().map(|ip| ip.to_string()).collect();
18 println!("{:<20} {:<8} {:<20} {:<8} {}",
19 iface.name,
20 if iface.is_up { "UP" } else { "DOWN" },
21 mac,
22 if iface.is_loopback { "LOOP" } else { "" },
23 ips.join(", "),
24 );
25 }
26}More examples
examples/cross_platform_dump.rs (line 19)
17fn main() -> Result<(), Box<dyn std::error::Error>> {
18 let ifname = std::env::args().nth(1).unwrap_or_else(|| {
19 list_interfaces()
20 .ok()
21 .and_then(|ifaces| ifaces.into_iter().find(|i| !i.is_loopback && i.is_up).map(|i| i.name))
22 .unwrap_or_else(|| "lo".into())
23 });
24
25 println!("Opening {} ...", ifname);
26 let mut rx = DefaultL2Reader::new(&ifname)?;
27 rx.set_timeout(Some(Duration::from_secs(1)))?;
28
29 println!("Listening on {} (Ctrl+C to stop)...\n", ifname);
30 for i in 0..10 {
31 match rx.recv() {
32 Ok(eth) => {
33 println!("#{}: {} → {} EtherType={:?} len={}",
34 i + 1,
35 mac_hex(ð.src_mac()),
36 mac_hex(ð.dst_mac()),
37 eth.ethertype(),
38 eth.payload().len() + 14,
39 );
40 }
41 Err(wireforge_io::IoError::Timeout) => continue,
42 Err(e) => { eprintln!("Error: {e}"); break; }
43 }
44 }
45 Ok(())
46}