Java获取主机的网络接口和IP地址

 

import java.util.Enumeration;
import java.net.*;
import java.io.*;

public class ShowNetworkInterfaces {
    public static void main(String[] args) throws Exception {
        Enumeration interfaces = NetworkInterface.getNetworkInterfaces();
        while (interfaces.hasMoreElements()) {
            NetworkInterface intf = (NetworkInterface)interfaces.nextElement();
            System.out.println("=====================================");
            System.out.println(intf);
            // Enumerate InetAddresses of this network interface                                                                                                        
            Enumeration addresses = intf.getInetAddresses();
            while (addresses.hasMoreElements()) {
                InetAddress address = (InetAddress)addresses.nextElement();
                System.out.println(address);
            }
        }
    }
}

root@localhost :/home/James/mypro/Network/ShowNetworkInterface# java ShowNetworkInterfaces

=====================================

name:eth0 (eth0) index: 2 addresses:

/fe80:0:0:0:4637:e6ff:fe19:f274%2;

/10.0.0.29;

/10.0.0.28;

 

/fe80:0:0:0:4637:e6ff:fe19:f274%2

/10.0.0.29

/10.0.0.28

=====================================

name:lo (lo) index: 1 addresses:

/0:0:0:0:0:0:0:1%1;

/127.0.0.1;

 

/0:0:0:0:0:0:0:1%1

/127.0.0.1

你可能感兴趣的:(Java获取主机的网络接口和IP地址)