Example usage for java.net NetworkInterface getNetworkInterfaces

List of usage examples for java.net NetworkInterface getNetworkInterfaces

Introduction

In this page you can find the example usage for java.net NetworkInterface getNetworkInterfaces.

Prototype

public static Enumeration<NetworkInterface> getNetworkInterfaces() throws SocketException 

Source Link

Document

Returns an Enumeration of all the interfaces on this machine.

Usage

From source file:comikit.droidscript.DroidScriptServer.java

public static String[] getIpAddresses() {
    try {/*from   w w  w  .j a va 2  s .c  o m*/
        List<String> ipaddresses = new ArrayList<String>();
        Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
        while (interfaces.hasMoreElements()) {
            NetworkInterface interf = interfaces.nextElement();
            Enumeration<InetAddress> adresses = interf.getInetAddresses();
            while (adresses.hasMoreElements()) {
                InetAddress address = adresses.nextElement();
                if (!address.isLoopbackAddress()) {
                    ipaddresses.add(address.getHostAddress().toString());
                }
            }
        }

        if (0 < ipaddresses.size()) {
            return ipaddresses.toArray(new String[1]);
        }
    } catch (SocketException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:org.kuali.rice.core.api.util.RiceUtilities.java

/** * @return the current environment's IP address, taking into account the Internet connection to any of the available
*         machine's Network interfaces. Examples of the outputs can be in octatos or in IPV6 format.
*
*         fec0:0:0:9:213:e8ff:fef1:b717%4 siteLocal: true isLoopback: false isIPV6: true
*         ============================================ 130.212.150.216 <<<<<<<<<<<------------- This is the one we
*         want to grab so that we can. siteLocal: false address the DSP on the network. isLoopback: false isIPV6:
*         false ==> lo ============================================ 0:0:0:0:0:0:0:1%1 siteLocal: false isLoopback:
*         true isIPV6: true ============================================ 127.0.0.1 siteLocal: false isLoopback:
*         true isIPV6: false//from   ww  w  .  ja v a 2  s  .  c  om
*/
private static String getCurrentEnvironmentNetworkIp() {
    Enumeration<NetworkInterface> netInterfaces = null;
    try {
        netInterfaces = NetworkInterface.getNetworkInterfaces();
    } catch (SocketException e) {
        LOG.error("Somehow we have a socket error...", e);
        return "127.0.0.1";
    }

    while (netInterfaces.hasMoreElements()) {
        NetworkInterface ni = netInterfaces.nextElement();
        Enumeration<InetAddress> address = ni.getInetAddresses();
        while (address.hasMoreElements()) {
            InetAddress addr = address.nextElement();
            if (!addr.isLoopbackAddress() && !addr.isSiteLocalAddress()
                    && !(addr.getHostAddress().indexOf(":") > -1)) {
                return addr.getHostAddress();
            }
        }
    }
    try {
        return InetAddress.getLocalHost().getHostAddress();
    } catch (UnknownHostException e) {
        return "127.0.0.1";
    }
}

From source file:license.regist.ReadProjectInfo.java

private static boolean checkMac(Map<Integer, String> infoMap) throws SocketException {
    Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
    Base64 base64 = new Base64();
    int i = 12;/*from w ww . ja va2 s . c o m*/
    for (NetworkInterface ni : Collections.list(interfaces)) {
        if (ni.getName().substring(0, 1).equalsIgnoreCase("e")) {
            if (!((String) infoMap.get(Integer.valueOf(i))).equalsIgnoreCase(ni.getName()))
                return false;
            i++;
            byte[] mac = ni.getHardwareAddress();

            if (!((String) infoMap.get(Integer.valueOf(i))).equalsIgnoreCase(base64.encodeAsString(mac))) {
                return false;
            }
            i++;
        }
    }
    return true;
}

From source file:cc.arduino.net.PACSupportMethods.java

public String myIpAddress() throws SocketException {
    Optional<NetworkInterface> publicIface = Collections.list(NetworkInterface.getNetworkInterfaces()).stream()
            .filter((iface) -> {/*  www  .java 2 s. co  m*/
                try {
                    return !iface.isLoopback();
                } catch (SocketException e) {
                    throw new RuntimeException(e);
                }
            }).filter((iface) -> iface.getInetAddresses().hasMoreElements()).findFirst();

    if (publicIface.isPresent()) {
        return publicIface.get().getInetAddresses().nextElement().getHostAddress();
    }

    return "127.0.0.1";
}

From source file:org.apache.ftpserver.test.TestUtil.java

public static String[] getHostAddresses() throws Exception {
    Enumeration<NetworkInterface> nifs = NetworkInterface.getNetworkInterfaces();

    List<String> hostIps = new ArrayList<String>();
    while (nifs.hasMoreElements()) {
        NetworkInterface nif = nifs.nextElement();
        Enumeration<InetAddress> ips = nif.getInetAddresses();

        while (ips.hasMoreElements()) {
            InetAddress ip = ips.nextElement();
            if (ip instanceof java.net.Inet4Address) {
                hostIps.add(ip.getHostAddress());
            } else {
                // IPv6 not tested
            }//w  w w.  j  ava2  s  .  c  om
        }
    }

    return hostIps.toArray(new String[0]);
}

From source file:com.frame.network.utils.NetworkUtil.java

public static String getLocalIpv4Address() {
    try {/* www.  j a v  a2  s  . c o m*/
        for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en
                .hasMoreElements();) {
            NetworkInterface intf = en.nextElement();
            for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
                InetAddress inetAddress = enumIpAddr.nextElement();
                if (!inetAddress.isLoopbackAddress()
                        && InetAddressUtils.isIPv4Address(inetAddress.getHostAddress())) {
                    return inetAddress.getHostAddress().toString();
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
    return null;
}

From source file:com.buaa.cfs.net.DNS.java

/**
 * @return NetworkInterface for the given subinterface name (eg eth0:0) or null if no interface with the given name
 * can be found//  w  ww  .j  a  v  a2s.  c  o  m
 */
private static NetworkInterface getSubinterface(String strInterface) throws SocketException {
    Enumeration<NetworkInterface> nifs = NetworkInterface.getNetworkInterfaces();

    while (nifs.hasMoreElements()) {
        Enumeration<NetworkInterface> subNifs = nifs.nextElement().getSubInterfaces();

        while (subNifs.hasMoreElements()) {
            NetworkInterface nif = subNifs.nextElement();
            if (nif.getName().equals(strInterface)) {
                return nif;
            }
        }
    }
    return null;
}

From source file:com.git.original.common.utils.IPUtils.java

/**
 * ?IP?/*from   w w  w  .j a  va2 s .  c  o m*/
 * <p>
 * :<br/>
 * 1. 220.xxx.xxx.xxx<br>
 * 2. 123.xxx.xxx.xxx<br>
 * other<br>
 * 
 * @return
 * @throws SocketException
 *             If an I/O error occurs.
 * @throws NullPointerException
 *             can not found the interface
 * @throws RuntimeException
 *             can not get net address
 */
public static InetAddress getWANIpv4Address(String interfaceName)
        throws SocketException, NullPointerException, RuntimeException {

    InetAddress ipStartWith123 = null;
    InetAddress ipv4Addr = null;

    if (StringUtils.isNotEmpty(interfaceName)) {
        // ?
        NetworkInterface netInterface = NetworkInterface.getByName(interfaceName.trim());
        if (netInterface == null) {
            throw new NullPointerException("can not found the network interface by name: " + interfaceName);
        }

        Enumeration<InetAddress> addrEnum = netInterface.getInetAddresses();
        while (addrEnum.hasMoreElements()) {
            InetAddress addr = addrEnum.nextElement();
            String hostAddr = addr.getHostAddress();

            if (hostAddr.startsWith("220.")) {
                return addr;
            } else if (ipStartWith123 == null && hostAddr.startsWith("123.")) {
                ipStartWith123 = addr;
            } else if (addr instanceof Inet4Address) {
                ipv4Addr = addr;
            }
        }
    } else {
        /*
         * ???
         */
        Enumeration<NetworkInterface> interfaceEnum = NetworkInterface.getNetworkInterfaces();
        while (interfaceEnum.hasMoreElements()) {
            NetworkInterface netInterface = interfaceEnum.nextElement();
            if (netInterface.isLoopback() || !netInterface.isUp()) {
                continue;
            }

            Enumeration<InetAddress> addrEnum = netInterface.getInetAddresses();
            while (addrEnum.hasMoreElements()) {
                InetAddress addr = addrEnum.nextElement();
                String hostAddr = addr.getHostAddress();

                if (hostAddr.startsWith("220.")) {
                    return addr;
                } else if (ipStartWith123 == null && hostAddr.startsWith("123.")) {
                    ipStartWith123 = addr;
                } else if (addr instanceof Inet4Address) {
                    ipv4Addr = addr;
                }
            }
        }
    }

    if (ipStartWith123 != null) {
        return ipStartWith123;
    } else if (ipv4Addr != null) {
        return ipv4Addr;
    }

    throw new RuntimeException("can not get WAN Address");
}

From source file:com.frostwire.util.VPNs.java

public static void printNetworkInterfaces() {
    try {/*from  w w  w  .  j  av  a2  s.  c o m*/
        Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
        while (networkInterfaces.hasMoreElements()) {
            NetworkInterface iface = networkInterfaces.nextElement();
            System.out.println(iface.getIndex() + ":" + iface.getDisplayName() + ":" + "virtual="
                    + iface.isVirtual() + ":" + "mtu=" + iface.getMTU() + ":mac="
                    + (iface.getHardwareAddress() != null
                            ? "0x" + ByteUtils.encodeHex(iface.getHardwareAddress())
                            : "n/a"));
        }
    } catch (SocketException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

From source file:org.openhab.binding.networkhealth.discovery.NetworkHealthDiscoveryService.java

/**
 * Gets every IPv4 Address on each Interface except the loopback
 * The Address format is ip/subnet//  w ww.  ja  v a  2  s.c om
 * @return The collected IPv4 Addresses
 */
private TreeSet<String> getInterfaceIPs() {
    TreeSet<String> interfaceIPs = new TreeSet<String>();

    try {
        // For each interface ...
        for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en
                .hasMoreElements();) {
            NetworkInterface networkInterface = en.nextElement();
            if (!networkInterface.isLoopback()) {

                // .. and for each address ...
                for (Iterator<InterfaceAddress> it = networkInterface.getInterfaceAddresses().iterator(); it
                        .hasNext();) {

                    // ... get IP and Subnet 
                    InterfaceAddress interfaceAddress = it.next();
                    interfaceIPs.add(interfaceAddress.getAddress().getHostAddress() + "/"
                            + interfaceAddress.getNetworkPrefixLength());
                }
            }
        }
    } catch (SocketException e) {
    }

    return interfaceIPs;
}