package com.zy.system.entity.license; import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.InetAddress; import java.nio.charset.Charset; import java.util.List; import java.util.stream.Collectors; /** * 用于获取客户Windows服务器的基本信息 */ public class WindowsServerInfos extends AbstractServerInfos { @Override protected List getIpAddress() throws Exception { List result = null; //获取所有网络接口 List inetAddresses = getLocalAllInetAddress(); if(inetAddresses != null && inetAddresses.size() > 0){ result = inetAddresses.stream().map(InetAddress::getHostAddress).distinct().map(String::toLowerCase).collect(Collectors.toList()); } return result; } @Override protected List getMacAddress() throws Exception { List result = null; //1. 获取所有网络接口 List inetAddresses = getLocalAllInetAddress(); if(inetAddresses != null && inetAddresses.size() > 0){ //2. 获取所有网络接口的Mac地址 result = inetAddresses.stream() .map(this::getMacByInetAddress) .filter(mac -> mac != null && !mac.trim().isEmpty()) .distinct() .collect(Collectors.toList()); } return result; } @Override protected String getCPUSerial() throws Exception { String serialNumber = readCommandValue("wmic", "cpu", "get", "processorid"); if (!isBlank(serialNumber)) { return serialNumber; } return readCommandValue("powershell", "-NoProfile", "-Command", "(Get-CimInstance Win32_Processor | Select-Object -First 1 -ExpandProperty ProcessorId)"); } @Override protected String getMainBoardSerial() throws Exception { String serialNumber = readCommandValue("wmic", "baseboard", "get", "serialnumber"); if (!isBlank(serialNumber)) { return serialNumber; } return readCommandValue("powershell", "-NoProfile", "-Command", "(Get-CimInstance Win32_BaseBoard | Select-Object -First 1 -ExpandProperty SerialNumber)"); } private String readCommandValue(String... command) { try { ProcessBuilder processBuilder = new ProcessBuilder(command); processBuilder.redirectErrorStream(true); Process process = processBuilder.start(); process.getOutputStream().close(); try (BufferedReader reader = new BufferedReader( new InputStreamReader(process.getInputStream(), Charset.forName("GBK")))) { String line; while ((line = reader.readLine()) != null) { String value = normalizeCommandOutput(line); if (!isBlank(value)) { return value; } } } } catch (Exception ignored) { } return ""; } private String normalizeCommandOutput(String line) { if (line == null) { return ""; } String value = line.trim(); if (isBlank(value)) { return ""; } String lower = value.toLowerCase(); if ("processorid".equals(lower) || "serialnumber".equals(lower)) { return ""; } if (lower.contains("access denied") || lower.contains("拒绝访问") || lower.contains("get-ciminstance") || lower.contains("fullyqualifiederrorid") || lower.contains("at line:") || lower.contains("categoryinfo") || lower.contains("cimexception") || lower.contains("createprocess error")) { return ""; } return value; } private boolean isBlank(String value) { return value == null || value.trim().isEmpty(); } }