Java Utililty Methods Network Interface Check

List of utility methods to do Network Interface Check

Description

The list of methods to do Network Interface Check are organized into topic(s).

Method

voidaddAllInterfaces(List target, List level)
Helper for getInterfaces, recursively adds subinterfaces to target
if (!level.isEmpty()) {
    target.addAll(level);
    for (NetworkInterface intf : level) {
        addAllInterfaces(target, Collections.list(intf.getSubInterfaces()));
BooleancheckMethod(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;
EnumerationcloneInterfaces(List interfaces)
Method cloning list of strings (used for network interfaces)
return Collections.enumeration(new ArrayList<>(interfaces));
StringdescribeInterface(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()));
...
StringdumpLocalNetworkInfo()
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");
...
booleanhasNetworkAccess()
has Network Access
try {
    final InetAddress address = InetAddress.getByName("www.google.com");
    return true;
} catch (Throwable ignored) {
return false;
voidisCIDR(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");
booleanisHostInNetworkCard(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;
booleanisInterfaceLoopback(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();
booleanisInterfaceUp(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;