Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.PrintStream;
- import java.net.InetAddress;
- import java.net.NetworkInterface;
- import java.net.SocketException;
- import java.util.Collections;
- import java.util.Enumeration;
- public class ListMacAddress {
- public static void main(String[] args) throws SocketException {
- final PrintStream out = System.out;
- final Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
- displayInformationAbout(networkInterfaces, out);
- }
- private static void displayInformationAbout(final Enumeration<NetworkInterface> networkInterfaces, final PrintStream out) throws SocketException {
- for (NetworkInterface networkInterface : Collections.list(networkInterfaces)) {
- out.printf("%s (%s) - %s [%s]%n",
- networkInterface.getName(),
- networkInterface.getDisplayName(),
- ip(networkInterface),
- hardware(networkInterface));
- displayInformationAbout(networkInterface.getSubInterfaces(), out);
- }
- }
- private static String ip(final NetworkInterface networkInterface) {
- final StringBuilder builder = new StringBuilder();
- final Enumeration<InetAddress> inetAddresses = networkInterface.getInetAddresses();
- while (inetAddresses.hasMoreElements()) {
- final InetAddress inetAddress = inetAddresses.nextElement();
- builder.append(inetAddress.getHostAddress());
- if(inetAddresses.hasMoreElements()){
- builder.append(":");
- }
- }
- return builder.toString();
- }
- private static String hardware(final NetworkInterface networkInterface) throws SocketException {
- final byte[] hardwareAddress = networkInterface.getHardwareAddress();
- return hardwareAddress == null ? "" : readable(hardwareAddress);
- }
- private static String readable(byte[] hardwareAddress) {
- if(hardwareAddress == null) return "";
- final StringBuilder builder = new StringBuilder('[');
- for (int i = 0; i < hardwareAddress.length; i++) {
- byte b = hardwareAddress[i];
- builder.append(String.format("%02X", b));
- if(i < hardwareAddress.length - 1){
- builder.append(':');
- }
- }
- return builder.append(']').toString();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement