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 List<NetworkInterface> getAllAvailableInterfaces() throws SocketException { List<NetworkInterface> retval=new ArrayList<>(10); NetworkInterface intf; for(Enumeration en=NetworkInterface.getNetworkInterfaces(); en.hasMoreElements(); ) { intf=(NetworkInterface)en.nextElement(); retval.add(intf); } return retval; }
@Override public Optional<InetAddress> getLocalInetAddress(Predicate<InetAddress> predicate) { try { return Collections.list(NetworkInterface.getNetworkInterfaces()).stream() .flatMap(ni -> Collections.list(ni.getInetAddresses()).stream()) .filter(a -> a.getHostAddress() != null) .filter(predicate) .findFirst(); } catch (SocketException e) { throw new IllegalStateException("Can not retrieve network interfaces", e); } } }
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 ""; }
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;
List<InetAddress> addrList = new ArrayList<InetAddress>(); for(NetworkInterface ifc : NetworkInterface.getNetworkInterfaces()) { if(ifc.isUp()) { for(InetAddress addr : ifc.getInetAddresses()) { addrList.add(addr); } } }
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(); }
public static NetworkInterface findInterfaceWithScopeId(String scopeName) { final Enumeration<NetworkInterface> enumeration; try { enumeration = NetworkInterface.getNetworkInterfaces(); } catch (SocketException ignored) { return null; } while (enumeration.hasMoreElements()) { final NetworkInterface net = enumeration.nextElement(); if (net.getName().equals(scopeName)) { return net; } } return null; }
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();
public static void main(String[] args) throws UnknownHostException, SocketException { System.out.println("Current IP address : " + InetAddress.getLocalHost().getHostAddress()); for(NetworkInterface network : IterableEnumeration.make(NetworkInterface.getNetworkInterfaces())) { byte[] mac = network.getHardwareAddress(); if(mac != null) { System.out.print("Current MAC address : "); StringBuilder sb = new StringBuilder(); for (int i = 0; i < mac.length; i++) { sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : "")); } System.out.println(sb.toString()); //Bound InetAddress for interface for(InetAddress address : IterableEnumeration.make(network.getInetAddresses())) { System.out.println("\tBound to:"+address.getHostAddress()); } } } }
Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); List<NetworkInterface> nis = interfaces == null ? Collections.<NetworkInterface>emptyList() : Collections.list(interfaces); List<InetAddress> addresses = new ArrayList<InetAddress>(); for (NetworkInterface ni : nis) { if (ni.isUp() && !ni.isLoopback()) { addresses.addAll(Collections.list(ni.getInetAddresses()));
public static List<String> getLocalInetAddress() { List<String> inetAddressList = new ArrayList<String>(); try { Enumeration<NetworkInterface> enumeration = NetworkInterface.getNetworkInterfaces(); while (enumeration.hasMoreElements()) { NetworkInterface networkInterface = enumeration.nextElement(); Enumeration<InetAddress> addrs = networkInterface.getInetAddresses(); while (addrs.hasMoreElements()) { inetAddressList.add(addrs.nextElement().getHostAddress()); } } } catch (SocketException e) { throw new RuntimeException("get local inet address fail", e); } return inetAddressList; }
private static List<NetworkInterface> getAllNetworkInterfaces() { Enumeration<NetworkInterface> networkInterfaces; try { networkInterfaces = NetworkInterface.getNetworkInterfaces(); if (networkInterfaces == null) { return Collections.emptyList(); } List<NetworkInterface> ifaces = new ArrayList<>(); while (networkInterfaces.hasMoreElements()) { ifaces.add(networkInterfaces.nextElement()); } return ifaces; } catch (SocketException e) { return Collections.emptyList(); } }
/** * 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; }
/** * Private constructor. */ private Socks5Proxy() { this.serverProcess = new Socks5ServerProcess(); Enumeration<NetworkInterface> networkInterfaces; try { networkInterfaces = NetworkInterface.getNetworkInterfaces(); } catch (SocketException e) { throw new IllegalStateException(e); } Set<String> localHostAddresses = new HashSet<>(); for (NetworkInterface networkInterface : Collections.list(networkInterfaces)) { // We can't use NetworkInterface.getInterfaceAddresses here, which // would return a List instead the deprecated Enumeration, because // it's Android API 9 and Smack currently uses 8. Change that when // we raise Smack's minimum Android API. Enumeration<InetAddress> inetAddresses = networkInterface.getInetAddresses(); for (InetAddress address : Collections.list(inetAddresses)) { localHostAddresses.add(address.getHostAddress()); } } if (localHostAddresses.isEmpty()) { throw new IllegalStateException("Could not determine any local host address"); } replaceLocalAddresses(localHostAddresses); }
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(); } } } } }
private static String getMacAddress() throws IOException { final Enumeration<NetworkInterface> net = NetworkInterface.getNetworkInterfaces(); final StringBuilder result = new StringBuilder(); while (net.hasMoreElements()) { final NetworkInterface element = net.nextElement(); byte[] mac = element.getHardwareAddress(); if (mac != null) { for (byte b : mac) { result.append(String.format("%02x", b)); } } } return result.toString(); }
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; }
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;
Enumeration<NetworkInterface> nis = NetworkInterface.getNetworkInterfaces(); while (nis.hasMoreElements()) { NetworkInterface ni = nis.nextElement(); System.out.println(ni.getName() + " " + ni.getDisplayName()); }