获取CPU、硬盘、主板序列号及MAC地址工具类

获取CPU、硬盘、主板序列号、MAC工具类

public class MachineSNUtils {

    /**
     * 获取主板序列号
     *
     * @return
     */
    public static String getMotherboardSN() {
        String result = "";
        try {
            File file = File.createTempFile("realhowto", ".vbs");
            file.deleteOnExit();
            FileWriter fw = new FileWriter(file);

            String vbs = "Set objWMIService = GetObject(\"winmgmts:\\\\.\\root\\cimv2\")\n"
                    + "Set colItems = objWMIService.ExecQuery _ \n"
                    + "   (\"Select * from Win32_BaseBoard\") \n"
                    + "For Each objItem in colItems \n" + "    Wscript.Echo objItem.SerialNumber \n"
                    + "    exit for  ' do the first cpu only! \n" + "Next \n";

            fw.write(vbs);
            fw.close();
            String path = file.getPath().replace("%20", " ");
            Process p = Runtime.getRuntime().exec("cscript //NoLogo " + path);
            BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
            String line;
            while ((line = input.readLine()) != null) {
                result += line;
            }
            input.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result.trim();
    }

    /**
     * 获取CPU序列号
     *
     * @return
     */
    public static String getCPUSerial() {
        String result = "";
        try {
            File file = File.createTempFile("tmp", ".vbs");
            file.deleteOnExit();
            FileWriter fw = new FileWriter(file);
            String vbs = "Set objWMIService = GetObject(\"winmgmts:\\\\.\\root\\cimv2\")\n"
                    + "Set colItems = objWMIService.ExecQuery _ \n"
                    + "   (\"Select * from Win32_Processor\") \n"
                    + "For Each objItem in colItems \n" + "    Wscript.Echo objItem.ProcessorId \n"
                    + "    exit for  ' do the first cpu only! \n" + "Next \n";

            // + "    exit for  \r\n" + "Next";
            fw.write(vbs);
            fw.close();
            String path = file.getPath().replace("%20", " ");
            Process p = Runtime.getRuntime().exec("cscript //NoLogo " + path);
            BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
            String line;
            while ((line = input.readLine()) != null) {
                result += line;
            }
            input.close();
            file.delete();
        } catch (Exception e) {
            e.fillInStackTrace();
        }
        if (result.trim().length() < 1 || result == null) {
            result = "无CPU_ID被读取";
        }
        return result.trim();
    }

    /**
     * 获取localhost的LANAddress
     *
     * @return
     */
    private static List<String> getLocalHostLANAddress()
            throws UnknownHostException, SocketException {
        List<String> ips = new ArrayList<String>();
        Enumeration<NetworkInterface> interfs = NetworkInterface.getNetworkInterfaces();
        while (interfs.hasMoreElements()) {
            NetworkInterface interf = interfs.nextElement();
            Enumeration<InetAddress> addres = interf.getInetAddresses();
            while (addres.hasMoreElements()) {
                InetAddress in = addres.nextElement();
                if (in instanceof Inet4Address) {
//                    System.out.println("v4:" + in.getHostAddress());
                    if (!"127.0.0.1".equals(in.getHostAddress())) {
                        //连接VPN后,根据产生的ip查询MAC时,返回结果为null 因此获取IP时不要VPN产生的IP
                        if (!interf.getName().contains("ppp")){
                            ips.add(in.getHostAddress());
                        }

                    }
                }
            }
        }
        return ips;
    }

    /**
     * MAC
     * 通过jdk自带的方法,先获取本机所有的ip,然后通过NetworkInterface获取mac地址
     * 注意:连接VPN后,根据产生的ip查询MAC时,返回结果为null,需特殊处理
     *
     * @return
     */
    public static String getMac() {
        try {
            String resultStr = "";
            List<String> ls = getLocalHostLANAddress();
            int num = 0;
            for (String str : ls) {
                InetAddress ia = InetAddress.getByName(str);// 获取本地IP对象
                // 获得网络接口对象(即网卡),并得到mac地址,mac地址存在于一个byte数组中。
                byte[] mac = NetworkInterface.getByInetAddress(ia).getHardwareAddress();
                if (mac == null) {
                    continue;
                }
                // 下面代码是把mac地址拼装成String
                StringBuilder sb = new StringBuilder();
                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);
                }
                if (num == ls.size() - 1) {
                    resultStr += sb.toString().toUpperCase();
                } else {
                    // 把字符串所有小写字母改为大写成为正规的mac地址并返回
                    resultStr += sb.toString().toUpperCase() + ",";
                }
                num++;
            }
            return resultStr;
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }

