--- /dev/null
+import static java.lang.System.out;\r
+\r
+import java.net.InetAddress;\r
+import java.net.NetworkInterface;\r
+import java.net.SocketException;\r
+import java.util.Collections;\r
+import java.util.Enumeration;\r
+\r
+// this utility displays information on addresses and ports, you don't need to use it\r
+public class NetHelper \r
+{\r
+ public static void main(String args[]) throws SocketException {\r
+ Enumeration<NetworkInterface> nets = NetworkInterface.getNetworkInterfaces();\r
+ \r
+ for (NetworkInterface netIf : Collections.list(nets)) {\r
+ out.printf("Display name: %s\n", netIf.getDisplayName());\r
+ out.printf("Name: %s\n", netIf.getName());\r
+ displayInterfaceInformation(netIf);\r
+ displaySubInterfaces(netIf);\r
+ out.printf("\n");\r
+ }\r
+ }\r
+ \r
+ public static NetworkInterface getNetworkInterface(String netName) {\r
+ NetworkInterface netIntf = null;\r
+ try {\r
+ for (Enumeration<NetworkInterface> en = NetworkInterface\r
+ .getNetworkInterfaces(); en.hasMoreElements();) {\r
+ NetworkInterface intf = en.nextElement();\r
+ if (intf.getName().compareToIgnoreCase(netName) == 0) {\r
+ netIntf = intf;\r
+ break;\r
+ }\r
+ }\r
+ } catch (SocketException e) {\r
+ System.out\r
+ .println("Socket Exception failed to find internet addresses!");\r
+ e.printStackTrace();\r
+ System.exit(0);\r
+ }\r
+ return netIntf;\r
+ }\r
+\r
+ public static InetAddress getFirstNonLocalHost(NetworkInterface netIntf) {\r
+ Enumeration<InetAddress> hosts = netIntf.getInetAddresses();\r
+ InetAddress address = null;\r
+ while (hosts.hasMoreElements()) {\r
+ InetAddress host = hosts.nextElement();\r
+ if (!host.isLinkLocalAddress() && !host.isLoopbackAddress()) {\r
+ address = host;\r
+ break;\r
+ }\r
+ }\r
+ return address;\r
+ }\r
+\r
+ \r
+ \r
+ static String getMac(NetworkInterface netint) throws SocketException {\r
+ byte[] mac = netint.getHardwareAddress();\r
+ String rez = "null";\r
+ if(mac != null) {\r
+ StringBuilder sb = new StringBuilder();\r
+ for (int i = 0; i < mac.length; i++) {\r
+ sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : "")); \r
+ }\r
+ rez = sb.toString();\r
+ }\r
+ return rez;\r
+ //System.out.println(sb.toString());\r
+ }\r
+ \r
+ static void displayInterfaceInformation(NetworkInterface netint) throws SocketException {\r
+ out.printf("Display name: %s\n", netint.getDisplayName());\r
+ out.printf("Name: %s\n", netint.getName());\r
+ out.printf("MAC: %s\n", getMac(netint));\r
+ Enumeration<InetAddress> inetAddresses = netint.getInetAddresses();\r
+ for (InetAddress inetAddress : Collections.list(inetAddresses)) {\r
+ out.printf("InetAddress: %s\n", inetAddress);\r
+ out.println("LOCAL:" + Boolean.toString(inetAddress.isLoopbackAddress() || inetAddress.isLinkLocalAddress()));\r
+ }\r
+ out.printf("\n");\r
+ }\r
+\r
+ static void displaySubInterfaces(NetworkInterface netIf) throws SocketException {\r
+ Enumeration<NetworkInterface> subIfs = netIf.getSubInterfaces();\r
+ \r
+ for (NetworkInterface subIf : Collections.list(subIfs)) {\r
+ out.printf("\tSub Interface Display name: %s\n", subIf.getDisplayName());\r
+ out.printf("\tSub Interface Name: %s\n", subIf.getName());\r
+ displayInterfaceInformation(subIf);\r
+ }\r
+ }\r
+}
\ No newline at end of file