import java.util.Enumeration ; 
import java.util.List ; 
import java.util.NoSuchElementException ; 
import java.net.NetworkInterface ; 
import java.net.InterfaceAddress ; 
import java.net.InetAddress ; 
import java.net.Inet6Address ; 
import java.net.Inet4Address ; 
import java.net.SocketException ; 
import java.net.UnknownHostException; 

public class TestGetNetworkInterfaces {
	public static void main(String[] args) { 

		try { 
			// get all the network interfaces on this machine 
			Enumeration intfs = NetworkInterface.getNetworkInterfaces() ;	
			while (intfs != null && intfs.hasMoreElements()) { 
				// get the next interface 
				NetworkInterface intf = (NetworkInterface) intfs.nextElement() ; 
				System.out.println("Network interface: name = " + intf.getName() + ", display name = " + intf.getDisplayName()); 

				// interface address (IP address, subnet, broadcast address) 
				List<InterfaceAddress> interfaceAddresses = intf.getInterfaceAddresses(); 

				// get all the InetAddresses defined on the interface 
				Enumeration<InetAddress> inetAddresses = intf.getInetAddresses() ; 
				System.out.println("\tInet addresses:"); 
				while (inetAddresses != null && inetAddresses.hasMoreElements()) {	
					// get the next InetAddress for the current interface 
					InetAddress inetAddress = (InetAddress) inetAddresses.nextElement() ; 
					displayInetAddressDetails(inetAddress, "\t\t"); 
				} 

				// get all the SubInterfaces defined on the interface 
				Enumeration<NetworkInterface> subInterfaces = intf.getSubInterfaces() ; 
				System.out.println("\tSub interfaces:"); 
				while (subInterfaces != null && subInterfaces.hasMoreElements()) {	
					// get the next SubInterface for the current interface 
					NetworkInterface subInterface = (NetworkInterface) subInterfaces.nextElement() ; 
					System.out.println("\t\tSub interface =" + subInterface.toString()) ; 

					// get all the InetAddresses defined on the interface 
					Enumeration<InetAddress> subInetAddresses = subInterface.getInetAddresses() ; 
					System.out.println("\t\t\tSubinterface Inet addresses:"); 
					while (subInetAddresses != null && subInetAddresses.hasMoreElements()) {	
						// get the next InetAddress for the current interface 
						InetAddress inetAddress = (InetAddress) subInetAddresses.nextElement() ; 
						displayInetAddressDetails(inetAddress, "\t\t\t\t"); 
					} 
				} 
			} 
		} 
		catch(SocketException e) { 
			// NetworkInterface.getNetworkInterfaces() -> java.net.SocketException 
		} 
		catch(NoSuchElementException e) { 
			// Enumeration.nextElement() -> java.util.NoSuchElementException 
		} 
	} 

	private static void displayInetAddressDetails(InetAddress inetAddress, String indent) { 
		// this just prints out the inet address part, in the form <IP address>%<interface name> 
		String hostAddress = inetAddress.getHostAddress(); 
		System.out.println(indent + "Inet address.getHostAddress() =" + hostAddress) ; 
		boolean ipv6 = inetAddress instanceof Inet6Address; 
		if (ipv6) { 
			System.out.print(indent + "IsIpv6Address? = true"); 
			Inet6Address inet6Address = (Inet6Address) inetAddress; 
			if (inet6Address.isSiteLocalAddress()) { 
				System.out.println(", type = Site Local Address"); 
			} else if (inet6Address.isLinkLocalAddress()) { 
				System.out.print(", type = Link Local Address"); 
				System.out.print(", scoped interface = " + inet6Address.getScopedInterface()); 
				System.out.println(", scopeid = " + inet6Address.getScopeId()); 
			} else if (inet6Address.isLoopbackAddress()) { 
				System.out.println(", type = Loopback Address"); 
			} else { 
				System.out.println(", type = Global Address"); 
			} 

			// check that we can do a reverse lookup 
			try { 
				String reversalString = inet6Address.getHostAddress(); 
				System.out.println(indent + "Doing reverse lookup on address: " + reversalString); 
				InetAddress reversal = InetAddress.getByName(reversalString) ; 
				System.out.println(indent + "Result of reverse lookup on address: " + reversal.getHostAddress());	
			} catch(UnknownHostException uhe) { 
				System.out.println(indent + "problem computing reversal: exception = " + uhe.toString()); 
			} 
		} else { 
			System.out.println(indent + "IsIpv6Address? = false"); 
		}	
	} 
}
