Java获取Cpu序列号,Mac地址(Windows,Linux)

1、注意只能获取本地的信息,获取客户端信息需要另加操作

2、获取Cpu序列号

    /**
     * description 获取CPU序列号
     *
     * @return java.lang.String
     * @version 1.0
     * @date 2021/1/19 10:33
     */
    public static String getCpuId() throws IOException {
    
    

        // linux,windows命令
        String[] linux = {
    
    "dmidecode", "-t", "processor", "|", "grep", "'ID'"};
        String[] windows = {
    
    "wmic", "cpu", "get", "ProcessorId"};

        // 获取系统信息
        String property = System.getProperty("os.name");
        Process process = Runtime.getRuntime().exec(property.contains("Window") ? windows : linux);
        process.getOutputStream().close();
        Scanner sc = new Scanner(process.getInputStream(), "utf-8");
        sc.next();
        return sc.next();
    }

运行结果:

cpu序列号:AEANABFF033NKH7C

3、获取Mac地址:

    /**
     * description 获取Mac地址
     *
     * @return java.lang.String
     * @version 1.0
     * @date 2021/1/19 10:35
     */
    public static String getMacAddress() throws UnknownHostException, SocketException {
    
    

        // 获取本地IP对象
        InetAddress ia = InetAddress.getLocalHost();
        InetAddress[] inetAddressArr = InetAddress.getAllByName(ia.getHostName());
        for (int i = 0; i < inetAddressArr.length; i++) {
    
    
            if (inetAddressArr[i].getHostAddress() != null) {
    
    
                String ip = inetAddressArr[i].getHostAddress();
                if (!(ip.endsWith(".1") || ip.endsWith(".0") || ip.endsWith(".255"))) {
    
    
                    ia = inetAddressArr[i];
                    break;
                }
            }
        }

        // 获得网络接口对象(即网卡),并得到mac地址,mac地址存在于一个byte数组中。
        byte[] mac = NetworkInterface.getByInetAddress(ia).getHardwareAddress();

        // 下面代码是把mac地址拼装成String
        StringBuilder sb = new StringBuilder();

        // 解析mac地址
        parseMac(mac, sb);

        // 把字符串所有小写字母改为大写成为正规的mac地址并返回
        return sb.toString().toUpperCase();
    }
    
    /**
     * description 解析Mac地址
     *
     * @return void
     * @version 1.0
     * @date 2021/1/19 13:40
     */
    public static void parseMac(byte[] mac, StringBuilder sb) {
    
    
        for (int i = 0; i < mac.length; i++) {
    
    
            if (i != 0) {
    
    
                sb.append("-");
            }
            // mac[i] & 0xFF 是为了把byte转化为正整数
            String s = Integer.toHexString(mac[i] & 0xFF);
            sb.append(s.length() == 1 ? 0 + s : s);
        }
    }

运行结果:

本地mac地址:A3-86-2B-A9-45-3B

如果电脑存在多个网卡(有线网卡,无线网卡)的情况怎么办?经过试验,结果感觉不是太满意,经过大量的查找看到了这篇博客 java如何获取mac物理地址,感觉挺不错的这里给大家简单的摘要出来看下:

    /**
     * description 判断有多个网卡的情况
     *
     * @return java.util.List<java.lang.String>
     * @version 1.0
     * @date 2021/1/19 13:17
     */
    public static List<String> getMacList() throws Exception {
    
    

        Enumeration<NetworkInterface> enumeration = NetworkInterface.getNetworkInterfaces();
        StringBuilder sb = new StringBuilder();
        ArrayList<String> macList = new ArrayList<>();
        while (enumeration.hasMoreElements()) {
    
    
            NetworkInterface iface = enumeration.nextElement();
            List<InterfaceAddress> addrs = iface.getInterfaceAddresses();
            for (InterfaceAddress addr : addrs) {
    
    

                // 获取本地IP对象
                InetAddress ia = addr.getAddress();
                NetworkInterface network = NetworkInterface.getByInetAddress(ia);
                if (network == null) {
    
    
                    continue;
                }
                // 获得网络接口对象(即网卡),并得到mac地址,mac地址存在于一个byte数组中。
                byte[] mac = network.getHardwareAddress();
                if (mac == null) {
    
    
                    continue;
                }
                parseMac(mac, sb);
                macList.add(sb.toString().toUpperCase());
                sb.delete(0, sb.length());
            }
        }
        // 去重处理,同一个网卡的ipv4,ipv6得到的mac都是一样的
        if (macList != null && macList.size() > 0) {
    
    
            List<String> result = macList.stream().distinct().collect(Collectors.toList());
            return result;
        }
        return null;
    }

运行结果:

mac地址:[A9-C7-39-26-09-AE, 03-82-85-K2-27-05, F7-01-79-28-13-F0]

猜你喜欢

转载自blog.csdn.net/qq_39486119/article/details/112802675
今日推荐