How to get all network interfaces and addresses that are active

We were recently working on a small project and the need to get all network interfaces and addresses was required. Although this is not Wowza specific we thought it would be a useful bit of information to save other people time.

	try {
		Enumeration InterfaceNets = NetworkInterface.getNetworkInterfaces();
		for (NetworkInterface netint : Collections.list(InterfaceNets))
        	{
			try 
				{
				if ( netint.isUp() )
					{
					
					String interfaceName = netint.getName().toLowerCase();
					Enumeration inetAddresses = netint.getInetAddresses();
					String IPAddresses = "";
					for (InetAddress inetAddress : Collections.list(inetAddresses)) 
					{
						String ThisAddress = inetAddress.toString();
						if ( ThisAddress != null )
						{
							if ( ThisAddress.length()>0 )
								IPAddresses += IPAddresses +ThisAddress+",";
						}
					}
					if ( IPAddresses != null )
					{
						if ( IPAddresses.length()>0 )
						{
						WMSLoggerFactory.getLogger(null).info("found interface '"+interfaceName+"' addresses are '"+IPAddresses+"'");
						}
					}
					}
				} catch (Exception noInterface) {}
        	}
		} catch ( Exception noInterfaceFinal) {}
	

When we run the code we get the following output

INFO server comment – found interface ‘lo’ addresses are ‘/127.0.0.1,’
INFO server comment – found interface ‘wlan0′ addresses are ‘/10.0.0.22,’
INFO server comment – found interface ‘eth6′ addresses are ‘/192.168.18.1,’
INFO server comment – found interface ‘eth7′ addresses are ‘/192.168.199.1,’


Comments are closed.