import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import java.net.InetAddress;
import java.net.Inet4Address;
import java.net.Inet6Address;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;

import java.util.Enumeration;

public class ListNetworkInterfaces {

	public static String bytes2MAC(byte[] input) {
		StringBuffer sb = new StringBuffer(input.length * 2);
		for (int i = 0; i < input.length; i++) {
			String hex = Integer.toHexString(0x0100 + (input[i] & 0x00FF)).substring(1);
			sb.append((i > 0 ? ":" : "") + (hex.length() < 2 ? "0" : "") + hex);
		}
		return sb.toString().toUpperCase();
	}

	public static void main(String[] args) {
		try {
			InetAddress address = InetAddress.getLocalHost();
			System.out.println("IP address of the local host: " + address.getHostAddress());
		} catch (UnknownHostException ex) {
			System.err.println("IP address could not be found. Reason: " + ex.getMessage());
		}
		Enumeration<NetworkInterface> ifaces = null;
		try {
			ifaces = NetworkInterface.getNetworkInterfaces();
		} catch (SocketException ex) {
			System.err.println("Failed to fetch list of network interfaces. Reason: " + ex.getMessage());
		}
		if (ifaces != null) {
			if (ifaces.hasMoreElements()) {
				System.out.println("List of available network interfaces ...");
				Method getHardwareAddress = null;
				boolean methodTested = false;
				while (ifaces.hasMoreElements()) {
					NetworkInterface iface = ifaces.nextElement();
					System.out.println("network interface: " + iface.getDisplayName());
					if (!methodTested) {
						try {
							getHardwareAddress = iface.getClass().getMethod("getHardwareAddress");
						} catch (NoSuchMethodException ex) {
						}
						methodTested = true;
						if (getHardwareAddress == null) {
							System.out.println("  (Note: the java.net.NetworkInterface class does not support the getHardwareAddress() method. Probably you're using a JRE older than v6.)");
						}
					}
					if (getHardwareAddress != null) {
						try {
							Object hwAddress = getHardwareAddress.invoke(iface);
							if (hwAddress != null) {
								if ("[B".equals(hwAddress.getClass().getName())) {
									byte[] MAC = (byte[]) hwAddress;
									System.out.println("  MAC: " + bytes2MAC(MAC));
								} else {
									System.out.println("  The getHardwareAddress() method of the NetworkInterface object returned an object of the wrong class: " + hwAddress.getClass().getCanonicalName());
								}
							} else {
								System.out.println("  MAC: (no hardware address is available for the network interface)");
							}
						} catch (InvocationTargetException ex) {
							System.err.println(ex.getClass().getName() + " while trying to fetch hardware address.");
						} catch (IllegalAccessException ex) {
							System.err.println(ex.getClass().getName() + " while trying to fetch hardware address.");
						}
					}
					Enumeration<InetAddress> ips = null;
					ips = iface.getInetAddresses();
					if (ips != null) {
						while (ips.hasMoreElements()) {
							InetAddress ifaceAddress = ips.nextElement();
							String ipType = null;
							if (ifaceAddress instanceof Inet4Address) {
								ipType = "(v4)";
							} else if (ifaceAddress instanceof Inet6Address) {
								ipType = "(v6)";
							} else {
								ipType = "";
							}
							System.out.println("  IP" + ipType + ": " + ifaceAddress.getHostAddress());
						}
					}
				}
			} else {
				System.out.println("No network interfaces were found.");
			}
		} else {
			System.out.println("No network interfaces are available.");
		}
	}
}

