Java Network Interface Check dumpLocalNetworkInfo()

Here you can find the source of dumpLocalNetworkInfo()

Description

Get the local network info as a string

License

Apache License

Exception

Parameter Description
UnknownHostException an exception
SocketException an exception

Return

return a description of the current network setup of the local host.

Declaration

public static String dumpLocalNetworkInfo() throws UnknownHostException, SocketException 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.lang.reflect.Method;
import java.net.*;
import java.util.*;

public class Main {
    private static Method isUp;
    private static Method supportsMulticast;

    /**/*  w ww  .j a v a 2s  .c om*/
     * Get the local network info as a string
     *
     * @return return a description of the current network setup of the local host.
     * @throws UnknownHostException
     * @throws SocketException
     */
    public static String dumpLocalNetworkInfo() throws UnknownHostException, SocketException {
        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");
            Enumeration<InetAddress> addresses = nif.getInetAddresses();
            while (addresses.hasMoreElements()) {
                addr = addresses.nextElement();
                buffer.append("    " + getAddrInfo(addr) + "\n");
            }
        }
        return buffer.toString();
    }

    private static String getAddrInfo(InetAddress pAddr) throws SocketException {
        String ret = pAddr.getHostName() != null ? pAddr.getHostName() + " (" + pAddr.getHostAddress() + ")"
                : pAddr.getHostAddress();
        ret += " [site-local: " + pAddr.isSiteLocalAddress() + ", link-local: " + pAddr.isLinkLocalAddress()
                + ", lb: " + pAddr.isLoopbackAddress() + "]";
        NetworkInterface nif = NetworkInterface.getByInetAddress(pAddr);
        ret += " -- nif: " + getNetworkInterfaceInfo(nif);
        return ret;
    }

    private static String getNetworkInterfaceInfo(NetworkInterface pNif) throws SocketException {
        if (pNif == null) {
            return "[null]";
        }
        return pNif.getDisplayName() + " [up: " + pNif.isUp() + ", mc: " + pNif.supportsMulticast() + ", lb: "
                + pNif.isLoopback() + ", hw: " + formatHwAddress(pNif.getHardwareAddress()) + "]";
    }

    private static String formatHwAddress(byte[] pHardwareAddress) {
        if (pHardwareAddress == null) {
            return "[none]";
        }
        StringBuilder sb = new StringBuilder(18);
        for (byte b : pHardwareAddress) {
            if (sb.length() > 0) {
                sb.append(':');
            }
            sb.append(String.format("%02x", b));
        }
        return sb.toString();
    }
}

Related

  1. addAllInterfaces(List target, List level)
  2. checkMethod(NetworkInterface iface, Method toCheck)
  3. cloneInterfaces( List interfaces)
  4. describeInterface(NetworkInterface subIf, boolean subinterface)
  5. hasNetworkAccess()
  6. isCIDR(String network)
  7. isHostInNetworkCard(String host)
  8. isInterfaceLoopback(NetworkInterface iface)