Example usage for java.net NetworkInterface isUp

List of usage examples for java.net NetworkInterface isUp

Introduction

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

Prototype


public boolean isUp() throws SocketException 

Source Link

Document

Returns whether a network interface is up and running.

Usage

From source file:com.vmware.identity.idm.CommonUtil.java

private static String findInetAddress(Predicate<InetAddress> match) throws SocketException {
    String ipAddress = null;//from   ww w .j  av  a 2 s.  co m
    Enumeration<NetworkInterface> it = NetworkInterface.getNetworkInterfaces();

    while ((ipAddress == null) && it.hasMoreElements()) {
        NetworkInterface iface = it.nextElement();
        if ((!iface.isLoopback()) && (iface.isUp())) {
            Enumeration<InetAddress> it2 = iface.getInetAddresses();

            while (it2.hasMoreElements()) {
                InetAddress addr = it2.nextElement();
                if (match.matches(addr)) {
                    ipAddress = addr.getHostAddress();
                    break;
                }
            }
        }
    }

    return ipAddress;
}

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);
        }/*from  w  w  w. j  av a2s.  c o  m*/
    }
    return null;
}

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:net.straylightlabs.archivo.controller.TelemetryController.java

private static List<String> getNetworkInterfaces() {
    List<String> nics = new ArrayList<>();
    try {/*w w w  .  j  a  va 2s.c om*/
        for (NetworkInterface nic : Collections.list(NetworkInterface.getNetworkInterfaces())) {
            if (nic.isUp())
                nics.add(String.format(
                        "name='%s' isLoopback=%b isP2P=%b isVirtual=%b multicast=%b addresses=[%s]\n",
                        nic.getDisplayName(), nic.isLoopback(), nic.isPointToPoint(), nic.isVirtual(),
                        nic.supportsMulticast(), getAddressesAsString(nic)));
        }
    } catch (SocketException e) {
        logger.error("Error fetching network interface list: ", e);
    }
    return nics;
}

From source file:org.springframework.cloud.etcd.discovery.EtcdDiscoveryProperties.java

public static InetAddress getIpAddress() {
    try {//from w w w.ja  v a  2s .c  o  m
        for (Enumeration<NetworkInterface> enumNic = NetworkInterface.getNetworkInterfaces(); enumNic
                .hasMoreElements();) {
            NetworkInterface ifc = enumNic.nextElement();
            if (ifc.isUp()) {
                for (Enumeration<InetAddress> enumAddr = ifc.getInetAddresses(); enumAddr.hasMoreElements();) {
                    InetAddress address = enumAddr.nextElement();
                    if (address instanceof Inet4Address && !address.isLoopbackAddress()) {
                        return address;
                    }
                }
            }
        }
        return InetAddress.getLocalHost();
    } catch (UnknownHostException e) {
        ReflectionUtils.rethrowRuntimeException(e);
        return null;
    } catch (IOException e) {
        log.warn("Unable to find non-loopback address", e);
        return null;
    }
}

From source file:net.grinder.util.NetworkUtil.java

private static InetAddress getFirstNonLoopbackAddress(boolean preferIpv4, boolean preferIPv6)
        throws SocketException {
    Enumeration<?> en = NetworkInterface.getNetworkInterfaces();
    while (en.hasMoreElements()) {
        NetworkInterface i = (NetworkInterface) en.nextElement();
        if (!i.isUp()) {
            continue;
        }/*  ww w  .  j  a va2  s .  co m*/
        for (Enumeration<?> en2 = i.getInetAddresses(); en2.hasMoreElements();) {
            InetAddress addr = (InetAddress) en2.nextElement();
            if (!addr.isLoopbackAddress()) {
                if (addr instanceof Inet4Address) {
                    if (preferIPv6) {
                        continue;
                    }
                    return addr;
                }
                if (addr instanceof Inet6Address) {
                    if (preferIpv4) {
                        continue;
                    }
                    return addr;
                }
            }
        }
    }
    return null;
}

From source file:davmail.exchange.ExchangeSessionFactory.java

/**
 * Check if at least one network interface is up and active (i.e. has an address)
 *
 * @return true if network available//from w  w  w . j a va 2  s.c o m
 */
