java 输出服务器的 mac 地址

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

public class PrintServerMacInfo {

    private static List<String> getMacAddress4Linux() throws Exception {
        List<String> macAddressList = new ArrayList<>();
        List<InetAddress> inetAddresses = getLocalAllInetAddress();
        if (inetAddresses != null && inetAddresses.size() > 0) {
            for( InetAddress inetAddress:inetAddresses ) {
                String macAddress = getMacByInetAddress(inetAddress);
                macAddressList.add( macAddress );
            }
        }
        return macAddressList;
    }

    private static List<String> getMacAddress4Windows() throws Exception {
        List<String> macAddressList = new ArrayList<>();
        List<InetAddress> inetAddresses = getLocalAllInetAddress();
        if (inetAddresses != null && inetAddresses.size() > 0) {
            for( InetAddress inetAddress:inetAddresses ){
                String macAddress = getMacByInetAddress(inetAddress);
                macAddressList.add( macAddress );
            }
        }
        return macAddressList;
    }

    
    private static List<InetAddress> getLocalAllInetAddress() throws Exception {
        List<InetAddress> result = new ArrayList<>(4);
        for (Enumeration networkInterfaces = NetworkInterface.getNetworkInterfaces(); networkInterfaces.hasMoreElements(); ) {
            NetworkInterface iface = (NetworkInterface) networkInterfaces.nextElement();
            for (Enumeration inetAddresses = iface.getInetAddresses(); inetAddresses.hasMoreElements(); ) {
                InetAddress inetAddr = (InetAddress) inetAddresses.nextElement();
                if (!inetAddr.isLoopbackAddress()
                        && !inetAddr.isLinkLocalAddress() && !inetAddr.isMulticastAddress()) {
                    result.add(inetAddr);
                }
            }
        }
        return result;
    }

   
    private static String getMacByInetAddress(InetAddress inetAddr) {
        try {
            byte[] mac = NetworkInterface.getByInetAddress(inetAddr).getHardwareAddress();
            StringBuffer stringBuffer = new StringBuffer();

            for (int i = 0; i < mac.length; i++) {
                if (i != 0) {
                    stringBuffer.append("-");
                }

               
                String temp = Integer.toHexString(mac[i] & 0xff);
                if (temp.length() == 1) {
                    stringBuffer.append("0" + temp);
                } else {
                    stringBuffer.append(temp);
                }
            }
            return stringBuffer.toString().toUpperCase();
        } catch (SocketException e) {
            e.printStackTrace();
        }
        return null;
    }

    private static void printServerInfo() throws Exception {
        System.out.println( "server info:" );
        System.out.println( "macAddress list:" );
        String osName = System.getProperty("os.name").toLowerCase();
        
        List<String> macAddressList = null;
        if (osName.startsWith("windows")) {
            macAddressList = getMacAddress4Windows();
        } else if (osName.startsWith("linux")) {
            macAddressList = getMacAddress4Linux();
        }else{
            macAddressList = getMacAddress4Linux();
        }
        if( macAddressList == null || macAddressList.size() == 0 ){
            return;
        }
        for( String macAddress:macAddressList ){
            System.out.println( "   " + macAddress );
        }
    }

    public static void main(String[] args) throws Exception {
        printServerInfo();
    }
}
javac PrintServerMacInfo.java
java PrintServerMacInfo

猜你喜欢

转载自blog.csdn.net/heshiyuan1406146854/article/details/128646940
今日推荐