自动化立体仓库 - WMS系统
zwl
昨天 780ff45fdc167cadf4724c6c94530929b7445aab
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
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<String> getIpAddress() throws Exception {
        List<String> result = null;
 
        //获取所有网络接口
        List<InetAddress> 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<String> getMacAddress() throws Exception {
        List<String> result = null;
 
        //1. 获取所有网络接口
        List<InetAddress> 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();
    }
}