Get Information on all network adapters


package de.eknori.test;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;

public class NICInfo {
	public static void main(String[] args) throws Exception {
		try {
			for (@SuppressWarnings("rawtypes")
			Enumeration nis = NetworkInterface.getNetworkInterfaces(); nis
					.hasMoreElements();) {
				NetworkInterface ni = (NetworkInterface) nis.nextElement();
				System.out.print("DisplayName : ");
				System.out.println(ni.getDisplayName());
				for (@SuppressWarnings("rawtypes")
				Enumeration ias = ni.getInetAddresses(); ias.hasMoreElements();) {
					InetAddress ia = (InetAddress) ias.nextElement();
					System.out.print("HostName : ");
				      System.out.println(ia.getHostName());
				      System.out.print("IP Address : ");
				      System.out.println(ia.getHostAddress());
					NetworkInterface network = NetworkInterface
							.getByInetAddress(ia);
					byte[] mac = network.getHardwareAddress();

					System.out.print("MAC address : ");

					StringBuilder sb = new StringBuilder();
					for (int i = 0; i < mac.length; i++) {
						sb.append(String.format("%02X%s", mac[i],
								(i < mac.length - 1) ? "-" : ""));
					}
					System.out.println(sb.toString());
				}
				System.out.println("-------------------------");
			}

		} catch (SocketException e) {
			e.printStackTrace();
		} finally {
		}
	}
}
All code submitted to OpenNTF XSnippets, whether submitted as a "Snippet" or in the body of a Comment, is provided under the Apache License Version 2.0. See Terms of Use for full details.
No comments yetLogin first to comment...