How to get a list of the machine's IP addresse

2019-04-07 03:21发布

问题:

Specifically, I am interested in a programmatic way for acquiring a list of IP addresses such as those returned by ifconfig.

Preferably, the solution would be cross-platform.

回答1:

Check out the pnet crate:

extern crate pnet;

use pnet::datalink;

fn main() {
    for iface in datalink::interfaces() {
        println!("{:?}", iface.ips);
    }
}