自动化立体仓库 - 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
119
120
121
122
123
124
125
126
127
128
129
package com.zy.system.entity.license;
 
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
 
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
 
/**
 * 用于获取客户服务器的基本信息,如:IP、Mac地址、CPU序列号、主板序列号等
 */
public abstract class AbstractServerInfos {
    private static Logger logger = LogManager.getLogger(AbstractServerInfos.class);
 
    /**
     * 组装需要额外校验的License参数
     */
    public LicenseCheck getServerInfos(){
        LicenseCheck result = new LicenseCheck();
 
        try {
            result.setIpAddress(this.getIpAddress());
        }catch (Exception e){
            logger.error("获取服务器IP失败",e);
        }
 
        try {
            result.setMacAddress(this.getMacAddress());
        }catch (Exception e){
            logger.error("获取服务器MAC失败",e);
        }
 
        try {
            result.setCpuSerial(this.getCPUSerial());
        }catch (Exception e){
            logger.error("获取服务器CPU序列号失败",e);
        }
 
        try {
            result.setMainBoardSerial(this.getMainBoardSerial());
        }catch (Exception e){
            logger.error("获取服务器主板序列号失败",e);
        }
 
        return result;
    }
 
    /**
     * 获取IP地址
     */
    protected abstract List<String> getIpAddress() throws Exception;
 
    /**
     * 获取Mac地址
     */
    protected abstract List<String> getMacAddress() throws Exception;
 
    /**
     * 获取CPU序列号
     */
    protected abstract String getCPUSerial() throws Exception;
 
    /**
     * 获取主板序列号
     */
    protected abstract String getMainBoardSerial() throws Exception;
 
    /**
     * 获取当前服务器所有符合条件的InetAddress
     */
    protected List<InetAddress> getLocalAllInetAddress() throws Exception {
        List<InetAddress> result = new ArrayList<>(4);
 
        // 遍历所有的网络接口
        for (Enumeration networkInterfaces = NetworkInterface.getNetworkInterfaces(); networkInterfaces.hasMoreElements(); ) {
            NetworkInterface iface = (NetworkInterface) networkInterfaces.nextElement();
            // 在所有的接口下再遍历IP
            for (Enumeration inetAddresses = iface.getInetAddresses(); inetAddresses.hasMoreElements(); ) {
                InetAddress inetAddr = (InetAddress) inetAddresses.nextElement();
 
                //排除LoopbackAddress、SiteLocalAddress、LinkLocalAddress、MulticastAddress类型的IP地址
                if(!inetAddr.isLoopbackAddress() /*&& !inetAddr.isSiteLocalAddress()*/
                        && !inetAddr.isLinkLocalAddress() && !inetAddr.isMulticastAddress()){
                    result.add(inetAddr);
                }
            }
        }
 
        return result;
    }
 
    /**
     * 获取某个网络接口的Mac地址
     */
    protected String getMacByInetAddress(InetAddress inetAddr){
        try {
            byte[] mac = NetworkInterface.getByInetAddress(inetAddr).getHardwareAddress();
            if (mac == null) {
                return null;
            }
            StringBuffer stringBuffer = new StringBuffer();
 
            for(int i=0;i<mac.length;i++){
                if(i != 0) {
                    stringBuffer.append("-");
                }
 
                //将十六进制byte转化为字符串
                String temp = Integer.toHexString(mac[i] & 0xff);
                if(temp.length() == 1){
                    stringBuffer.append("0" + temp);
                }else{
                    stringBuffer.append(temp);
                }
            }
 
            return stringBuffer.toString().toUpperCase();
        } catch (SocketException e) {
            e.printStackTrace();
        }
 
        return null;
    }
 
}