Java遍历所有网卡打印对应IP

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

public class Test {

    /**
     * @param args
     */
    public static void main(String[] args) {
        try {
            Enumeration<NetworkInterface> interfaceList = NetworkInterface.getNetworkInterfaces();
            if (interfaceList == null) {
                System.out.println("--No interface found--");
            } else {
                while (interfaceList.hasMoreElements()) {
                    NetworkInterface iface = interfaceList.nextElement();
                    System.out.println("Interface " + iface.getName() + ":");
                    Enumeration<InetAddress> addrList = iface.getInetAddresses();
                    while (addrList.hasMoreElements()) {
                        InetAddress address = addrList.nextElement();
                        if (address instanceof Inet6Address) {
                            System.out.println("\t v6:" + address.getHostAddress());
                        } else if(address instanceof Inet4Address) {
                            System.out.println("\t v4:" + address.getHostAddress());
                        } else {
                            System.out.println("\t " + address.getHostAddress());
                        }
                    }
                }
            }
        } catch (SocketException e) {
            System.out.println("Error getting network interfaces:" + e.getMessage());
            e.printStackTrace();
        }
    }

}

结果

Interface lo:
     v6:0:0:0:0:0:0:0:1
     v4:127.0.0.1
Interface net0:
Interface net1:
Interface net2:
Interface ppp0:
Interface eth0:
Interface eth1:
Interface eth2:
Interface ppp1:
Interface net3:
Interface net4:
Interface eth3:
Interface eth4:
Interface net5:
Interface eth5:
Interface net6:
Interface net7:
Interface net8:
Interface net9:
     v6:fe80:0:0:0:0:100:7f:fffe%19
Interface eth6:
     v6:fe80:0:0:0:995d:ea5a:42c2:75a6%20
Interface net10:
Interface eth7:
     v6:fe80:0:0:0:c61:34b5:e716:b2c0%22
Interface net11:
     v6:fe80:0:0:0:11d7:823:d6e6:4d1d%23
     v4:172.16.119.22
Interface eth8:
Interface net12:
Interface net13:
     v6:fe80:0:0:0:818f:a275:9fd5:6fde%26
Interface net14:
     v6:fe80:0:0:0:fcad:518c:fb2a:8d02%27
Interface net15:
     v6:fe80:0:0:0:0:5efe:c0a8:f301%28
Interface net16:
Interface eth9:
     v6:fe80:0:0:0:8c7f:6874:40c6:c23e%30
     v4:192.168.243.1
Interface eth10:
     v6:fe80:0:0:0:c12b:9cdb:edc3:1dae%31
     v4:192.168.75.1
Interface net17:
Interface net18:
     v6:fe80:0:0:0:0:5efe:ac10:7716%33
Interface eth11:
Interface net19:
     v6:fe80:0:0:0:0:5efe:c0a8:4b01%35
Interface eth12:
Interface eth13:
Interface net20:
Interface net21:
Interface eth14:
     v6:fe80:0:0:0:d9f:7d1b:e2e3:689b%40
Interface net22:
Interface net23:
Interface net24:
Interface eth15:
Interface eth16:
Interface net25:
Interface net26:
Interface eth17:
Interface eth18:
Interface eth19:
Interface eth20:
Interface eth21:
Interface eth22:
Interface eth23:
Interface net27:
Interface net28:
Interface net29:
Interface net30:
Interface net31:
Interface net32:
Interface net33:
Interface net34:

 

posted @ 2016-12-05 16:52  carlo-z  阅读(876)  评论(0编辑  收藏  举报