|
@@ -0,0 +1,121 @@
|
|
|
+package com.huimv.eartag2.admin.controller;
|
|
|
+
|
|
|
+import java.io.BufferedReader;
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.InputStreamReader;
|
|
|
+import java.net.InetAddress;
|
|
|
+import java.net.NetworkInterface;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+public class MacAddressExample {
|
|
|
+
|
|
|
+ public static String getMACAddress() {
|
|
|
+ List<String> commands = new ArrayList<>();
|
|
|
+ String os = System.getProperty("os.name").toLowerCase();
|
|
|
+
|
|
|
+ if (os.contains("win")) {
|
|
|
+ return getMacAddress();
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ Process process = Runtime.getRuntime().exec("ifconfig -a");
|
|
|
+ try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()))) {
|
|
|
+ String line;
|
|
|
+ while ((line = reader.readLine()) != null) {
|
|
|
+ if (line.contains("ether")) {
|
|
|
+ int start = line.indexOf("ether") + 6;
|
|
|
+ return line.substring(start, start + 17); // MAC地址的长度为17个字符
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return "MAC Address not found or not accessible.";
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String getMacAddress() {
|
|
|
+
|
|
|
+ try {
|
|
|
+ InetAddress localHost = InetAddress.getLocalHost();
|
|
|
+ NetworkInterface networkInterface = NetworkInterface.getByInetAddress(localHost);
|
|
|
+ byte[] macAddressBytes = networkInterface.getHardwareAddress();
|
|
|
+
|
|
|
+ if (macAddressBytes != null) {
|
|
|
+ StringBuilder macAddress = new StringBuilder();
|
|
|
+ for (byte b : macAddressBytes) {
|
|
|
+ macAddress.append(String.format("%02X:", b));
|
|
|
+ }
|
|
|
+
|
|
|
+ if (macAddress.length() > 0) {
|
|
|
+ macAddress.deleteCharAt(macAddress.length() - 1); // Remove the trailing ":"
|
|
|
+ }
|
|
|
+
|
|
|
+ return macAddress.toString();
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String getDiskId() {
|
|
|
+ try {
|
|
|
+ List<String> commands = new ArrayList<>();
|
|
|
+ String os = System.getProperty("os.name").toLowerCase();
|
|
|
+
|
|
|
+ if (os.contains("win")) {
|
|
|
+ // For Windows
|
|
|
+ commands.add("wmic");
|
|
|
+ commands.add("diskdrive");
|
|
|
+ commands.add("get");
|
|
|
+ commands.add("SerialNumber");
|
|
|
+ } else if (os.contains("nix") || os.contains("nux") || os.contains("mac")) {
|
|
|
+ // For Linux and macOS
|
|
|
+ commands.add("lsblk");
|
|
|
+ commands.add("-no");
|
|
|
+ commands.add("serial");
|
|
|
+ commands.add("/dev/sda"); // Change to the appropriate device name
|
|
|
+ } else {
|
|
|
+ System.out.println("Unsupported operating system");
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ ProcessBuilder processBuilder = new ProcessBuilder(commands);
|
|
|
+ Process process = processBuilder.start();
|
|
|
+ BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
|
|
|
+
|
|
|
+ String line;
|
|
|
+ String dis="";
|
|
|
+ while ((line = reader.readLine()) != null) {
|
|
|
+ if(!"".equals(line)){
|
|
|
+ dis = line;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return dis;
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String getMotherboardSerial() {
|
|
|
+ try {
|
|
|
+ Process process = Runtime.getRuntime().exec("sudo dmidecode -t 2");
|
|
|
+ try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()))) {
|
|
|
+ String line;
|
|
|
+ while ((line = reader.readLine()) != null) {
|
|
|
+ if (line.contains("Serial Number")) {
|
|
|
+ int start = line.indexOf("Serial Number") + 15;
|
|
|
+ return line.substring(start).trim();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return "Motherboard Serial not available on this system.";
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+}
|