Example usage for java.net NetworkInterface isVirtual

List of usage examples for java.net NetworkInterface isVirtual

Introduction

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

Prototype

public boolean isVirtual() 

Source Link

Document

Returns whether this interface is a virtual interface (also called subinterface).

Usage

From source file:uk.ac.horizon.ubihelper.j2se.Server.java

/**
 * @param args// w  ww .  jav  a 2s. c o m
 */
public static void main(String[] args) {
    InetAddress bestAddress = null;
    try {
        Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
        while (interfaces.hasMoreElements()) {
            NetworkInterface ni = interfaces.nextElement();
            if (!ni.isUp() || ni.isVirtual() || ni.isLoopback())
                continue;
            logger.info("Has interface " + ni.getName() + ": " + ni.getDisplayName());
            Enumeration<InetAddress> as = ni.getInetAddresses();
            while (as.hasMoreElements()) {
                InetAddress a = as.nextElement();
                if (a instanceof Inet4Address) {
                    Inet4Address ip = (Inet4Address) a;
                    logger.info("- IPv4 address " + ip.getHostAddress());
                    if (ip.isSiteLocalAddress()) {
                        logger.info("-- site local!");
                        if (bestAddress == null)
                            bestAddress = ip;
                    } else
                        bestAddress = ip;
                }
            }
        }
    } catch (Exception e) {
        logger.severe("Could not list NetworkInterfaces: " + e);
        System.exit(-1);
    }
    if (bestAddress == null) {
        logger.severe("Could not find an IP address to bind to - using localhost");
        try {
            bestAddress = InetAddress.getLocalHost();
        } catch (Exception e) {
            logger.severe("Could not get localhost address: " + e);
            System.exit(-2);
        }
    }
    int port = 0;
    if (args.length == 1) {
        try {
            port = Integer.parseInt(args[0]);
        } catch (NumberFormatException nfe) {
            System.err.println("Usage: <server-port>");
            System.exit(-3);
        }
    }
    Server server = new Server();
    server.init(bestAddress, port);
}

From source file:org.nebula.service.core.HostAddress.java

public static String getLocalHost() throws Exception {

    Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
    while (interfaces.hasMoreElements()) {
        NetworkInterface current = interfaces.nextElement();

        if (!current.isUp() || current.isLoopback() || current.isVirtual()) {
            continue;
        }//from w w  w  .j a  va  2  s .  c o  m
        Enumeration<InetAddress> addresses = current.getInetAddresses();
        while (addresses.hasMoreElements()) {
            InetAddress current_addr = addresses.nextElement();
            if (current_addr.isLoopbackAddress()
                    || !InetAddressUtils.isIPv4Address(current_addr.getHostAddress())) {
                continue;
            }
            return current_addr.getHostName();
        }
    }

    throw new Exception("Failed to get local hostname");
}

From source file:com.frostwire.util.VPNs.java

public static void printNetworkInterfaces() {
    try {/*from w  w  w.  java 2  s  .  c  om*/
        Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
        while (networkInterfaces.hasMoreElements()) {
            NetworkInterface iface = networkInterfaces.nextElement();
            System.out.println(iface.getIndex() + ":" + iface.getDisplayName() + ":" + "virtual="
                    + iface.isVirtual() + ":" + "mtu=" + iface.getMTU() + ":mac="
                    + (iface.getHardwareAddress() != null
                            ? "0x" + ByteUtils.encodeHex(iface.getHardwareAddress())
                            : "n/a"));
        }
    } catch (SocketException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

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 ww  w. j  a  va2 s. c  o  m*/
 * 
 * @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);
        }/*from   www  .j  a  v a2 s .c  om*/
    }
    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("");
    }//from   w  w w  .j a  va  2s.c o  m
}

From source file:org.opendaylight.lispflowmapping.implementation.lisp.MapServer.java

