Refine search
/** * Try to get the primary {@link InetAddress} of the primary network interface with * fallback to the local loopback address (usually {@code 127.0.0.1} or {@code ::1}. * * @return The primary {@link InetAddress} of the primary network interface * or the loopback address as fallback. * @throws SocketException if the list of network interfaces couldn't be retrieved */ public static InetAddress guessPrimaryNetworkAddress(boolean preferIPv4) throws SocketException { final Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); if (interfaces != null) { for (NetworkInterface interf : Collections.list(interfaces)) { if (!interf.isLoopback() && interf.isUp()) { // Interface is not loopback and up. Try to get the first address. for (InetAddress addr : Collections.list(interf.getInetAddresses())) { if (preferIPv4 && addr instanceof Inet4Address) { return addr; } if (!preferIPv4 && addr instanceof Inet6Address) { return addr; } } } } } return InetAddress.getLoopbackAddress(); }
private static List<NetworkInterface> getGoodNetworkInterfaces() { ImmutableList.Builder<NetworkInterface> builder = ImmutableList.builder(); try { for (NetworkInterface networkInterface : Collections.list(NetworkInterface.getNetworkInterfaces())) { try { if (!networkInterface.isLoopback() && networkInterface.isUp()) { builder.add(networkInterface); } } catch (Exception ignored) { } } } catch (SocketException ignored) { } return builder.build(); }
/** * @return The MAC hardware address of the first network interface of this host. */ public static byte[] getFirstNetworkInterfaceHardwareAddress() { try { Enumeration<NetworkInterface> interfaceEnumeration = NetworkInterface.getNetworkInterfaces(); for (NetworkInterface iface : Collections.list(interfaceEnumeration)) { if (!iface.isLoopback() && iface.isUp() && iface.getHardwareAddress() != null) { return iface.getHardwareAddress(); } } } catch (Exception ex) { throw new RuntimeException("Could not discover first network interface hardware address"); } throw new RuntimeException("Could not discover first network interface hardware address"); }
@CheckForNull private static byte[] getMacAddress() throws SocketException { Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); if (en != null) { while (en.hasMoreElements()) { NetworkInterface nint = en.nextElement(); if (!nint.isLoopback()) { // Pick the first valid non loopback address we find byte[] address = nint.getHardwareAddress(); if (isValidAddress(address)) { return address; } } } } // Could not find a mac address return null; }
public static String findAvailableHostAddress() throws UnknownHostException, SocketException { InetAddress address = InetAddress.getLocalHost(); if (address.isLoopbackAddress()) { for (NetworkInterface networkInterface : Collections .list(NetworkInterface.getNetworkInterfaces())) { if (!networkInterface.isLoopback()) { for (InterfaceAddress interfaceAddress : networkInterface.getInterfaceAddresses()) { InetAddress a = interfaceAddress.getAddress(); if (a instanceof Inet4Address) { return a.getHostAddress(); } } } } } return address.getHostAddress(); }
Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); List<NetworkInterface> nis = interfaces == null ? Collections.<NetworkInterface>emptyList() : Collections.list(interfaces); List<InetAddress> addresses = new ArrayList<InetAddress>(); if (ni.isUp() && !ni.isLoopback()) { addresses.addAll(Collections.list(ni.getInetAddresses()));
String ip; try { Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); while (interfaces.hasMoreElements()) { NetworkInterface iface = interfaces.nextElement(); // filters out 127.0.0.1 and inactive interfaces if (iface.isLoopback() || !iface.isUp()) continue; Enumeration<InetAddress> addresses = iface.getInetAddresses(); while(addresses.hasMoreElements()) { InetAddress addr = addresses.nextElement(); ip = addr.getHostAddress(); System.out.println(iface.getDisplayName() + " " + ip); } } } catch (SocketException e) { throw new RuntimeException(e); }
private static InetAddress getIpAddress(AddressSelectionCondition condition) throws SocketException { // Before we connect somewhere, we cannot be sure about what we'd be bound to; however, // we only connect when the message where client ID is, is long constructed. Thus, // just use whichever IP address we can find. Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); while (interfaces.hasMoreElements()) { NetworkInterface current = interfaces.nextElement(); if (!current.isUp() || current.isLoopback() || current.isVirtual()) continue; Enumeration<InetAddress> addresses = current.getInetAddresses(); while (addresses.hasMoreElements()) { InetAddress addr = addresses.nextElement(); if (addr.isLoopbackAddress()) continue; if (condition.isAcceptableAddress(addr)) { return addr; } } } throw new SocketException("Can't get our ip address, interfaces are: " + interfaces); }
/** * Retrieve the first validated local ip address(the Public and LAN ip addresses are validated). * * @return the local address * @throws SocketException the socket exception */ public static InetAddress getLocalInetAddress() throws SocketException { // enumerates all network interfaces Enumeration<NetworkInterface> enu = NetworkInterface.getNetworkInterfaces(); while (enu.hasMoreElements()) { NetworkInterface ni = enu.nextElement(); if (ni.isLoopback()) { continue; } Enumeration<InetAddress> addressEnumeration = ni.getInetAddresses(); while (addressEnumeration.hasMoreElements()) { InetAddress address = addressEnumeration.nextElement(); // ignores all invalidated addresses if (address.isLinkLocalAddress() || address.isLoopbackAddress() || address.isAnyLocalAddress()) { continue; } return address; } } throw new RuntimeException("No validated local address!"); }
/** * Gets a list of all local non-loopback IPs known to this JVM. * Note that this will include both IPv4 and IPv6 addresses (even if one "resolves" * into another). Loopbacks will be skipped. * * @return List of all known local IPs (empty list if no addresses available). */ public static synchronized Collection<String> allLocalIps() { List<String> ips = new ArrayList<>(4); try { Enumeration<NetworkInterface> itfs = NetworkInterface.getNetworkInterfaces(); if (itfs != null) { for (NetworkInterface itf : asIterable(itfs)) { if (!itf.isLoopback()) { Enumeration<InetAddress> addrs = itf.getInetAddresses(); for (InetAddress addr : asIterable(addrs)) { String hostAddr = addr.getHostAddress(); if (!addr.isLoopbackAddress() && !ips.contains(hostAddr)) ips.add(hostAddr); } } } } } catch (SocketException ignore) { return Collections.emptyList(); } Collections.sort(ips); return ips; }
final Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces(); while ( networkInterfaces.hasMoreElements() ) if ( networkInterface.isLoopback() )
Enumeration ifaces = NetworkInterface.getNetworkInterfaces(); while (ifaces.hasMoreElements()) { NetworkInterface iface = (NetworkInterface) ifaces.nextElement(); continue; if (!includeLoopback && iface.isLoopback()) { continue;
Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); if (interfaces != null) { while (interfaces.hasMoreElements()) { try { NetworkInterface network = interfaces.nextElement(); if (network.isLoopback() || network.isVirtual() || !network.isUp()) { continue;
Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); if (interfaces != null) { while (interfaces.hasMoreElements()) { try { NetworkInterface network = interfaces.nextElement(); if (network.isLoopback() || network.isVirtual() || !network.isUp()) { continue;
Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); if (interfaces != null) { while (interfaces.hasMoreElements()) { try { NetworkInterface network = interfaces.nextElement(); if (network.isLoopback() || network.isVirtual() || !network.isUp()) { continue;
Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); while (interfaces.hasMoreElements()) { NetworkInterface ifc = interfaces.nextElement(); if (!ifc.isLoopback()) { for (InterfaceAddress ifadr : ifc.getInterfaceAddresses()) { InetAddress adr = ifadr.getAddress();
String bindAddress = null; Enumeration<NetworkInterface> intfs = NetworkInterface.getNetworkInterfaces(); if (i.isLoopback()) { Enumeration<InetAddress> addrs = i.getInetAddresses(); while (addrs.hasMoreElements()) {
/** * @return The MAC hardware address of the first network interface of this host. */ public static byte[] getFirstNetworkInterfaceHardwareAddress() { try { Enumeration<NetworkInterface> interfaceEnumeration = NetworkInterface.getNetworkInterfaces(); for (NetworkInterface iface : Collections.list(interfaceEnumeration)) { if (!iface.isLoopback() && iface.isUp() && iface.getHardwareAddress() != null) { return iface.getHardwareAddress(); } } } catch (Exception ex) { throw new RuntimeException("Could not discover first network interface hardware address"); } throw new RuntimeException("Could not discover first network interface hardware address"); }
/** * Find hardware ID. */ private static byte[] getHardwareId() throws SocketException { final Enumeration<NetworkInterface> e = NetworkInterface.getNetworkInterfaces(); while (e.hasMoreElements()) { NetworkInterface network = e.nextElement(); try { logger.trace("Probing interface {}", network); if (!network.isLoopback()) { byte[] addr = network.getHardwareAddress(); if (addr != null) { logger.debug("Using interface {}", network); return addr; } } } catch (SocketException ex) { logger.debug("Skipping {}", network, ex); } } return null; }
private static byte[] getMacAddress() throws SocketException { Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); if (en != null) { while (en.hasMoreElements()) { NetworkInterface nint = en.nextElement(); if (!nint.isLoopback()) { // Pick the first valid non loopback address we find byte[] address = nint.getHardwareAddress(); if (isValidAddress(address)) { return address; } } } } // Could not find a mac address return null; }