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:me.j360.trace.server.brave.BraveConfiguration.java

/** This gets the lanIP without trying to lookup its name. */
// http://stackoverflow.com/questions/8765578/get-local-ip-address-without-connecting-to-the-internet
@Bean/*ww  w  . j a v  a  2 s.  c o  m*/
@Scope
Endpoint local(@Value("${server.port:9411}") int port) {
    int ipv4;
    try {
        ipv4 = Collections.list(NetworkInterface.getNetworkInterfaces()).stream()
                .flatMap(i -> Collections.list(i.getInetAddresses()).stream())
                .filter(ip -> ip instanceof Inet4Address && ip.isSiteLocalAddress())
                .map(InetAddress::getAddress).map(bytes -> new BigInteger(bytes).intValue()).findAny().get();
    } catch (Exception ignored) {
        ipv4 = 127 << 24 | 1;
    }
    return Endpoint.create("zipkin-server", ipv4, port);
}

From source file:com.icloud.framework.core.util.FBUtilities.java

public static String getIp() {
    String localip = null;// IP?IP
    String netip = null;// IP
    try {//from w  w w  . java2  s  .co m
        Enumeration<NetworkInterface> netInterfaces = NetworkInterface.getNetworkInterfaces();
        InetAddress ip = null;
        boolean finded = false;// ?IP
        while (netInterfaces.hasMoreElements() && !finded) {
            NetworkInterface ni = netInterfaces.nextElement();
            Enumeration<InetAddress> address = ni.getInetAddresses();
            while (address.hasMoreElements()) {
                ip = address.nextElement();

                if (!ip.isSiteLocalAddress() && !ip.isLoopbackAddress()
                        && ip.getHostAddress().indexOf(":") == -1) {// IP
                    netip = ip.getHostAddress();
                    finded = true;
                    break;
                } else if (ip.isSiteLocalAddress() && !ip.isLoopbackAddress()
                        && ip.getHostAddress().indexOf(":") == -1) {// IP
                    localip = ip.getHostAddress();
                }
            }
        }
    } catch (SocketException e) {
        e.printStackTrace();
    }
    if (netip != null && !"".equals(netip)) {
        return netip;
    } else {
        return localip;
    }
}

From source file:org.wso2.carbon.analytics.message.tracer.handler.util.PublisherUtil.java

private static InetAddress getLocalAddress() {
    Enumeration<NetworkInterface> ifaces = null;
    try {//from w w w.j av  a  2 s  .co  m
        ifaces = NetworkInterface.getNetworkInterfaces();
    } catch (SocketException e) {
        LOG.error("Failed to get host address", e);
    }
    if (ifaces != null) {
        while (ifaces.hasMoreElements()) {
            NetworkInterface iface = ifaces.nextElement();
            Enumeration<InetAddress> addresses = iface.getInetAddresses();
            while (addresses.hasMoreElements()) {
                InetAddress addr = addresses.nextElement();
                if (addr instanceof Inet4Address && !addr.isLoopbackAddress()) {
                    return addr;
                }
            }
        }
    }
    return null;
}

From source file:com.adito.core.InterfacesMultiSelectListDataSource.java

public Collection<LabelValueBean> getValues(SessionInfo session) {
    ArrayList l = new ArrayList();
    try {/*from w  ww. j  av  a  2s  .  c o m*/
        // TODO make this localised
        l.add(new LabelValueBean("All Interfaces", "0.0.0.0"));
        for (Enumeration e = NetworkInterface.getNetworkInterfaces(); e.hasMoreElements();) {
            NetworkInterface ni = (NetworkInterface) e.nextElement();
            for (Enumeration e2 = ni.getInetAddresses(); e2.hasMoreElements();) {
                InetAddress addr = (InetAddress) e2.nextElement();
                l.add(new LabelValueBean(addr.getHostAddress(), addr.getHostAddress()));
            }
        }
    } catch (Throwable t) {
        log.error("Failed to list network interfaces.", t);
    }
    return l;
}

From source file:com.wmfsystem.eurekaserver.broadcast.Server.java

public static Set<InetAddress> getLocalAddress() throws SocketException {
    Set<InetAddress> address = new HashSet<>();
    Enumeration<NetworkInterface> ifaces = NetworkInterface.getNetworkInterfaces();
    while (ifaces.hasMoreElements()) {
        NetworkInterface iface = ifaces.nextElement();
        Enumeration<InetAddress> addresses = iface.getInetAddresses();

        while (addresses.hasMoreElements()) {
            InetAddress addr = addresses.nextElement();

            if (addr instanceof Inet4Address && !addr.isLoopbackAddress()) {
                address.add(addr);/*from w w w.  ja  va  2 s.com*/
            }
        }
    }

    return address;
}

From source file:org.openhab.binding.zway.internal.discovery.ZWayBridgeDiscoveryService.java