    /**
     * 获取硬盘序列号
     *
     * @return
     */
    public static String getHardDiskSN() {

        String line = "";
        String HdSerial = "";
        try {
            Process proces = Runtime.getRuntime().exec("cmd /c dir c:");
            BufferedReader buffreader = new BufferedReader(new InputStreamReader(proces.getInputStream(), "gbk"));

            while ((line = buffreader.readLine()) != null) {
                if (line.indexOf("卷的序列号是 ") != -1) {  //读取参数并获取硬盘序列号
                    HdSerial = line.substring(line.indexOf("卷的序列号是 ") + "卷的序列号是 ".length());
                    break;
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return HdSerial;
    }

    /***************************linux*********************************/

    public static String executeLinuxCmd(String cmd) {
        try {
            System.out.println("got cmd job : " + cmd);
            Runtime run = Runtime.getRuntime();
            Process process;
            process = run.exec(cmd);
            InputStream in = process.getInputStream();
            BufferedReader bs = new BufferedReader(new InputStreamReader(in));
            StringBuffer out = new StringBuffer();
            byte[] b = new byte[8192];
            for (int n; (n = in.read(b)) != -1; ) {
                out.append(new String(b, 0, n));
            }
            in.close();
            process.destroy();
            return out.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * @param cmd    命令语句
     * @param record 要查看的字段
     * @param symbol 分隔符
     * @return
     */
    public static String getSerialNumber(String cmd, String record, String symbol) {
        String execResult = executeLinuxCmd(cmd);
        String[] infos = execResult.split("\n");

        for (String info : infos) {
            info = info.trim();
            if (info.indexOf(record) != -1) {
                info.replace(" ", "");
                String[] sn = info.split(symbol);
                return sn[1];
            }
        }

        return null;
    }

    /**
     * @param cmd    命令语句
     * @param record 要查看的字段
     * @param symbol 分隔符
     * @return
     */
    public static String getAllSerialNumber(String cmd, String record, String symbol) {
        String execResult = executeLinuxCmd(cmd);
        String[] infos = execResult.split("\n");
        StringBuilder result = new StringBuilder();
        int k = 0;
        for (int i = 0; i < infos.length - 1; i++) {
            String info = infos[i];
            info = info.trim();
            if (info.indexOf(record) != -1) {
                info.replace(" ", "");
                String[] sn = info.split(symbol);
                if (k != 0) {
                    result.append(',');
                }
                result.append(sn[1]);
                k++;
            }
        }
        if (k != 0) {
            return result.toString();
        } else {
            return null;
        }
    }

    /**
     * 判断是否为容器、虚拟机,返回虚拟ID
     *
     * @return
     */
    public static String getVirtualID() {
        String execResult = executeLinuxCmd("systemd-detect-virt");
        if (!execResult.contains("none")) {
            //docker容器
            String VirtualID = getSerialNumber("cat /proc/1/cgroup", "docker", "docker/");
            if (VirtualID != null) {
                return VirtualID;
            }
            //machine-rkt
            VirtualID = getSerialNumber("cat /proc/1/cgroup", "machine-rkt", "machine-rkt\\");
            if (VirtualID != null) {
                VirtualID.replaceAll("\\x2d", "-");
                return VirtualID;
            }
            //vmware
            VirtualID = getSerialNumber("dmidecode -t system", "UUID", ":");
            if (VirtualID != null) {
                return VirtualID;
            }
            return "UNKNOWN";
        }
        return null;
    }

    /**
     * 获取CPUID、硬盘序列号、MAC地址、主板序列号
     *
     * @return
     */
    public static Map<String, String> getAllSn() {
        String os = System.getProperty("os.name");
        os = os.toUpperCase();
        System.out.println("OS Name : " + os);

        Map<String, String> snVo = new HashMap<String, String>();

        if ("LINUX".equals(os)) {
            snVo.put("operating system", "LINUX");
            String virtualID = getVirtualID();
            if (virtualID != null) {
                if (virtualID.equals("UNKNOWN")) {
                    System.out.println("UNKNOWN VMWARE!");
                    return snVo;
                } else {
                    System.out.println("virtualID : " + virtualID);
                    snVo.put("virtualID", virtualID.toUpperCase().replace(" ", ""));
                    String mac = getAllSerialNumber("ifconfig -a", "ether", " ");
                    System.out.println("mac : " + mac);
                    snVo.put("mac", mac.toUpperCase().replace(" ", ""));
                }
            } else {
                String cpuid = getSerialNumber("dmidecode -t processor | grep 'ID'", "ID", ":");
                System.out.println("cpuid : " + cpuid);
                String mainboardNumber =
                        getSerialNumber("dmidecode |grep 'Serial Number'", "Serial Number", ":");
                System.out.println("mainboardNumber : " + mainboardNumber);
                String mac = getAllSerialNumber("ifconfig -a", "ether", " ");
                System.out.println("mac : " + mac);

                snVo.put("cpuid", cpuid.toUpperCase().replace(" ", ""));
                snVo.put("mac", mac.toUpperCase().replace(" ", ""));
                snVo.put("mainboard", mainboardNumber.toUpperCase().replace(" ", ""));
            }
        } else {
            snVo.put("operating system", "windows");

            String cpuid = getCPUSerial();
            String mainboard = getMotherboardSN();
            String disk = getHardDiskSN();
            String mac = getMac();

            System.out.println("CPU_SN : " + cpuid);
            System.out.println("MAINBOARD_SN : " + mainboard);
            System.out.println("CDISK_SN : " + disk);
            System.out.println("MAC_SN : " + mac);

            snVo.put("cpuid", cpuid.toUpperCase().replace(" ", ""));
            snVo.put("cdiskid", disk.toUpperCase().replace(" ", ""));
            snVo.put("mac", mac.toUpperCase().replace(" ", ""));
            snVo.put("mainboard", mainboard.toUpperCase().replace(" ", ""));
        }
        return snVo;
    }

	public static void main(String[] args) {
        getAllSn();
    }
}

参考文章地址:https://blog.csdn.net/linhaibing009/article/details/102524648

本文章修改了原帖中getLocalHostLANAddress()方法,增加对VPN连接的处理。由于连接VPN后,根据产生的ip查询MAC时,返回结果为null,做了特殊处理,即获取本机IP时,过滤掉VPN产生的IP。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值