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:com.frostwire.util.VPNs.java

private static boolean isAnyNetworkInterfaceATunnel() {
    boolean result = false;
    try {/* w  w  w.  ja va  2 s . c  om*/
        Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
        while (networkInterfaces.hasMoreElements()) {
            NetworkInterface iface = networkInterfaces.nextElement();
            if (iface.getDisplayName().contains("tun")) {
                result = true;
                break;
            }
        }
    } catch (Throwable e) {
        e.printStackTrace();
    }

    return result;
}

From source file:org.jgrades.lic.service.MacRule.java

public MacRule() {
    try {/*w w  w .j ava2  s.c  o m*/
        networkInterfaces = Collections.list(NetworkInterface.getNetworkInterfaces());
        LOGGER.trace("Network interfaces in system: {}", networkInterfaces);
    } catch (SocketException e) {
        LOGGER.error("Error during preparing network interfaces", e);
        throw new ViolationOfLicenceConditionException(e);
    }
}

From source file:org.sonar.process.NetworkUtils.java

/**
 * Identifying the localhost machine/* w  w  w.  j  av  a 2s  .c om*/
 * It will try to retrieve the hostname and the IPv4 addresses
 *
 * @return "hostname (ipv4_1, ipv4_2...)"
 */
public static String getHostName() {
    String hostname;
    String ips;
    try {
        hostname = InetAddress.getLocalHost().getHostName();
    } catch (UnknownHostException e) {
        hostname = "unresolved hostname";
    }

    try {
        ips = list(NetworkInterface.getNetworkInterfaces()).stream()
                .flatMap(netif -> list(netif.getInetAddresses()).stream().filter(inetAddress ->
                // Removing IPv6 for the time being
                inetAddress instanceof Inet4Address &&
                // Removing loopback addresses, useless for identifying a server
                        !inetAddress.isLoopbackAddress() &&
                        // Removing interfaces without IPs
                        !isBlank(inetAddress.getHostAddress())).map(InetAddress::getHostAddress))
                .filter(p -> !isBlank(p)).collect(Collectors.joining(","));
    } catch (SocketException e) {
        ips = "unresolved IPs";
    }

    return format("%s (%s)", hostname, ips);
}

From source file:com.starit.diamond.client.processor.LocalConfigInfoProcessor.java

static String getHostAddress() {
    String address = "127.0.0.1";
    try {/*  w  w w  .j a va2  s. c om*/
        Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces();
        while (en.hasMoreElements()) {
            NetworkInterface ni = en.nextElement();
            Enumeration<InetAddress> ads = ni.getInetAddresses();
            while (ads.hasMoreElements()) {
                InetAddress ip = ads.nextElement();
                if (!ip.isLoopbackAddress() && ip.isSiteLocalAddress()) {
                    return ip.getHostAddress();
                }
            }
        }
    } catch (Exception e) {
        throw new RuntimeException("??", e);
    }
    return address;
}

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

