How to Determine Internet Network Interface in Java

后端 未结 4 805
再見小時候
再見小時候 2020-12-05 08:10

How do you determine which network interface is connected to the internet using Java? For example, I run

InetAddress.getLocalHost().getHostAddress();
         


        
4条回答
  •  無奈伤痛
    2020-12-05 08:46

    I was having the same problem, and came up with this solution (not as thorough as the answer, but I needed something that didn't try to connect to the internet.

    Based on known OUI for mac addresses: http://standards-oui.ieee.org/oui.txt

    I made a quick check (could be optimized with binary logic)

    private static boolean isVmwareMac(byte[] mac) {
        byte invalidMacs[][] = {
                {0x00, 0x05, 0x69},             //VMWare
                {0x00, 0x1C, 0x14},             //VMWare
                {0x00, 0x0C, 0x29},             //VMWare
                {0x00, 0x50, 0x56}              //VMWare
        };
    
        for (byte[] invalid: invalidMacs){
            if (invalid[0] == mac[0] && invalid[1] == mac[1] && invalid[2] == mac[2])
                return true;
        }
    
        return false;
    }
    

提交回复
热议问题