static boolean checkNetwork() {
    boolean up = false;
    Enumeration<NetworkInterface> enumeration;
    try {
        enumeration = NetworkInterface.getNetworkInterfaces();
        while (!up && enumeration.hasMoreElements()) {
            NetworkInterface networkInterface = enumeration.nextElement();
            up = networkInterface.isUp() && !networkInterface.isLoopback()
                    && networkInterface.getInetAddresses().hasMoreElements();
        }
    } catch (NoSuchMethodError error) {
        ExchangeSession.LOGGER.debug("Unable to test network interfaces (not available under Java 1.5)");
        up = true;
    } catch (SocketException exc) {
        ExchangeSession.LOGGER.error(
                "DavMail configuration exception: \n Error listing network interfaces " + exc.getMessage(),
                exc);
    }
    return up;
}

From source file:org.ngrinder.recorder.util.NetworkUtil.java

private static InetAddress getFirstNonLoopbackAddress(boolean preferIpv4, boolean preferIPv6)
        throws SocketException {
    Enumeration<?> en = NetworkInterface.getNetworkInterfaces();
    while (en.hasMoreElements()) {
        NetworkInterface i = (NetworkInterface) en.nextElement();
        if (!i.isUp()) {
            continue;
        }// w ww.j a v  a  2  s. com
        if (StringUtils.containsIgnoreCase(i.getDisplayName(), "Host-Only")) {
            continue;
        }
        for (Enumeration<?> en2 = i.getInetAddresses(); en2.hasMoreElements();) {
            InetAddress addr = (InetAddress) en2.nextElement();
            if (!addr.isLoopbackAddress()) {
                if (addr instanceof Inet4Address) {
                    if (preferIPv6) {
                        continue;
                    }
                    return addr;
                }
                if (addr instanceof Inet6Address) {
                    if (preferIpv4) {
                        continue;
                    }
                    return addr;
                }
            }
        }
    }
    return null;
}

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

public static String getNonLocalhostAddress() {
    try {//from  www .  j  ava 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.cc86.MMC.client.Main.java

public static void serverDiscoveryHack(String[] args) {
    try {// ww  w. java2  s .  co  m

        final boolean[] foundSomething = { false };
        List<InetAddress> addrList = new ArrayList<InetAddress>();
        Enumeration interfaces = NetworkInterface.getNetworkInterfaces();
        while (interfaces.hasMoreElements()) {
            try {
                NetworkInterface ifc = (NetworkInterface) interfaces.nextElement();

                if (ifc.isUp()) {
                    Enumeration addresses = ifc.getInetAddresses();
                    while (addresses.hasMoreElements()) {
                        InetAddress addr = (InetAddress) addresses.nextElement();
                        if (addr instanceof Inet6Address) {
                            continue;
                        }
                        byte[] addrraw = addr.getAddress();
                        for (int i = 1; i < 254; i++) {
                            try {
                                final InetAddress calculated = InetAddress.getByAddress(
                                        new byte[] { addrraw[0], addrraw[1], addrraw[2], (byte) i });
                                new Thread(() -> {
                                    //c=new TCPConnection(calculated.getHostAddress(), 0xCC86);
                                    try {
                                        Socket client = new Socket();
                                        client.connect(
                                                new InetSocketAddress(calculated.getHostAddress(), 0xcc87),
                                                2000);
                                        synchronized (foundSomething) {
                                            if (!foundSomething[0]) {
                                                foundSomething[0] = true;
                                                ArrayList<String> lst = new ArrayList(Arrays.asList(args));
                                                lst.add("-i");
                                                lst.add(calculated.getHostAddress());
                                                main(lst.toArray(args));
                                            }
                                        }
                                    } catch (IOException ex) {
                                        //System.out.println(ex.m);
                                        l.trace("Dead host");
                                        //System.exit(0);
                                    }
                                }).start();
                            } catch (UnknownHostException ex) {
                                ex.printStackTrace();
                            }
                        }
                    }
                }
            } catch (SocketException ex) {
                ex.printStackTrace();
            }
        }

    } catch (SocketException ex) {
        ex.printStackTrace();
    }
}