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 ListgetMacAddress4Linux() throws Exception {
        ListmacAddressList = new ArrayList<>();
        ListinetAddresses = getLocalAllInetAddress();
        if (inetAddresses != null && inetAddresses.size() >0) {
            for( InetAddress inetAddress:inetAddresses ) {
                String macAddress = getMacByInetAddress(inetAddress);
                macAddressList.add( macAddress );
            }
        }
        return macAddressList;
    }

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

    
    private static ListgetLocalAllInetAddress() throws Exception {
        Listresult = 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();
        
        ListmacAddressList = 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

你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧

让客户满意是我们工作的目标,不断超越客户的期望值来自于我们对这个行业的热爱。我们立志把好的技术通过有效、简单的方式提供给客户,将通过不懈努力成为客户在信息化领域值得信任、有价值的长期合作伙伴,公司提供的服务项目有:主机域名、虚拟空间、营销软件、网站建设、郎溪网站维护、网站推广。
当前标题:java输出服务器的mac地址-创新互联
文章来源:http://ybzwz.com/article/csicpj.html