Example usage for java.net NetworkInterface getIndex

List of usage examples for java.net NetworkInterface getIndex

Introduction

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

Prototype

public int getIndex() 

Source Link

Document

Returns the index of this network interface.

Usage

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

public static void printNetworkInterfaces() {
    try {/*from w  w  w. j a v a2s . com*/
        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:org.sakuli.actions.environment.CipherUtil.java

/**
 * fetch the local network interfaceLog and reads out the MAC of the chosen encryption interface.
 * Must be called before the methods {@link #encrypt(String)} or {@link #decrypt(String)}.
 *
 * @throws SakuliCipherException for wrong interface names and MACs.
 *///  w ww.j av  a  2s  . c om
@PostConstruct
public void scanNetworkInterfaces() throws SakuliCipherException {
    Enumeration<NetworkInterface> networkInterfaces;
    try {
        interfaceName = checkEthInterfaceName();
        networkInterfaces = getNetworkInterfaces();
        while (networkInterfaces.hasMoreElements()) {
            NetworkInterface anInterface = networkInterfaces.nextElement();
            if (anInterface.getHardwareAddress() != null) {
                interfaceLog = interfaceLog + "\nNET-Interface " + anInterface.getIndex() + " - Name: "
                        + anInterface.getName() + "\t MAC: " + formatMAC(anInterface.getHardwareAddress())
                        + "\t VirtualAdapter: " + anInterface.isVirtual() + "\t Loopback: "
                        + anInterface.isLoopback() + "\t Desc.: " + anInterface.getDisplayName();
            }
            if (anInterface.getName().equals(interfaceName)) {
                macOfEncryptionInterface = anInterface.getHardwareAddress();
            }

        }
        if (macOfEncryptionInterface == null) {
            throw new SakuliCipherException(
                    "Cannot resolve MAC address ... please check your config of the property: "
                            + ActionProperties.ENCRYPTION_INTERFACE + "=" + interfaceName,
                    interfaceLog);
        }
    } catch (Exception e) {
        throw new SakuliCipherException(e, interfaceLog);
    }
}

From source file:ch.cyberduck.core.socket.NetworkInterfaceAwareSocketFactory.java

/**
 * @param network Network interface index
 * @throws UnknownHostException//from   ww w . ja  va  2 s .c  o  m
 */
private Inet6Address getByAddressForInterface(final NetworkInterface network, final InetAddress address)
        throws UnknownHostException {
    // Append network interface. Workaround for issue #8802
    return Inet6Address.getByAddress(address.getHostAddress(),
            IPAddressUtil.textToNumericFormatV6(address.getHostAddress()), network.getIndex());
}

From source file:org.springframework.cloud.commons.util.InetUtils.java

public InetAddress findFirstNonLoopbackAddress() {
    InetAddress result = null;//from  www .  j  ava2 s.c  o m
    try {
        int lowest = Integer.MAX_VALUE;
        for (Enumeration<NetworkInterface> nics = NetworkInterface.getNetworkInterfaces(); nics
                .hasMoreElements();) {
            NetworkInterface ifc = nics.nextElement();
            if (ifc.isUp()) {
                log.trace("Testing interface: " + ifc.getDisplayName());
                if (ifc.getIndex() < lowest || result == null) {
                    lowest = ifc.getIndex();
                } else if (result != null) {
                    continue;
                }

                // @formatter:off
                if (!ignoreInterface(ifc.getDisplayName())) {
                    for (Enumeration<InetAddress> addrs = ifc.getInetAddresses(); addrs.hasMoreElements();) {
                        InetAddress address = addrs.nextElement();
                        if (address instanceof Inet4Address && !address.isLoopbackAddress()
                                && !ignoreAddress(address)) {
                            log.trace("Found non-loopback interface: " + ifc.getDisplayName());
                            result = address;
                        }
                    }
                }
                // @formatter:on
            }
        }
    } catch (IOException ex) {
        log.error("Cannot get first non-loopback address", ex);
    }

    if (result != null) {
        return result;
    }

    try {
        return InetAddress.getLocalHost();
    } catch (UnknownHostException e) {
        log.warn("Unable to retrieve localhost");
    }

    return null;
}