Example usage for java.net NetworkInterface getName

List of usage examples for java.net NetworkInterface getName

Introduction

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

Prototype

public String getName() 

Source Link

Document

Get the name of this network interface.

Usage

From source file:ru.asmsoft.p2p.incoming.LocalAddressesFilteringService.java

@PostConstruct
public void init() {
    try {/* www.j av  a2  s . c o  m*/
        logger.info("Full list of Network Interfaces:");
        for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en
                .hasMoreElements();) {
            NetworkInterface intf = en.nextElement();
            logger.info("    " + intf.getName() + " " + intf.getDisplayName());
            for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
                String ip = enumIpAddr.nextElement().toString().replace("/", "");
                logger.info("        " + ip);
                localIpAddresses.add(ip);
            }
        }
    } catch (SocketException e) {
        logger.info(" (error retrieving network interface list)");
    }
}

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

/**
 * ?(eth0, eth1..)??/*  ww w.ja  v  a  2 s .  c  o  m*/
 */
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:at.tugraz.ist.akm.networkInterface.WifiIpAddress.java

private String readIP4AddressOfEmulator() throws SocketException {
    String inet4Address = "0.0.0.0";

    for (Enumeration<NetworkInterface> iter = NetworkInterface.getNetworkInterfaces(); iter
            .hasMoreElements();) {/*  w ww.j a va2s  .c  om*/
        NetworkInterface nic = iter.nextElement();

        if (nic.getName().startsWith("eth")) {
            Enumeration<InetAddress> addresses = nic.getInetAddresses();
            addresses.nextElement(); // skip first
            if (addresses.hasMoreElements()) {
                InetAddress address = addresses.nextElement();

                String concreteAddressString = address.getHostAddress().toUpperCase(Locale.getDefault());
                if (InetAddressUtils.isIPv4Address(concreteAddressString)) {
                    inet4Address = concreteAddressString;
                }
            }
        }
    }
    return inet4Address;
}

From source file:at.tugraz.ist.akm.networkInterface.WifiIpAddress.java

public String readIp4ApAddress() {
    try {//from w w w .  j a v  a  2s  .  co  m
        for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en
                .hasMoreElements();) {
            NetworkInterface intf = en.nextElement();
            if (intf.getName().contains("wlan")) {
                for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr
                        .hasMoreElements();) {
                    InetAddress inetAddress = enumIpAddr.nextElement();
                    if (!inetAddress.isLoopbackAddress() && (inetAddress.getAddress().length == 4)) {
                        mLog.debug("found AP address [" + inetAddress.getHostAddress() + "]");
                        return inetAddress.getHostAddress();
                    }
                }
            }
        }
    } catch (SocketException ex) {
        mLog.debug("failed to read ip address in access point mode");
    }
    return null;
}

From source file:org.springframework.vault.authentication.MacAddressUserId.java

private NetworkInterface getNetworkInterface(String hint, List<NetworkInterface> interfaces) {

    for (NetworkInterface anInterface : interfaces) {
        if (hint.equals(anInterface.getDisplayName()) || hint.equals(anInterface.getName())) {
            return anInterface;
        }/*  ww  w  . j  a va2s .  c  o m*/
    }

    return null;
}

From source file:io.tilt.minka.domain.NetworkShardIDImpl.java

private InetAddress findSiteAddress(final Config config) {
    InetAddress fallback = null;/*from  ww  w .  j a va2 s .  co  m*/
    try {
        final String niName = config.getFollowerUseNetworkInterfase();
        logger.info("{}: Looking for configured network interfase: {}", getClass().getSimpleName(), niName);
        final Enumeration<NetworkInterface> nis = NetworkInterface.getNetworkInterfaces();
        while (nis.hasMoreElements()) {
            final NetworkInterface ni = nis.nextElement();
            if (niName.contains(ni.getName())) {
                logger.info("{}: Interfase found: {}, looking for a Site address..", getClass().getSimpleName(),
                        ni.getName());
                final Enumeration<InetAddress> ias = ni.getInetAddresses();
                while (ias.hasMoreElements()) {
                    final InetAddress ia = ias.nextElement();
                    if (ia.isSiteLocalAddress()) {
                        logger.info("{}: Host site address found: {}:{} with HostName {}",
                                getClass().getSimpleName(), ia.getHostAddress(), config.getBrokerServerPort(),
                                ia.getHostName());
                        return ia;
                    } else {
                        fallback = ia;
                        logger.warn("{}: Specified interfase: {} is not a site address!!, "
                                + "you should specify a non local-only valid interfase !! where's your lan?",
                                getClass().getSimpleName(), ia.getHostAddress());
                    }
                }
            }
        }
    } catch (Exception e) {
        logger.error("{}: Cannot build shard id value with hostname", getClass().getSimpleName(), e);
    }
    logger.error("{}: Site network interfase not found !", getClass().getSimpleName());
    return fallback;
}

