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 ShowNetworkInterfacesjava

=====================================this

name:eth0 (eth0) index: 2 addresses:.net

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

/10.0.0.29;get

/10.0.0.28;io

 

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

/10.0.0.29import

/10.0.0.28network

=====================================im

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

相關文章
相關標籤/搜索