Example usage for java.net NetworkInterface getInetAddresses

List of usage examples for java.net NetworkInterface getInetAddresses

Introduction

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

Prototype

public Enumeration<InetAddress> getInetAddresses() 

Source Link

Document

Get an Enumeration with all or a subset of the InetAddresses bound to this network interface.

Usage

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

public static InetAddress findNonLocalhostIp() throws Exception {
    Enumeration<NetworkInterface> nifs = NetworkInterface.getNetworkInterfaces();

    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 && !ip.isLoopbackAddress()) {
                return ip;
            } else {
                // IPv6 not tested
            }// w  w w.  j  a  v  a2s  .c  o  m
        }
    }

    return null;
}

From source file:com.netsteadfast.greenstep.util.HostUtils.java

public static String getHostAddress() {
    String hostAddress = "";
    try {//from  w ww. ja v a2s  .c o  m
        Enumeration<NetworkInterface> nics = NetworkInterface.getNetworkInterfaces();
        for (; nics.hasMoreElements() && "".equals(hostAddress);) {
            NetworkInterface interfece = nics.nextElement();
            if (interfece.getName().toLowerCase().startsWith("lo")) {
                continue;
            }
            Enumeration<InetAddress> addrs = interfece.getInetAddresses();
            for (; addrs.hasMoreElements() && "".equals(hostAddress);) {
                InetAddress addr = addrs.nextElement();
                if (addr.getHostAddress().indexOf(":") > -1) {
                    continue;
                }
                hostAddress = addr.getHostAddress();
            }
        }
    } catch (SocketException e) {
        e.printStackTrace();
    }
    if (StringUtils.isBlank(hostAddress)) {
        hostAddress = "127.0.0.1";
    }
    return hostAddress;
}

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
            }/*from ww w  .ja v  a 2  s. c o m*/
        }
    }

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

From source file:com.DPFaragir.DPFUtils.java

public static String getIPAddress(boolean useIPv4) {
    try {// ww w.  jav  a  2  s.com
        List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces());
        for (NetworkInterface intf : interfaces) {
            List<InetAddress> addrs = Collections.list(intf.getInetAddresses());
            for (InetAddress addr : addrs) {
                if (!addr.isLoopbackAddress()) {
                    String sAddr = addr.getHostAddress().toUpperCase();
                    boolean isIPv4 = InetAddressUtils.isIPv4Address(sAddr);
                    if (useIPv4) {
                        if (isIPv4)
                            return sAddr;
                    } else {
                        if (!isIPv4) {
                            int delim = sAddr.indexOf('%'); // drop ip6 port suffix
                            return delim < 0 ? sAddr : sAddr.substring(0, delim);
                        }
                    }
                }
            }
        }
    } catch (Exception ex) {
    } // for now eat exceptions
    return "";
}

From source file:com.vinexs.tool.NetworkManager.java