public static InetAddress getIpAddress() {
    try {// w ww  .  java 2 s  .  c  om
        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:camp.pixels.signage.util.NetworkInterfaces.java

/**
 * @param interfaceName eth0, wlan0 or NULL=use first interface 
 * @param useIPv4/*from   w w  w .ja  v  a2s .co  m*/
 * @return address or empty string
 */
@SuppressLint("DefaultLocale")
public static String getIPAddress(String interfaceName, boolean useIPv4) {
    try {
        List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces());
        for (NetworkInterface intf : interfaces) {
            if (interfaceName != null) {
                if (!intf.getName().equalsIgnoreCase(interfaceName))
                    continue;
            }
            if (!VALID_INTERFACES.contains(intf.getName()))
                continue;
            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 (SocketException ex) {
    }
    return "";
}

From source file:com.marcosdiez.server.php.service.Network.java

public Network() {
    names = new LinkedList<String>();
    titles = new LinkedList<String>();
    adresses = new LinkedList<String>();
    try {//  w w  w  .  j a v  a 2s  .c o m
        List<NetworkInterface> list = Collections.list(NetworkInterface.getNetworkInterfaces());
        for (NetworkInterface iface : list) {
            List<InetAddress> addresses = Collections.list(iface.getInetAddresses());
            for (InetAddress address : addresses) {
                if (!address.isLoopbackAddress()) {
                    String host = address.getHostAddress().toUpperCase();
                    if (InetAddressUtils.isIPv4Address(host)) {
                        names.add(iface.getName());
                        titles.add(host + "(" + iface.getDisplayName() + ")");
                        adresses.add(host);
                    }
                }
            }
        }
        for (NetworkInterface iface : list) {
            List<InetAddress> addresses = Collections.list(iface.getInetAddresses());
            for (InetAddress address : addresses) {
                if (address.isLoopbackAddress()) {
                    String host = address.getHostAddress().toUpperCase();
                    if (InetAddressUtils.isIPv4Address(host)) {
                        names.add(iface.getName());
                        titles.add(host + "(" + iface.getDisplayName() + ")");
                        adresses.add(host);
                    }
                }
            }
        }
    } catch (Exception ex) {
    }
    names.add("all");
    adresses.add("0.0.0.0");
    titles.add("0.0.0.0 (all)");
}

From source file:org.namelessrom.devicecontrol.net.NetworkInfo.java

public static String getIpAddress(final boolean useIPv4) {
    List<NetworkInterface> interfaces;
    try {// www  . ja  v a2  s .  com
        interfaces = Collections.list(NetworkInterface.getNetworkInterfaces());
    } catch (Exception e) {
        interfaces = null;
    }
    if (interfaces == null)
        return "0.0.0.0";
    for (NetworkInterface intf : interfaces) {
        final List<InetAddress> addrs = Collections.list(intf.getInetAddresses());
        for (final InetAddress addr : addrs) {
            if (!addr.isLoopbackAddress()) {
                final String sAddr = addr.getHostAddress().toUpperCase();
                boolean isIPv4 = InetAddressUtils.isIPv4Address(sAddr);
                if (useIPv4) {
                    if (isIPv4) {
                        return sAddr;
                    }
                } else {
                    if (!isIPv4) {
                        final int delim = sAddr.indexOf('%'); // drop ip6 port suffix
                        return ((delim < 0) ? sAddr : sAddr.substring(0, delim));
                    }
                }
            }
        }
    }
    return "0.0.0.0";
}

From source file:org.peercast.pecaport.NetworkDeviceManager.java

/**
 * ?(eth0, eth1..)??//  ww  w. j  a v a2s. c  om
 */
public List<NetworkInterfaceInfo.Ethernet> getEthernetInterface() {
    List<NetworkInterfaceInfo.Ethernet> infos = new ArrayList<>();
    try {

        for (NetworkInterface ni : Collections.list(NetworkInterface.getNetworkInterfaces())) {
            if (!ni.isLoopback() && !ni.isVirtual() && ni.getName().matches("^eth\\d+$") && //
                    ni.getInetAddresses().hasMoreElements())
                infos.add(new NetworkInterfaceInfo.Ethernet(ni));
        }
    } catch (SocketException e) {
        Log.w(TAG, "getEthernetInterface()", e);
    }
    return infos;
}

From source file:org.sakuli.actions.environment.CipherUtil.java

/**
 * Determines a valid network interfaces.
 *
 * @return ethernet interface name//  w w w. j  ava  2s .c om
 * @throws Exception
 */
static String autodetectValidInterfaceName() throws Exception {
    Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
    while (interfaces.hasMoreElements()) {
        NetworkInterface anInterface = interfaces.nextElement();
        if (anInterface.getHardwareAddress() != null && anInterface.getHardwareAddress().length == 6) {
            return anInterface.getName();
        }
    }
    throw new Exception("No network interface with a MAC address is present, please check your os settings!");
}