Example usage for java.net NetworkInterface isLoopback

List of usage examples for java.net NetworkInterface isLoopback

Introduction

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

Prototype


public boolean isLoopback() throws SocketException 

Source Link

Document

Returns whether a network interface is a loopback interface.

Usage

From source file:ee.ria.xroad.proxy.opmonitoring.OpMonitoringBuffer.java

@SneakyThrows
private static boolean isNonLoopback(NetworkInterface ni) {
    return !ni.isLoopback() && ni.isUp();
}

From source file:eu.stratosphere.pact.test.util.minicluster.NepheleMiniCluster.java

private static NetworkInterface getNetworkInterface() throws SocketException {
    final Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();

    while (interfaces.hasMoreElements()) {
        NetworkInterface nic = interfaces.nextElement();
        if (!nic.isLoopback() && !nic.isPointToPoint())
            return nic;
    }//w ww  . j  a va 2  s . com

    throw new SocketException("Cannot find network interface which is not a loopback interface.");
}

From source file:com.vaadin.tests.tb3.PrivateTB3Configuration.java

/**
 * Tries to automatically determine the IP address of the machine the test
 * is running on./*from   w w  w  .j av  a  2s  . com*/
 * 
 * @return An IP address of one of the network interfaces in the machine.
 * @throws RuntimeException
 *             if there was an error or no IP was found
 */
private static String findAutoHostname() {
    try {
        Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
        while (interfaces.hasMoreElements()) {
            NetworkInterface nwInterface = interfaces.nextElement();
            if (!nwInterface.isUp() || nwInterface.isLoopback() || nwInterface.isVirtual()) {
                continue;
            }
            Enumeration<InetAddress> addresses = nwInterface.getInetAddresses();
            while (addresses.hasMoreElements()) {
                InetAddress address = addresses.nextElement();
                if (address.isLoopbackAddress()) {
                    continue;
                }
                if (address.isSiteLocalAddress()) {
                    return address.getHostAddress();
                }
            }
        }
    } catch (SocketException e) {
        throw new RuntimeException("Could not enumerate ");
    }

    throw new RuntimeException("No compatible (10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16) ip address found.");
}

From source file:org.pentaho.platform.util.UUIDUtil.java

public static String getActiveNetworkMacAddress() throws Exception {
    Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
    while (interfaces.hasMoreElements()) {
        NetworkInterface nif = interfaces.nextElement();
        // Obvious what the next IF is doing, but it must be connected, non-PPP, non-loopback, and non-virtual
        if ((nif.isUp()) && (!nif.isPointToPoint()) && (!nif.isLoopback()) && (!nif.isVirtual())) {
            return getInterfaceInfo(nif);
        }/* w  w  w.  j a  v a2s  .co m*/
    }
    return null;
}

From source file:Main.java

public static void printParameter(NetworkInterface ni) throws SocketException {
    System.out.println(" Name = " + ni.getName());
    System.out.println(" Display Name = " + ni.getDisplayName());
    System.out.println(" Is up = " + ni.isUp());
    System.out.println(" Support multicast = " + ni.supportsMulticast());
    System.out.println(" Is loopback = " + ni.isLoopback());
    System.out.println(" Is virtual = " + ni.isVirtual());
    System.out.println(" Is point to point = " + ni.isPointToPoint());
    System.out.println(" Hardware address = " + ni.getHardwareAddress());
    System.out.println(" MTU = " + ni.getMTU());

    System.out.println("\nList of Interface Addresses:");
    List<InterfaceAddress> list = ni.getInterfaceAddresses();
    Iterator<InterfaceAddress> it = list.iterator();

    while (it.hasNext()) {
        InterfaceAddress ia = it.next();
        System.out.println(" Address = " + ia.getAddress());
        System.out.println(" Broadcast = " + ia.getBroadcast());
        System.out.println(" Network prefix length = " + ia.getNetworkPrefixLength());
        System.out.println("");
    }/* www .  j ava  2s.  c  o m*/
}

From source file:net.centro.rtb.monitoringcenter.infos.NodeInfo.java

