List of utility methods to do Network Interface Check
void | addAllInterfaces(List Helper for getInterfaces, recursively adds subinterfaces to target if (!level.isEmpty()) { target.addAll(level); for (NetworkInterface intf : level) { addAllInterfaces(target, Collections.list(intf.getSubInterfaces())); |
Boolean | checkMethod(NetworkInterface iface, Method toCheck) check Method if (toCheck != null) { try { return (Boolean) toCheck.invoke(iface, (Object[]) null); } catch (IllegalAccessException e) { return false; } catch (InvocationTargetException e) { return false; return true; |
Enumeration | cloneInterfaces(List Method cloning list of strings (used for network interfaces) return Collections.enumeration(new ArrayList<>(interfaces)); |
String | describeInterface(NetworkInterface subIf, boolean subinterface) describe Interface StringBuilder description = new StringBuilder(); String padding = ""; if (subinterface) { description.append("\t\tSUBINTERFACE"); description.append("\t\t____________"); padding = "\t\t"; description.append(String.format("%sInterface Display name: %s\n", padding, subIf.getDisplayName())); ... |
String | dumpLocalNetworkInfo() Get the local network info as a string StringBuffer buffer = new StringBuffer(); InetAddress addr = InetAddress.getLocalHost(); buffer.append("Localhost: " + getAddrInfo(addr) + "\n"); Enumeration<NetworkInterface> nifs = NetworkInterface.getNetworkInterfaces(); buffer.append("Network interfaces:\n"); while (nifs.hasMoreElements()) { NetworkInterface nif = nifs.nextElement(); buffer.append(" - " + getNetworkInterfaceInfo(nif) + "\n"); ... |
boolean | hasNetworkAccess() has Network Access try { final InetAddress address = InetAddress.getByName("www.google.com"); return true; } catch (Throwable ignored) { return false; |
void | isCIDR(String network) Verify if CIDR string is a valid CIDR address. String[] hostMask = network.split("/"); if (hostMask.length != 2) { throw new IllegalArgumentException("subnetAddress is not a CIDR"); isValidInetAddress(hostMask[0]); if (Integer.parseUnsignedInt(hostMask[1]) > 32) { throw new IllegalArgumentException("CIDR mask may not be larger than 32"); |
boolean | isHostInNetworkCard(String host) is Host In Network Card try { InetAddress address = InetAddress.getByName(host); NetworkInterface networkInterface = NetworkInterface.getByInetAddress(address); return networkInterface != null; } catch (Exception e) { return false; |
boolean | isInterfaceLoopback(NetworkInterface iface) Determines whether or not the iface interface is a loopback interface. try { Method method = iface.getClass().getMethod("isLoopback"); return ((Boolean) method.invoke(iface, new Object[] {})).booleanValue(); } catch (Throwable t) { Enumeration<InetAddress> addresses = iface.getInetAddresses(); return addresses.hasMoreElements() && addresses.nextElement().isLoopbackAddress(); |
boolean | isInterfaceUp(NetworkInterface iface) Determines, if possible, whether or not the iface interface is up. try { Method method = iface.getClass().getMethod("isUp"); return ((Boolean) method.invoke(iface)).booleanValue(); } catch (Throwable t) { return true; |