static boolean isValidV4Address(InetAddress address) { if (address == null || address.isLoopbackAddress()) { return false; } String name = address.getHostAddress(); return (name != null && IP_PATTERN.matcher(name).matches() && !Constants.ANYHOST_VALUE.equals(name) && !Constants.LOCALHOST_VALUE.equals(name)); }
/** * 检查有否非ipv6,非127.0.0.1的inetaddress */ private static InetAddress findAvailableInetAddress(NetworkInterface nic) { for (Enumeration<InetAddress> indetAddresses = nic.getInetAddresses(); indetAddresses.hasMoreElements();) { InetAddress inetAddress = indetAddresses.nextElement(); if (!(inetAddress instanceof Inet6Address) && !inetAddress.isLoopbackAddress()) { return inetAddress; } } return null; } }
public static String getIPAddress(boolean useIPv4) { try { List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces()); for (NetworkInterface intf : interfaces) { List<InetAddress> addrs = Collections.list(intf.getInetAddresses()); for (InetAddress addr : addrs) { if (!addr.isLoopbackAddress()) { String sAddr = addr.getHostAddress(); //boolean isIPv4 = InetAddressUtils.isIPv4Address(sAddr); boolean isIPv4 = sAddr.indexOf(':')<0; if (useIPv4) { if (isIPv4) return sAddr; } else { if (!isIPv4) { int delim = sAddr.indexOf('%'); // drop ip6 zone suffix return delim<0 ? sAddr.toUpperCase() : sAddr.substring(0, delim).toUpperCase(); } } } } } } catch (Exception ex) { } return ""; }
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(); }
List<NetworkInterface> itfs = new ArrayList<>(); for (NetworkInterface itf : asIterable(NetworkInterface.getNetworkInterfaces())) itfs.add(itf); boolean found = false; for (InetAddress addr : asIterable(itf.getInetAddresses())) { if (!addr.isLoopbackAddress() && !addr.isLinkLocalAddress() && reachable(itf, addr, reachTimeout)) { locHost = addr;
/** Returns all global scope addresses for interfaces that are up. */ static InetAddress[] getGlobalInterfaces() throws SocketException { List<InetAddress> list = new ArrayList<InetAddress> (); for (NetworkInterface intf : getInterfaces()) { if (intf.isUp()) { for (InetAddress address : Collections.list(intf.getInetAddresses())) { if (address.isLoopbackAddress() == false && address.isSiteLocalAddress() == false && address.isLinkLocalAddress() == false) { list.add(address); } } } } return list.toArray(new InetAddress[list.size()]); } }
public static String getFirstNoLoopbackIPAddresses() throws SocketException { Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces(); InetAddress localAddress = null; while (networkInterfaces.hasMoreElements()) { NetworkInterface networkInterface = networkInterfaces.nextElement(); Enumeration<InetAddress> inetAddresses = networkInterface.getInetAddresses(); while (inetAddresses.hasMoreElements()) { InetAddress address = inetAddresses.nextElement(); if (!address.isLoopbackAddress() && !Inet6Address.class.isInstance(address)) { return address.getHostAddress(); } else if (!address.isLoopbackAddress()) { localAddress = address; } } } return localAddress.getHostAddress(); }
static boolean isValidV4Address(InetAddress address) { if (address == null || address.isLoopbackAddress()) { return false; } String name = address.getHostAddress(); return (name != null && IP_PATTERN.matcher(name).matches() && !Constants.ANYHOST_VALUE.equals(name) && !Constants.LOCALHOST_VALUE.equals(name)); }
List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces()); for (NetworkInterface intf : interfaces) { if (interfaceName != null) { List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces()); for (NetworkInterface intf : interfaces) { List<InetAddress> addrs = Collections.list(intf.getInetAddresses()); for (InetAddress addr : addrs) { if (!addr.isLoopbackAddress()) { String sAddr = addr.getHostAddress();
private static boolean isLocalhostWithNonLoopbackIpAddress(String ipAddress) { for (NetworkInterface networkInterface : localInterfaces) { Enumeration<InetAddress> inetAddressEnumeration = networkInterface.getInetAddresses(); while (inetAddressEnumeration.hasMoreElements()) { InetAddress address = inetAddressEnumeration.nextElement(); if (!address.isLoopbackAddress() && ipAddress.equalsIgnoreCase(address.getHostAddress())) { return true; } } } return false; }
private static boolean isValidHostAddress(InetAddress address) { if (address == null || address.isLoopbackAddress()) return false; String name = address.getHostAddress(); return (name != null && !EMPTY_IP.equals(name) && !LOCALHOST_IP.equals(name) && IP_PATTERN.matcher(name) .matches()); }
/** * 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; }
public static InetAddress getLocalIPAddress() { try { Enumeration<?> netInterfaces = NetworkInterface.getNetworkInterfaces(); InetAddress inetAddress = null; while (netInterfaces.hasMoreElements()) { NetworkInterface ni = (NetworkInterface) netInterfaces.nextElement(); Enumeration<?> e2 = ni.getInetAddresses(); while (e2.hasMoreElements()) { inetAddress = (InetAddress) e2.nextElement(); if (!inetAddress.isLoopbackAddress() && !inetAddress.getHostAddress().contains(":")) { return inetAddress; } } } } catch (Exception e) { LOG.error("getLocalIP error", e); } return null; } }
public static boolean isValidAddress(InetAddress address) { if (address == null || address.isLoopbackAddress()) return false; String name = address.getHostAddress(); return (name != null && !ANYHOST.equals(name) && !LOCALHOST.equals(name) && IP_PATTERN.matcher(name).matches()); } //return ip to avoid lookup dns
private static IPInfo getIPInfo() { try { List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces()); for (NetworkInterface intf : interfaces) { List<InetAddress> addrs = Collections.list(intf.getInetAddresses()); for (InetAddress addr : addrs) { if (!addr.isLoopbackAddress()) { String sAddr = addr.getHostAddress().toUpperCase(); boolean isIPv4 = isIPv4Address(sAddr); if (isIPv4) { IPInfo info = new IPInfo(); info.addr = addr; info.intf = intf; info.ip = sAddr; info.ip_hex = InetAddress_to_hex(addr); info.netmask_hex = netmask_to_hex(intf.getInterfaceAddresses().get(0).getNetworkPrefixLength()); return info; } } } } } catch (SocketException e) { e.printStackTrace(); } return null; }
private static void resolveHost() throws Exception { InetAddress addr = InetAddress.getLocalHost(); hostName = addr.getHostName(); ip = addr.getHostAddress(); if (addr.isLoopbackAddress()) { // find the first IPv4 Address that not loopback Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); while (interfaces.hasMoreElements()) { NetworkInterface in = interfaces.nextElement(); Enumeration<InetAddress> addrs = in.getInetAddresses(); while (addrs.hasMoreElements()) { InetAddress address = addrs.nextElement(); if (!address.isLoopbackAddress() && address instanceof Inet4Address) { ip = address.getHostAddress(); } } } } }
public HTTPSession(NanoHTTPD httpd, ITempFileManager tempFileManager, InputStream inputStream, OutputStream outputStream, InetAddress inetAddress) { this.httpd = httpd; this.tempFileManager = tempFileManager; this.inputStream = new BufferedInputStream(inputStream, HTTPSession.BUFSIZE); this.outputStream = outputStream; this.remoteIp = inetAddress.isLoopbackAddress() || inetAddress.isAnyLocalAddress() ? "127.0.0.1" : inetAddress.getHostAddress().toString(); this.remoteHostname = inetAddress.isLoopbackAddress() || inetAddress.isAnyLocalAddress() ? "localhost" : inetAddress.getHostName().toString(); this.headers = new HashMap<String, String>(); }
public List<String> call() throws IOException { List<String> names = new ArrayList<String>(); Enumeration<NetworkInterface> nis = NetworkInterface.getNetworkInterfaces(); while (nis.hasMoreElements()) { NetworkInterface ni = nis.nextElement(); LOGGER.log(Level.FINE, "Listing up IP addresses for {0}", ni.getDisplayName()); Enumeration<InetAddress> e = ni.getInetAddresses(); while (e.hasMoreElements()) { InetAddress ia = e.nextElement(); if(ia.isLoopbackAddress()) { LOGGER.log(Level.FINE, "{0} is a loopback address", ia); continue; } if(!(ia instanceof Inet4Address)) { LOGGER.log(Level.FINE, "{0} is not an IPv4 address", ia); continue; } LOGGER.log(Level.FINE, "{0} is a viable candidate", ia); names.add(ia.getHostAddress()); } } return names; } private static final long serialVersionUID = 1L;
private static boolean isLocalhostWithLoopbackIpAddress(String forAddress, final String ipAddress) { InetAddress[] allMatchingAddresses; try { allMatchingAddresses = InetAddress.getAllByName(forAddress); } catch (UnknownHostException e) { return false; } for (InetAddress inetAddress : allMatchingAddresses) { if (inetAddress.isLoopbackAddress() && inetAddress.getHostAddress().equals(ipAddress)) { return true; } } return false; }
public static String getFirstLocalNonLoopbackIpAddress() { SortedSet<String> addresses = new TreeSet<>(); for (NetworkInterface networkInterface : localInterfaces) { Enumeration<InetAddress> inetAddressEnumeration = networkInterface.getInetAddresses(); while (inetAddressEnumeration.hasMoreElements()) { InetAddress address = inetAddressEnumeration.nextElement(); if (!address.isLoopbackAddress() && !address.getHostAddress().contains(":")) { addresses.add(address.getHostAddress()); } } } if (addresses.isEmpty()) { throw new RuntimeException("Failed to get non-loopback local ip address!"); } return addresses.first(); }