public static String getIPAddress(boolean useIPv4) {
    try {// ww w.  j  a va2  s.com
        List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces());
        for (NetworkInterface intf : interfaces) {
            List<InetAddress> addrs = Collections.list(intf.getInetAddresses());
            for (InetAddress addr : addrs) {
                if (!addr.isLoopbackAddress()) {
                    String sAddr = addr.getHostAddress().toUpperCase();
                    boolean isIPv4 = InetAddressUtilsHC4.isIPv4Address(sAddr);
                    if (useIPv4) {
                        if (isIPv4) {
                            return sAddr;
                        }
                    } else {
                        if (!isIPv4) {
                            int delim = sAddr.indexOf('%'); // drop ip6 port suffix
                            return delim < 0 ? sAddr : sAddr.substring(0, delim);
                        }
                    }
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return "";
}

From source file:org.talend.commons.utils.network.NetworkUtil.java

public static boolean isSelfAddress(String addr) {
    if (addr == null || addr.isEmpty()) {
        return false; // ?
    }/*from   www .  j ava2 s .  co  m*/

    try {
        final InetAddress sourceAddress = InetAddress.getByName(addr);
        if (sourceAddress.isLoopbackAddress()) {
            // final String hostAddress = sourceAddress.getHostAddress();
            // // if addr is localhost, will be 127.0.0.1 also
            // if (hostAddress.equals("127.0.0.1") || hostAddress.equals("localhost") ) {
            return true;
            // }
        } else {
            // check all ip configs
            InetAddress curAddr = null;
            Enumeration<NetworkInterface> netInterfaces = NetworkInterface.getNetworkInterfaces();
            while (netInterfaces.hasMoreElements()) {
                NetworkInterface ni = netInterfaces.nextElement();
                Enumeration<InetAddress> address = ni.getInetAddresses();
                while (address.hasMoreElements()) {
                    curAddr = address.nextElement();
                    if (addr.equals(curAddr.getHostAddress())) {
                        return true;
                    }
                }
            }
        }

    } catch (SocketException e) {
        e.printStackTrace();
    } catch (UnknownHostException e) {
        e.printStackTrace();
    }
    return false;
}

From source file:stargate.commons.utils.IPUtils.java

public static Collection<String> getIPAddress() {
    if (!cached_ip_addr.isEmpty()) {
        return Collections.unmodifiableCollection(cached_ip_addr);
    } else {/*  ww w . java2s . c o m*/
        try {
            Enumeration e = NetworkInterface.getNetworkInterfaces();
            while (e.hasMoreElements()) {
                NetworkInterface n = (NetworkInterface) e.nextElement();
                Enumeration ee = n.getInetAddresses();
                while (ee.hasMoreElements()) {
                    InetAddress i = (InetAddress) ee.nextElement();
                    if (!i.isLoopbackAddress()) {
                        String hostAddress = i.getHostAddress();
                        cached_ip_addr.add(hostAddress);
                    }
                }
            }
        } catch (SocketException ex) {
            LOG.error("Exception occurred while scanning local interfaces", ex);
        }

        return Collections.unmodifiableCollection(cached_ip_addr);
    }
}

From source file:net.straylightlabs.archivo.controller.TelemetryController.java

public static String getAddressesAsString(NetworkInterface nic) {
    StringJoiner sj = new StringJoiner(", ");
    for (InetAddress address : Collections.list(nic.getInetAddresses())) {
        sj.add(address.getHostAddress());

    }//from w  w  w. j a  v  a  2s.  c  om
    return sj.toString();
}

From source file:org.apache.sentry.provider.db.log.util.CommandUtil.java

@VisibleForTesting
public static boolean assertIPInAuditLog(String ipInAuditLog) throws Exception {
    if (ipInAuditLog == null) {
        return false;
    }/*from   w  w  w . j  a  v  a 2  s .  c  o m*/
    Enumeration<NetworkInterface> netInterfaces = NetworkInterface.getNetworkInterfaces();
    while (netInterfaces.hasMoreElements()) {
        NetworkInterface ni = netInterfaces.nextElement();
        Enumeration<InetAddress> ips = ni.getInetAddresses();
        while (ips.hasMoreElements()) {
            if (ipInAuditLog.indexOf(ips.nextElement().getHostAddress()) != -1) {
                return true;
            }
        }
    }
    return false;
}

From source file:com.vinexs.tool.NetworkManager.java

public static InetAddress getInetAddress(Context context) {
    if (haveNetwork(context)) {
        return null;
    }/*from w  ww  .j a v  a 2  s .co m*/
    if (isWifiNetwork(context)) {
        int ipAddress = ((WifiManager) context.getSystemService(Context.WIFI_SERVICE)).getConnectionInfo()
                .getIpAddress();
        if (ipAddress == 0) {
            return null;
        }
        return intToInet(ipAddress);
    }
    try {
        Enumeration<NetworkInterface> netinterfaces = NetworkInterface.getNetworkInterfaces();
        while (netinterfaces.hasMoreElements()) {
            NetworkInterface netinterface = netinterfaces.nextElement();
            Enumeration<InetAddress> adresses = netinterface.getInetAddresses();
            while (adresses.hasMoreElements()) {
                InetAddress address = adresses.nextElement();
                if (!address.isLoopbackAddress() && !address.isLinkLocalAddress()) {
                    return address;
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}