java获取机器码简单实现demo
作者:初窥门径
这篇文章主要为大家介绍了java获取机器码的简单实现demo,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
java获取机器码
import cn.hutool.crypto.digest.DigestUtil; import com.alibaba.fastjson.JSON; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.HashMap; import java.util.Map; import java.util.Objects; import java.util.Scanner; /** * @author shanjunpeng * @date 2023/9/27 */ public class MachineCodeUtils { public static final String WINDOWS = "Windows"; public static final String LINUX = "Linux"; /** * 获取机器码 */ public static String getMachineCode() { return getMachineCode(getOS()); } /** * 获取操作系统 * @return */ public static String getOS() { String os = System.getProperty("os.name").toLowerCase(); if (os.contains("win")) { return WINDOWS; } else if (os.contains("nix") || os.contains("nux") || os.contains("aix")) { return LINUX; } else if (os.contains("mac")) { return "Mac OS"; } else if (os.contains("sunos")) { return "Solaris"; } else { return "Unknown"; } } public static String getMachineCode(String type) { if (Objects.isNull(type)) { return ""; } Map<String, Object> codeMap = new HashMap<>(2); String result = ""; if (LINUX.equals(type)) { String boisVersion = getBoisVersion(); codeMap.put("boisVersion", boisVersion); System.out.println("boisVersion:" + boisVersion); String uuid = getUUID(); codeMap.put("uuid", uuid); System.out.println("uuid:" + uuid); } else if (WINDOWS.equals(type)) { String processorId = getCPUSerialNumber(); codeMap.put("ProcessorId", processorId); System.out.println("ProcessorId:" + processorId); String serialNumber = getHardDiskSerialNumber(); codeMap.put("SerialNumber", serialNumber); System.out.println("SerialNumber:" + serialNumber); }else{ return ""; } String codeMapStr = JSON.toJSONString(codeMap); String serials = DigestUtil.md5Hex(codeMapStr); result = getSplitString(serials, "-", 4); return result.toUpperCase(); } public static String getSplitString(String str, String joiner, int number) { StringBuilder sb = new StringBuilder(); int len = str.length(); for (int i = 0; i < len; i += number) { if (i + number <= len) { sb.append(str, i, i + number); } else { sb.append(str.substring(i)); } if (i + number < len) { sb.append(joiner); } } return sb.toString(); } /** * 获取CPU序列号 * * @return * @throws IOException */ public static String getCPUSerialNumber() { String next; try { Process process = Runtime.getRuntime().exec(new String[]{"wmic", "cpu", "get", "ProcessorId"}); process.getOutputStream().close(); Scanner sc = new Scanner(process.getInputStream()); String serial = sc.next(); next = sc.next(); } catch (IOException e) { throw new RuntimeException("获取CPU序列号失败"); } return next; } /** * 获取 硬盘序列号(Windows) * * @return * @throws IOException * @throws InterruptedException */ public static String getHardDiskSerialNumber() { try { Process process = Runtime.getRuntime().exec(new String[]{"wmic", "path", "win32_physicalmedia", "get", "serialnumber"}); process.getOutputStream().close(); Scanner sc = new Scanner(process.getInputStream()); String serial = sc.next(); return sc.next(); } catch (IOException e) { throw new RuntimeException("获取硬盘序列号失败"); } } /** * 获取系统序列号(linux) * * @return */ public static String getUUID() { String result = ""; try { Process process = Runtime.getRuntime().exec("sudo dmidecode -s system-uuid"); InputStream in; BufferedReader br; in = process.getInputStream(); br = new BufferedReader(new InputStreamReader(in)); while (in.read() != -1) { result = br.readLine(); } br.close(); in.close(); process.destroy(); System.out.println("获取序列号:" + result); } catch (Throwable e) { e.printStackTrace(); } return result; } /** * bois版本号(linux) * * @return */ public static String getBoisVersion() { String result = ""; Process p; try { // 管道 p = Runtime.getRuntime().exec("sudo dmidecode -s bios-version"); BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream())); String line; while ((line = br.readLine()) != null) { result += line; break; } br.close(); } catch (IOException e) { System.out.println("获取主板信息错误"); } return result; } }
以上就是java获取机器码实现demo的详细内容,更多关于java获取机器码的资料请关注脚本之家其它相关文章!