private static String detectLoadBalancerIpAddress() {
    Enumeration<NetworkInterface> networkInterfaces = null;
    try {/*from  w w w  .  jav a 2s .  c o  m*/
        networkInterfaces = NetworkInterface.getNetworkInterfaces();
    } catch (SocketException e) {
        logger.debug("Unable to obtain network interfaces!", e);
        return null;
    }

    for (NetworkInterface networkInterface : Collections.list(networkInterfaces)) {
        boolean isLoopback = false;
        try {
            isLoopback = networkInterface.isLoopback();
        } catch (SocketException e) {
            logger.debug("Unable to identify if a network interface is a loopback or not");
            continue;
        }

        if (isLoopback) {
            Enumeration<InetAddress> inetAddresses = networkInterface.getInetAddresses();
            while (inetAddresses.hasMoreElements()) {
                InetAddress inetAddress = inetAddresses.nextElement();
                if (!inetAddress.isLoopbackAddress() && Inet4Address.class.isInstance(inetAddress)) {
                    return inetAddress.getHostAddress();
                }
            }
        }
    }

    return null;
}

From source file:net.centro.rtb.monitoringcenter.infos.NodeInfo.java

private static String detectPublicIpAddress() {
    Enumeration<NetworkInterface> networkInterfaces = null;
    try {// w w w  . ja va2s  .  c o m
        networkInterfaces = NetworkInterface.getNetworkInterfaces();
    } catch (SocketException e) {
        logger.debug("Unable to obtain network interfaces!", e);
        return null;
    }

    for (NetworkInterface networkInterface : Collections.list(networkInterfaces)) {
        boolean isLoopback = false;
        try {
            isLoopback = networkInterface.isLoopback();
        } catch (SocketException e) {
            logger.debug("Unable to identify if a network interface is a loopback or not");
        }

        if (!isLoopback) {
            Enumeration<InetAddress> inetAddresses = networkInterface.getInetAddresses();
            while (inetAddresses.hasMoreElements()) {
                InetAddress inetAddress = inetAddresses.nextElement();
                if (Inet4Address.class.isInstance(inetAddress)) {
                    if (!inetAddress.isLoopbackAddress() && !inetAddress.isAnyLocalAddress()
                            && !inetAddress.isLinkLocalAddress() && !inetAddress.isSiteLocalAddress()) {
                        return inetAddress.getHostAddress();
                    }
                }
            }
        }
    }

    return null;
}

From source file:org.openhab.binding.network.service.NetworkService.java

/**
 * Gets every IPv4 Address on each Interface except the loopback
 * The Address format is ip/subnet//from  w ww . jav  a2  s . c o  m
 *
 * @return The collected IPv4 Addresses
 */
private static 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;
}

From source file:es.auth.plugin.AbstractUnitTest.java

public static String getNonLocalhostAddress() {
    try {/*w ww  .j  a  v  a 2 s  .  com*/
        for (final Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en
                .hasMoreElements();) {
            final NetworkInterface intf = en.nextElement();
            if (intf.isLoopback() || !intf.isUp()) {
                continue;
            }
            for (final Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr
                    .hasMoreElements();) {
                final InetAddress ia = enumIpAddr.nextElement();
                if (ia.isLoopbackAddress() || ia instanceof Inet6Address) {
                    continue;
                }
                return ia.getHostAddress();
            }
        }
    } catch (final SocketException e) {
        throw new RuntimeException(e);
    }
    System.out.println("ERROR: No non-localhost address available, will use localhost");
    return "localhost";
}

From source file:org.tinymediamanager.core.License.java

/**
 * returns the MAC address of this instance
 * //from  ww  w.jav  a2  s.c o m
 * @return MAC or empty string
 */
public static String getMac() {
    try {
        InetAddress ip = InetAddress.getLocalHost();
        if (ip != null) {
            // we are connected to Internet/router and have an IP
            NetworkInterface ni = NetworkInterface.getByInetAddress(ip);
            if (ni == null) {
                // search for other interfaces
                Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
                while (networkInterfaces.hasMoreElements()) {
                    NetworkInterface network = networkInterfaces.nextElement();
                    if (!network.isLoopback()) {
                        ni = network;
                        break;
                    }
                }
            }
            String macAddress = formatMac(ni.getHardwareAddress());
            if (macAddress != null && !macAddress.isEmpty()) {
                return macAddress;
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        LOGGER.warn("Error getting MAC from LocalHost IP - not connected to internet/router?");
    }

    try {
        for (Enumeration<NetworkInterface> nif = NetworkInterface.getNetworkInterfaces(); nif
                .hasMoreElements();) {
            NetworkInterface ni = null;
            try {
                ni = nif.nextElement();
                String macAddress = formatMac(ni.getHardwareAddress());
                if (macAddress != null && !macAddress.isEmpty()) {
                    // get first
                    return macAddress;
                }
            } catch (Exception e2) {
                LOGGER.warn("Error getting MAC of " + ni);
            }
        }
        return UNKNOWN_MAC;
    } catch (Exception e) {
        LOGGER.warn("I/O Error on getting network interfaces");
        return UNKNOWN_MAC;
    }
}