private void scan() {
    logger.debug("Starting scan for Z-Way Server");

    ValidateIPV4 validator = new ValidateIPV4();

    try {/*from   w  w w  .  j  a va  2 s . co m*/
        Enumeration<NetworkInterface> enumNetworkInterface = NetworkInterface.getNetworkInterfaces();
        while (enumNetworkInterface.hasMoreElements()) {
            NetworkInterface networkInterface = enumNetworkInterface.nextElement();
            if (networkInterface.isUp() && !networkInterface.isVirtual() && !networkInterface.isLoopback()) {
                for (InterfaceAddress address : networkInterface.getInterfaceAddresses()) {
                    if (validator.isValidIPV4(address.getAddress().getHostAddress())) {
                        String ipAddress = address.getAddress().getHostAddress();
                        Short prefix = address.getNetworkPrefixLength();

                        logger.debug("Scan IP address for Z-Way Server: {}", ipAddress);

                        // Search on localhost first
                        scheduler.execute(new ZWayServerScan(ipAddress));

                        String subnet = ipAddress + "/" + prefix;
                        SubnetUtils utils = new SubnetUtils(subnet);
                        String[] addresses = utils.getInfo().getAllAddresses();

                        for (String addressInSubnet : addresses) {
                            scheduler.execute(new ZWayServerScan(addressInSubnet));
                        }
                    }
                }
            }
        }
    } catch (SocketException e) {
        logger.warn("Error occurred while searching Z-Way servers ({})", e.getMessage());
    }
}

From source file:org.siddhiesb.transport.passthru.util.PassThroughTransportUtils.java

/**
 * Whatever this method returns as the IP is ignored by the actual http/s listener when
 * its getServiceEPR is invoked. This was originally copied from axis2
 *
 * @return Returns String./*from www .j a v a 2s  .  co m*/
 * @throws java.net.SocketException if the socket can not be accessed
 */
public static String getIpAddress() throws SocketException {
    Enumeration e = NetworkInterface.getNetworkInterfaces();
    String address = "127.0.0.1";
    while (e.hasMoreElements()) {
        NetworkInterface netface = (NetworkInterface) e.nextElement();
        Enumeration addresses = netface.getInetAddresses();

        while (addresses.hasMoreElements()) {
            InetAddress ip = (InetAddress) addresses.nextElement();
            if (!ip.isLoopbackAddress() && isIP(ip.getHostAddress())) {
                return ip.getHostAddress();
            }
        }
    }
    return address;
}

From source file:edu.stanford.junction.provider.jx.JunctionProvider.java

public static String getLocalIpAddress() {
    try {/* www .  j a v  a  2  s.  c  om*/
        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()) {
                    // not ready for IPv6, apparently.
                    if (!inetAddress.getHostAddress().contains(":")) {
                        return inetAddress.getHostAddress().toString();
                    }
                }
            }
        }
    } catch (SocketException ex) {
        Log.e("junction", ex.toString());
    }
    return null;
}

From source file:org.openhab.binding.network.internal.utils.NetworkUtils.java

/**
 * Gets every IPv4 Address on each Interface except the loopback
 * The Address format is ip/subnet//  w  w w. j ava 2 s. com
 *
 * @return The collected IPv4 Addresses
 */
public Set<String> getInterfaceIPs() {
    Set<String> interfaceIPs = new HashSet<>();

    Enumeration<NetworkInterface> interfaces;
    try {
        interfaces = NetworkInterface.getNetworkInterfaces();
    } catch (SocketException ignored) {
        // If we are not allowed to enumerate, we return an empty result set.
        return interfaceIPs;
    }

    // For each interface ...
    for (Enumeration<NetworkInterface> en = interfaces; en.hasMoreElements();) {
        NetworkInterface networkInterface = en.nextElement();
        boolean isLoopback = true;
        try {
            isLoopback = networkInterface.isLoopback();
        } catch (SocketException ignored) {
        }
        if (!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());
            }
        }
    }

    return interfaceIPs;
}

From source file:hsyndicate.utils.IPUtils.java

public static Collection<String> getHostAddress() {
    if (!cachedHostAddr.isEmpty()) {
        return Collections.unmodifiableCollection(cachedHostAddr);
    } else {/*from   w ww.j  a  v a2 s .com*/
        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();

                    String hostAddress = i.getHostAddress();
                    if (isProperHostAddress(hostAddress)) {
                        if (!cachedHostAddr.contains(hostAddress)) {
                            cachedHostAddr.add(hostAddress);
                        }
                    }

                    String hostName = i.getHostName();
                    if (isProperHostAddress(hostName)) {
                        if (!cachedHostAddr.contains(hostName)) {
                            cachedHostAddr.add(hostName);
                        }
                    }

                    String canonicalHostName = i.getCanonicalHostName();
                    if (isProperHostAddress(canonicalHostName)) {
                        if (!cachedHostAddr.contains(canonicalHostName)) {
                            cachedHostAddr.add(canonicalHostName);
                        }
                    }
                }
            }
        } catch (SocketException ex) {
            LOG.error("Exception occurred while scanning local interfaces", ex);
        }

        return Collections.unmodifiableCollection(cachedHostAddr);
    }
}