Java Network Interface Get getInterfaceDiagnostic()

Here you can find the source of getInterfaceDiagnostic()

Description

It provides a complete description of all available network interfaces in the system as well as the configured INET Addresses.

License

Apache License

Exception

Parameter Description
SocketException an exception

Declaration

public static String getInterfaceDiagnostic() throws SocketException 

Method Source Code

//package com.java2s;
/*//  ww  w  . java  2  s. c  om
 * Copyright 2016 tamas.csaba@gmail.com.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.net.InetAddress;

import java.net.NetworkInterface;

import java.net.SocketException;

import java.util.Arrays;
import java.util.Collections;
import java.util.Enumeration;

public class Main {
    /**
     * It provides a complete description of all available network interfaces in
     * the system as well as the configured INET Addresses.
     *
     * @return
     * @throws SocketException
     */
    public static String getInterfaceDiagnostic() throws SocketException {
        StringBuilder description = new StringBuilder();
        Enumeration<NetworkInterface> nets = NetworkInterface.getNetworkInterfaces();

        for (NetworkInterface netIf : Collections.list(nets)) {
            description.append(describeInterface(netIf, false));
            description.append(displaySubInterfaces(netIf));
            description.append("\n");
        }
        return description.toString();
    }

    public static String describeInterface(NetworkInterface subIf, boolean subinterface) throws SocketException {
        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()));
        description.append(String.format("%sInterface Name: %s\n", padding, subIf.getName()));
        Enumeration<InetAddress> inetAddresses = subIf.getInetAddresses();
        for (InetAddress inetAddress : Collections.list(inetAddresses)) {
            description.append(String.format("%s\tInetAddress: %s\n", padding, inetAddress));
        }
        description.append(String.format("%s\t\tUp? %s\n", padding, subIf.isUp()));
        description.append(String.format("%s\t\tLoopback? %s\n", padding, subIf.isLoopback()));
        description.append(String.format("%s\t\tPointToPoint? %s\n", padding, subIf.isPointToPoint()));
        description.append(String.format("%s\t\tSupports multicast? %s\n", padding, subIf.supportsMulticast()));
        description.append(String.format("%s\t\tVirtual? %s\n", padding, subIf.isVirtual()));
        description.append(String.format("%s\t\tHardware address: %s\n", padding,
                Arrays.toString(subIf.getHardwareAddress())));
        description.append(String.format("%s\t\tMTU: %s\n", padding, subIf.getMTU()));
        description.append("\n");
        return description.toString();
    }

    public static String displaySubInterfaces(NetworkInterface netIf) throws SocketException {
        StringBuilder sb = new StringBuilder();
        Enumeration<NetworkInterface> subIfs = netIf.getSubInterfaces();
        for (NetworkInterface subIf : Collections.list(subIfs)) {
            sb.append(describeInterface(subIf, true));
        }
        return sb.toString();
    }
}

Related

  1. getGlobalInterfaces()
  2. getGoodNetworkInterfaces()
  3. getHostnameFromNetworkInterface()
  4. getInterface(String ifaceName)
  5. getInterfaceByName(String interfaceName)
  6. getInterfaceInfo(NetworkInterface nif)
  7. getInterfaces()
  8. getInterfaces()
  9. getInterfaces()