📜  获取 Windows 和 Linux 机器的系统主板序列号的Java程序

📅  最后修改于: 2022-05-13 01:55:29.357000             🧑  作者: Mango

获取 Windows 和 Linux 机器的系统主板序列号的Java程序

主板是整个 CPU 架构的基础。它是主要的印刷电路板 (PCB),负责连接所有主要组件,例如硬盘驱动器、CPU、外部端口等。一个优秀且知识渊博的计算机用户必须知道他们开发板的制造商、版本和序列号。本文主要围绕编写Java代码来提取用户各自机器的主板序列号。每个主板的序列号都是唯一的,因此,每个用户在运行相同的程序时都会得到不同的输出。下面,我们提供了用于查找 Windows 和 Linux 机器主板序列号的代码。

A. Linux 机器:

检索主板序列号的主要概念是使用Java代码在终端中运行命令并将检索到的序列号存储为字符串,然后将其打印在屏幕上。

算法 :

  1. 首先,我们将应该在终端上运行的命令存储在名为 command 的变量中。
  2. 接下来,我们通过调用Java的 Runtime 类来初始化一个 Process,该类用于与Java运行时环境进行交互。
  3. 我们将命令作为参数传递给 exec函数,该函数的主要函数是让命令被执行。
  4. 对于下一步,我们使用Java I/O 包的 InputStreamReader 类从进程中捕获输入流。
  5. 然后,BufferedReader 类用于从 InputStreamReader 读取流。
  6. 我们将其存储在一个变量中。
  7. 接下来,我们等待进程终止。
  8. 我们关闭 BufferedReader。
  9. 在 catch 块中,如果发生错误,我们将打印堆栈跟踪并将保存序列号的变量设置为 null。
  10. 最后,我们返回这个变量并使用驱动程序代码打印输出。

使用的命令:

sudo dmidecode -s baseboard-serial-number

下面是问题陈述的实现:

Java
// Java code to get the system
// motherboard serial number on linux
  
// importing the libraries
import java.io.*;
  
class GFG {
    static String getLinuxMotherBoardSerialNumber()
    {
  
        // command to be executed on the terminal
        String command
            = "sudo dmidecode -s baseboard-serial-number";
  
        // variable to store the Serial Number
        String serialNumber = null;
  
        // try block
        try {
  
            // declaring the process to run the command
            Process SerialNumberProcess
                = Runtime.getRuntime().exec(command);
  
            // getting the input stream using
            // InputStreamReader using Serial Number Process
            InputStreamReader ISR = new InputStreamReader(
                SerialNumberProcess.getInputStream());
  
            // declaring the Buffered Reader
            BufferedReader br = new BufferedReader(ISR);
  
            // reading the serial number using
            // Buffered Reader
            serialNumber = br.readLine().trim();
  
            // waiting for the system to return
            // the serial number
            SerialNumberProcess.waitFor();
  
            // closing the Buffered Reader
            br.close();
        }
  
        // catch block
        catch (Exception e) {
  
            // printing the exception
            e.printStackTrace();
  
            // giving the serial number the value null
            serialNumber = null;
        }
  
        // returning the serial number
        return serialNumber;
    }
  
    // Driver Code
    public static void main(String[] args)
    {
  
        // printing and calling the method which
        // returns the Serial Number
        System.out.println(
            getLinuxMotherBoardSerialNumber());
    }
}


Java
// Java code to get the system
// motherboard serial number on Windows
  
// importing the libraries
import java.io.*;
  
class GFG {
    static String getWindowsMotherBoardSerialNumber()
    {
  
        // command to be executed on the terminal
        String command = "wmic baseboard get serialnumber";
  
        // variable to store the Serial Number
        String serialNumber = null;
  
        // try block
        try {
  
            // declaring the process to run the command
            Process SerialNumberProcess
                = Runtime.getRuntime().exec(command);
  
            // getting the input stream using
            // InputStreamReader using Serial Number Process
            InputStreamReader ISR = new InputStreamReader(
                SerialNumberProcess.getInputStream());
  
            // declaring the Buffered Reader
            BufferedReader br = new BufferedReader(ISR);
  
            // reading the serial number using
            // Buffered Reader
            serialNumber = br.readLine().trim();
  
            // waiting for the system to return
            // the serial number
            SerialNumberProcess.waitFor();
  
            // closing the Buffered Reader
            br.close();
        }
  
        // catch block
        catch (Exception e) {
  
            // printing the exception
            e.printStackTrace();
  
            // giving the serial number the value null
            serialNumber = null;
        }
  
        // returning the serial number
        return serialNumber;
    }
  
    // Driver Code
    public static void main(String[] args)
    {
  
        // printing and calling the method which
        // returns the Serial Number
        System.out.println(
            getWindowsMotherBoardSerialNumber());
    }
}


输出 :

PGPPP018J940BP

B. Windows 机器:

用于在 Windows 机器上检索主板序列号的代码与用于在 Linux 机器上检索它的代码的唯一区别在于所使用的命令。其余算法和代码保持不变。

使用的命令:

wmic baseboard get serialnumber

下面是问题陈述的实现:

Java

// Java code to get the system
// motherboard serial number on Windows
  
// importing the libraries
import java.io.*;
  
class GFG {
    static String getWindowsMotherBoardSerialNumber()
    {
  
        // command to be executed on the terminal
        String command = "wmic baseboard get serialnumber";
  
        // variable to store the Serial Number
        String serialNumber = null;
  
        // try block
        try {
  
            // declaring the process to run the command
            Process SerialNumberProcess
                = Runtime.getRuntime().exec(command);
  
            // getting the input stream using
            // InputStreamReader using Serial Number Process
            InputStreamReader ISR = new InputStreamReader(
                SerialNumberProcess.getInputStream());
  
            // declaring the Buffered Reader
            BufferedReader br = new BufferedReader(ISR);
  
            // reading the serial number using
            // Buffered Reader
            serialNumber = br.readLine().trim();
  
            // waiting for the system to return
            // the serial number
            SerialNumberProcess.waitFor();
  
            // closing the Buffered Reader
            br.close();
        }
  
        // catch block
        catch (Exception e) {
  
            // printing the exception
            e.printStackTrace();
  
            // giving the serial number the value null
            serialNumber = null;
        }
  
        // returning the serial number
        return serialNumber;
    }
  
    // Driver Code
    public static void main(String[] args)
    {
  
        // printing and calling the method which
        // returns the Serial Number
        System.out.println(
            getWindowsMotherBoardSerialNumber());
    }
}

输出:

PGPPP018J940BP