Function pnet::datalink::interfaces

source ·
pub fn interfaces() -> Vec<NetworkInterface, Global>
Expand description

Get a list of available network interfaces for the current machine.

If you need the default network interface, you can choose the first one that is up, not loopback and has an IP. This is not guaranteed to work on each system but should work for basic packet sniffing:

use pnet_datalink::interfaces;

// Get a vector with all network interfaces found
let all_interfaces = interfaces();

// Search for the default interface - the one that is
// up, not loopback and has an IP.
let default_interface = all_interfaces
    .iter()
    .find(|e| e.is_up() && !e.is_loopback() && !e.ips.is_empty());

match default_interface {
    Some(interface) => println!("Found default interface with [{}].", interface.name),
    None => println!("Error while finding the default interface."),
}