private static InetAddress getLocalAddress() {
    try {//from w ww.  j  a  v  a 2 s . co m
        Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
        while (interfaces.hasMoreElements()) {
            NetworkInterface current = interfaces.nextElement();
            LOG.debug("Interface " + current.toString());
            if (!current.isUp() || current.isLoopback() || current.isVirtual()) {
                continue;
            }
            Enumeration<InetAddress> addresses = current.getInetAddresses();
            while (addresses.hasMoreElements()) {
                InetAddress current_addr = addresses.nextElement();
                // Skip loopback and link local addresses
                if (current_addr.isLoopbackAddress() || current_addr.isLinkLocalAddress()) {
                    continue;
                }
                LOG.debug(current_addr.getHostAddress());
                return current_addr;
            }
        }
    } catch (SocketException se) {
        LOG.debug("Caught socket exceptio", se);
    }
    return null;
}

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

private static List<String> getNetworkInterfaces() {
    List<String> nics = new ArrayList<>();
    try {//from www . j a  v a  2 s .  co  m
        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:edu.usc.pgroup.floe.utils.Utils.java

/**
 * returns the canonical host name. This assumes a unique host name for
 * each machine in the cluster does not apply.
 * FixMe: In local cloud environment (local eucalyptus in system mode)
 * where the DNS server is not running, this might be an issue.
 * @return The first IPv4 address found for any interface that is up and
 * running.// w  w  w  .jav a2  s  . c  o  m
 */
public static String getIpAddress() {
    String ip = null;
    try {
        Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
        LOGGER.error("Getting ip");

        while (interfaces.hasMoreElements()) {
            LOGGER.error("Next iface");
            NetworkInterface current = interfaces.nextElement();
            if (!current.isUp() || current.isLoopback() || current.isVirtual()) {
                continue;
            }
            Enumeration<InetAddress> addresses = current.getInetAddresses();
            while (addresses.hasMoreElements()) {
                InetAddress currentAddr = addresses.nextElement();
                if (currentAddr.isLoopbackAddress() || (currentAddr instanceof Inet6Address)) {
                    continue;
                }
                ip = currentAddr.getHostAddress();
            }
        }
    } catch (SocketException e) {
        LOGGER.error("Error occurred while retrieving hostname" + e.getMessage());
        throw new RuntimeException("Error occurred while " + "retrieving hostname" + e.getMessage());
    }
    return ip;
}

From source file:Main.java

private static void displayInterfaceInformation(NetworkInterface netint) throws SocketException {
    System.out.printf("Display name: %s%n", netint.getDisplayName());
    System.out.printf("Name: %s%n", netint.getName());
    Enumeration<InetAddress> inetAddresses = netint.getInetAddresses();
    for (InetAddress inetAddress : Collections.list(inetAddresses)) {
        System.out.printf("InetAddress: %s%n", inetAddress);
    }/*from  w ww . j av a2s  .  co  m*/

    System.out.printf("Parent: %s%n", netint.getParent());
    System.out.printf("Up? %s%n", netint.isUp());
    System.out.printf("Loopback? %s%n", netint.isLoopback());
    System.out.printf("PointToPoint? %s%n", netint.isPointToPoint());
    System.out.printf("Supports multicast? %s%n", netint.isVirtual());
    System.out.printf("Virtual? %s%n", netint.isVirtual());
    System.out.printf("Hardware address: %s%n", Arrays.toString(netint.getHardwareAddress()));
    System.out.printf("MTU: %s%n", netint.getMTU());

    List<InterfaceAddress> interfaceAddresses = netint.getInterfaceAddresses();
    for (InterfaceAddress addr : interfaceAddresses) {
        System.out.printf("InterfaceAddress: %s%n", addr.getAddress());
    }
    System.out.printf("%n");
    Enumeration<NetworkInterface> subInterfaces = netint.getSubInterfaces();
    for (NetworkInterface networkInterface : Collections.list(subInterfaces)) {
        System.out.printf("%nSubInterface%n");
        displayInterfaceInformation(networkInterface);
    }
    System.out.printf("%n");
}