Hosts { public static Map<String, Set<InetAddress>> getNetworkInterfaceAddresses(boolean includeLoopback) { Map<String, Set<InetAddress>> interfaceAddressMap = new TreeMap<>(); try { Enumeration ifaces = NetworkInterface.getNetworkInterfaces(); while (ifaces.hasMoreElements()) { NetworkInterface iface = (NetworkInterface) ifaces.nextElement(); if (!iface.isUp()) { continue; } if (!includeLoopback && iface.isLoopback()) { continue; } String name = iface.getName(); Enumeration<InetAddress> ifaceAdresses = iface.getInetAddresses(); while (ifaceAdresses.hasMoreElements()) { InetAddress ia = ifaceAdresses.nextElement(); if (!includeLoopback && ia.isLoopbackAddress()) { continue; } if (!ia.getHostAddress().contains(":") && !(name != null && name.toLowerCase().contains("docker"))) { Set<InetAddress> addresses = interfaceAddressMap.get(name); if (addresses == null) { addresses = new LinkedHashSet<>(); } addresses.add(ia); interfaceAddressMap.put(name, addresses); } } } } catch (SocketException ex) { } return interfaceAddressMap; } private  Hosts(); static Map<String, Set<InetAddress>> getNetworkInterfaceAddresses(boolean includeLoopback); static Set<InetAddress> getAddresses(); static String getLocalHostName(); static String getLocalIp(); static final String PREFERED_ADDRESS_PROPERTY_NAME; }
@Test public void getNetworkInterfaceAddresses() throws Exception { Map<String, Set<InetAddress>> includeLoopback = Hosts.getNetworkInterfaceAddresses(true); assertFalse(includeLoopback.isEmpty()); assertLoopback("Should include loopback", true, includeLoopback); Map<String, Set<InetAddress>> noLoopback = Hosts.getNetworkInterfaceAddresses(false); assertFalse(noLoopback.isEmpty()); assertLoopback("Should not include loopback", false, noLoopback); }