Administrator
2023-04-14 cc0cbfc79a34e1b106fdb998450cd2ad03446126
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package com.mandi.common;
 
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;
import java.util.UUID;
 
import org.apache.commons.lang3.StringUtils;
 
public class Netmethod {
     public static InetAddress getlocaladdr(){
        InetAddress ia=null;
        if(Netmethod.iswindows())
        {
            try {
                ia=InetAddress.getLocalHost();
            } catch (UnknownHostException e) {
                e.printStackTrace();
            }
        }
        if(ia!=null)
            return ia;
        try {
            Enumeration<NetworkInterface> ens = NetworkInterface.getNetworkInterfaces();
            boolean flg=false;
            while (ens.hasMoreElements()) {
                NetworkInterface nif = (NetworkInterface) ens.nextElement();
                UUID.randomUUID();
                Enumeration<InetAddress>  iiss=nif.getInetAddresses();
                System.out.println("mac地址:"+BasicMethod.byte2hexStr(nif.getHardwareAddress()));
                while(iiss.hasMoreElements())
                {
                    InetAddress iis=iiss.nextElement();
                    System.out.println("iis:"+iis.getHostAddress());
                    if(!iis.isLoopbackAddress()&&iis.isSiteLocalAddress()&&iis.getHostAddress().indexOf(":")==-1)
                    {
                        System.out.println("找到inetaddr!!!");
                        ia=iis;
                        flg=true;
                        break;
                    }
                }
                if(flg==true)
                    break;
            }
        } catch (SocketException e) {
            e.printStackTrace();
        }
        return ia;
    }
    
     public static String allmac()
        {
            String re=null;
            Enumeration<NetworkInterface> nis;
            try {
                nis = NetworkInterface.getNetworkInterfaces();
                List<String> nds=new ArrayList<String>();
                while(nis.hasMoreElements())
                {
                    NetworkInterface ni=nis.nextElement();
                    byte[] bs=ni.getHardwareAddress();
                    if(bs==null||bs.length!=6)
                        continue;
                    String str=BasicMethod.byte2hexStr(bs);
                    if(str==null)
                        continue;
                    nds.add(str);
                }
                Collections.sort(nds);
                re=StringUtils.join(nds, "");
            } catch (SocketException e) {
                e.printStackTrace();
            }
            if(re==null)
                re=UUID.randomUUID().toString();
            return re;
        }
     
    public static boolean iswindows()
    {
         if (System.getProperties().getProperty("os.name").toUpperCase().indexOf("WINDOWS") != -1) {
              return true;
              }
       return false;    
    }
    
    private static String getcpunums()
    {
        String result = "";
        Path p=null;
        try {
            p=Files.createTempFile("tmp", ".vbs");
            String vbs = "On Error Resume Next \r\n\r\n" + "strComputer = \".\"  \r\n"
                    + "Set objWMIService = GetObject(\"winmgmts:\" _ \r\n"
                    + "    & \"{impersonationLevel=impersonate}!\\\\\" & strComputer & \"\\root\\cimv2\") \r\n"
                    + "Set colItems = objWMIService.ExecQuery(\"Select * from Win32_Processor\")  \r\n "
                    + "For Each objItem in colItems\r\n " + "    Wscript.Echo objItem.ProcessorId  \r\n "
                    + "    exit for  ' do the first cpu only! \r\n" + "Next                    ";
            System.out.println(p.toString());
                Files.write(p, vbs.getBytes("ISO-8859-1"));
                Process p1 = Runtime.getRuntime().exec("cscript //NoLogo " + p.toString());
                BufferedReader r=new BufferedReader(new InputStreamReader(p1.getInputStream()));
                String s=null;
                StringBuffer sb=new StringBuffer();
                while((s=r.readLine())!=null)
                {
                    sb.append(s);
                }
                result=sb.toString();
                result=result.trim();
            }catch (Exception e) {
                e.printStackTrace();
            }finally{
                if(p!=null)
                    try {
                        Files.deleteIfExists(p);
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
            }
           return result;
    }
    private static String getboardnums()
    {
        String result = "";
        Path p=null;
        try {
            p=Files.createTempFile("tmp2", ".vbs");
            String vbs ="On Error Resume Next \r\n\r\n" + "strComputer = \".\"  \r\n"
            + "Set objWMIService = GetObject(\"winmgmts:\" _ \r\n"
            + "    & \"{impersonationLevel=impersonate}!\\\\\" & strComputer & \"\\root\\cimv2\") \r\n"
            + "Set colItems = objWMIService.ExecQuery(\"Select * from Win32_BaseBoard\")  \r\n "
            + "For Each objItem in colItems\r\n " + "    Wscript.Echo objItem.SerialNumber  \r\n "
            + "    exit for  ' do the first cpu only! \r\n" + "Next                    ";
            System.out.println(p.toString());
                Files.write(p, vbs.getBytes("ISO-8859-1"));
                Process p1 = Runtime.getRuntime().exec("cscript //NoLogo " + p.toString());
                BufferedReader r=new BufferedReader(new InputStreamReader(p1.getInputStream()));
                String s=null;
                StringBuffer sb=new StringBuffer();
                while((s=r.readLine())!=null)
                {
                    sb.append(s);
                }
                result=sb.toString();
                result=result.trim();
            }catch (Exception e) {
                e.printStackTrace();
            }finally{
                if(p!=null)
                    try {
                        Files.deleteIfExists(p);
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
            }
           return result;
    }
    
    private static String getmac()
    {
        String str=null;
        try {
            InetAddress ia=getlocaladdr();
            NetworkInterface nif=NetworkInterface.getByInetAddress(ia);
            byte[] bytes=nif.getHardwareAddress();
            str=BasicMethod.byte2hexStr(bytes);
        } catch (SocketException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return str;
    }
    
    public static String getserial()
    {
        if(iswindows())
        {
            String str=null;
            str=getboardnums();
            if(str!=null&&!str.isEmpty())
            {
                System.out.println("主板序列号:"+str);
                return str;
            }
            str=getcpunums();
            System.out.println("cpu num:"+str);
            if(str==null||str.isEmpty())
            {
                str=getmac();
            }
            return str;
        }else
        {
            System.out.println("非windows,返回mac");
            return getmac();
        }
    }
}