package com.skr.mdm.util;

import java.io.IOException;
import java.util.Scanner;

/**
 * 获取服务器的基本信息
 */
public class WindowsServerUtil {

    /**
     * 获取 CPU 序列号
     * @return
     */
    public static String getCPUSerial() {
        String serialNumber = null;
        try {
            //序列号
            serialNumber = "";
            //使用 WMIC 获取 CPU 序列号
            Process process = Runtime.getRuntime().exec("wmic cpu get processorid");
            process.getOutputStream().close();
            Scanner scanner = new Scanner(process.getInputStream());

            if (scanner.hasNext()) {
                scanner.next();
            }
            if (scanner.hasNext()) {
                serialNumber = scanner.next().trim();
            }
            scanner.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return serialNumber;
    }

    /**
     * 获取主板序列号
     * @return
     * @throws Exception
     */
    public static String getMainBoardSerial(){
        String serialNumber = null;
        try {
            //序列号
            serialNumber = "";
            //使用 WMIC 获取主板序列号
            Process process = Runtime.getRuntime().exec("wmic baseboard get serialnumber");
            process.getOutputStream().close();
            Scanner scanner = new Scanner(process.getInputStream());
            if (scanner.hasNext()) {
                scanner.next();
            }
            if (scanner.hasNext()) {
                serialNumber = scanner.next().trim();
            }
            scanner.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return serialNumber;
    }
}