From source file:org.inaetics.pubsub.demo.config.Configurator.java

private String getIp() {
    try {//from w w w .j a  v  a  2s  . co  m
        Enumeration<NetworkInterface> nEnumeration = NetworkInterface.getNetworkInterfaces();
        while (nEnumeration.hasMoreElements()) {
            NetworkInterface networkInterface = (NetworkInterface) nEnumeration.nextElement();
            if (networkInterface.getName().equals("eth1")) {
                Enumeration<InetAddress> addresses = networkInterface.getInetAddresses();
                while (addresses.hasMoreElements()) {
                    InetAddress inetAddress = (InetAddress) addresses.nextElement();
                    if (inetAddress instanceof Inet4Address) {
                        return inetAddress.getHostAddress();
                    }
                }
            }
        }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return "localhost";
}

From source file:org.powertac.tournament.services.TournamentProperties.java

private String getTourneyUrl() {
    if (!properties.getProperty("tourney.location", "").isEmpty()) {
        return properties.getProperty("tourney.location");
    }//www .  j av a 2  s.c o  m

    String tourneyUrl = "http://%s:8080/TournamentScheduler/";
    String address = "127.0.0.1";
    try {
        Enumeration<NetworkInterface> n = NetworkInterface.getNetworkInterfaces();
        while (n.hasMoreElements()) {
            NetworkInterface e = n.nextElement();
            if (e.getName().startsWith("lo")) {
                continue;
            }

            Enumeration<InetAddress> a = e.getInetAddresses();
            while (a.hasMoreElements()) {
                InetAddress addr = a.nextElement();
                if (addr.getClass().getName().equals("java.net.Inet4Address")) {
                    address = addr.getHostAddress();
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        messages.add("Error getting Tournament Location!");
    }

    return String.format(tourneyUrl, address);
}

From source file:org.apereo.portal.PortalInfoProviderImpl.java

protected Set<String> getNetworkInterfaceNames() {
    final Enumeration<NetworkInterface> networkInterfacesEnum;
    try {//from w  w w .j  a v a2 s. co  m
        networkInterfacesEnum = NetworkInterface.getNetworkInterfaces();
    } catch (SocketException e) {
        logger.warn("Failed to get list of available NetworkInterfaces.", e);
        return Collections.emptySet();
    }

    final Set<String> names = new LinkedHashSet<String>();
    while (networkInterfacesEnum.hasMoreElements()) {
        final NetworkInterface networkInterface = networkInterfacesEnum.nextElement();
        names.add(networkInterface.getName());
    }

    return names;
}

From source file:com.zz.cluster4spring.localinfo.DefaultLocalNetworkInfoProvider.java

public List<String> getLocalHostAddresses(InetAddressAcceptor aAcceptor) throws SocketException {
    List<String> result = new ArrayList<String>();
    Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
    while (interfaces.hasMoreElements()) {
        NetworkInterface networkInterface = interfaces.nextElement();
        if (aAcceptor.acceptNetworkInterface(networkInterface)) {
            if (fLog.isInfoEnabled()) {
                String name = networkInterface.getName();
                String displayName = networkInterface.getDisplayName();
                fLog.info(MessageFormat.format(
                        "Discovered NetworkInterface - ACCEPTED. Name:[{0}]. Display Name:[{1}]", name,
                        displayName));/*from  w  ww .j av a  2  s.  c o  m*/
            }
            collectInterfaceIPs(networkInterface, result, aAcceptor);
        } else if (fLog.isInfoEnabled()) {
            String name = networkInterface.getName();
            String displayName = networkInterface.getDisplayName();
            fLog.info(MessageFormat.format(
                    "Discovered NetworkInterface - SKIPPED. Name:[{0}]. Display Name:[{1}]", name,
                    displayName));
        }
    }
    return result;
}