提交 | 用户 | 时间
|
58d006
|
1 |
package com.mandi.common; |
A |
2 |
|
|
3 |
import java.io.BufferedReader; |
|
4 |
import java.io.IOException; |
|
5 |
import java.io.InputStreamReader; |
|
6 |
import java.net.InetAddress; |
|
7 |
import java.net.NetworkInterface; |
|
8 |
import java.net.SocketException; |
|
9 |
import java.net.UnknownHostException; |
|
10 |
import java.nio.file.Files; |
|
11 |
import java.nio.file.Path; |
|
12 |
import java.util.ArrayList; |
|
13 |
import java.util.Collections; |
|
14 |
import java.util.Enumeration; |
|
15 |
import java.util.List; |
|
16 |
import java.util.UUID; |
|
17 |
|
|
18 |
import org.apache.commons.lang3.StringUtils; |
|
19 |
|
|
20 |
public class Netmethod { |
|
21 |
public static InetAddress getlocaladdr(){ |
|
22 |
InetAddress ia=null; |
|
23 |
if(Netmethod.iswindows()) |
|
24 |
{ |
|
25 |
try { |
|
26 |
ia=InetAddress.getLocalHost(); |
|
27 |
} catch (UnknownHostException e) { |
|
28 |
e.printStackTrace(); |
|
29 |
} |
|
30 |
} |
|
31 |
if(ia!=null) |
|
32 |
return ia; |
|
33 |
try { |
|
34 |
Enumeration<NetworkInterface> ens = NetworkInterface.getNetworkInterfaces(); |
|
35 |
boolean flg=false; |
|
36 |
while (ens.hasMoreElements()) { |
|
37 |
NetworkInterface nif = (NetworkInterface) ens.nextElement(); |
|
38 |
UUID.randomUUID(); |
|
39 |
Enumeration<InetAddress> iiss=nif.getInetAddresses(); |
|
40 |
System.out.println("mac地址:"+BasicMethod.byte2hexStr(nif.getHardwareAddress())); |
|
41 |
while(iiss.hasMoreElements()) |
|
42 |
{ |
|
43 |
InetAddress iis=iiss.nextElement(); |
|
44 |
System.out.println("iis:"+iis.getHostAddress()); |
|
45 |
if(!iis.isLoopbackAddress()&&iis.isSiteLocalAddress()&&iis.getHostAddress().indexOf(":")==-1) |
|
46 |
{ |
|
47 |
System.out.println("找到inetaddr!!!"); |
|
48 |
ia=iis; |
|
49 |
flg=true; |
|
50 |
break; |
|
51 |
} |
|
52 |
} |
|
53 |
if(flg==true) |
|
54 |
break; |
|
55 |
} |
|
56 |
} catch (SocketException e) { |
|
57 |
e.printStackTrace(); |
|
58 |
} |
|
59 |
return ia; |
|
60 |
} |
|
61 |
|
|
62 |
public static String allmac() |
|
63 |
{ |
|
64 |
String re=null; |
|
65 |
Enumeration<NetworkInterface> nis; |
|
66 |
try { |
|
67 |
nis = NetworkInterface.getNetworkInterfaces(); |
|
68 |
List<String> nds=new ArrayList<String>(); |
|
69 |
while(nis.hasMoreElements()) |
|
70 |
{ |
|
71 |
NetworkInterface ni=nis.nextElement(); |
|
72 |
byte[] bs=ni.getHardwareAddress(); |
|
73 |
if(bs==null||bs.length!=6) |
|
74 |
continue; |
|
75 |
String str=BasicMethod.byte2hexStr(bs); |
|
76 |
if(str==null) |
|
77 |
continue; |
|
78 |
nds.add(str); |
|
79 |
} |
|
80 |
Collections.sort(nds); |
|
81 |
re=StringUtils.join(nds, ""); |
|
82 |
} catch (SocketException e) { |
|
83 |
e.printStackTrace(); |
|
84 |
} |
|
85 |
if(re==null) |
|
86 |
re=UUID.randomUUID().toString(); |
|
87 |
return re; |
|
88 |
} |
|
89 |
|
|
90 |
public static boolean iswindows() |
|
91 |
{ |
|
92 |
if (System.getProperties().getProperty("os.name").toUpperCase().indexOf("WINDOWS") != -1) { |
|
93 |
return true; |
|
94 |
} |
|
95 |
return false; |
|
96 |
} |
|
97 |
|
|
98 |
private static String getcpunums() |
|
99 |
{ |
|
100 |
String result = ""; |
|
101 |
Path p=null; |
|
102 |
try { |
|
103 |
p=Files.createTempFile("tmp", ".vbs"); |
|
104 |
String vbs = "On Error Resume Next \r\n\r\n" + "strComputer = \".\" \r\n" |
|
105 |
+ "Set objWMIService = GetObject(\"winmgmts:\" _ \r\n" |
|
106 |
+ " & \"{impersonationLevel=impersonate}!\\\\\" & strComputer & \"\\root\\cimv2\") \r\n" |
|
107 |
+ "Set colItems = objWMIService.ExecQuery(\"Select * from Win32_Processor\") \r\n " |
|
108 |
+ "For Each objItem in colItems\r\n " + " Wscript.Echo objItem.ProcessorId \r\n " |
|
109 |
+ " exit for ' do the first cpu only! \r\n" + "Next "; |
|
110 |
System.out.println(p.toString()); |
|
111 |
Files.write(p, vbs.getBytes("ISO-8859-1")); |
|
112 |
Process p1 = Runtime.getRuntime().exec("cscript //NoLogo " + p.toString()); |
|
113 |
BufferedReader r=new BufferedReader(new InputStreamReader(p1.getInputStream())); |
|
114 |
String s=null; |
|
115 |
StringBuffer sb=new StringBuffer(); |
|
116 |
while((s=r.readLine())!=null) |
|
117 |
{ |
|
118 |
sb.append(s); |
|
119 |
} |
|
120 |
result=sb.toString(); |
|
121 |
result=result.trim(); |
|
122 |
}catch (Exception e) { |
|
123 |
e.printStackTrace(); |
|
124 |
}finally{ |
|
125 |
if(p!=null) |
|
126 |
try { |
|
127 |
Files.deleteIfExists(p); |
|
128 |
} catch (IOException e) { |
|
129 |
// TODO Auto-generated catch block |
|
130 |
e.printStackTrace(); |
|
131 |
} |
|
132 |
} |
|
133 |
return result; |
|
134 |
} |
|
135 |
private static String getboardnums() |
|
136 |
{ |
|
137 |
String result = ""; |
|
138 |
Path p=null; |
|
139 |
try { |
|
140 |
p=Files.createTempFile("tmp2", ".vbs"); |
|
141 |
String vbs ="On Error Resume Next \r\n\r\n" + "strComputer = \".\" \r\n" |
|
142 |
+ "Set objWMIService = GetObject(\"winmgmts:\" _ \r\n" |
|
143 |
+ " & \"{impersonationLevel=impersonate}!\\\\\" & strComputer & \"\\root\\cimv2\") \r\n" |
|
144 |
+ "Set colItems = objWMIService.ExecQuery(\"Select * from Win32_BaseBoard\") \r\n " |
|
145 |
+ "For Each objItem in colItems\r\n " + " Wscript.Echo objItem.SerialNumber \r\n " |
|
146 |
+ " exit for ' do the first cpu only! \r\n" + "Next "; |
|
147 |
System.out.println(p.toString()); |
|
148 |
Files.write(p, vbs.getBytes("ISO-8859-1")); |
|
149 |
Process p1 = Runtime.getRuntime().exec("cscript //NoLogo " + p.toString()); |
|
150 |
BufferedReader r=new BufferedReader(new InputStreamReader(p1.getInputStream())); |
|
151 |
String s=null; |
|
152 |
StringBuffer sb=new StringBuffer(); |
|
153 |
while((s=r.readLine())!=null) |
|
154 |
{ |
|
155 |
sb.append(s); |
|
156 |
} |
|
157 |
result=sb.toString(); |
|
158 |
result=result.trim(); |
|
159 |
}catch (Exception e) { |
|
160 |
e.printStackTrace(); |
|
161 |
}finally{ |
|
162 |
if(p!=null) |
|
163 |
try { |
|
164 |
Files.deleteIfExists(p); |
|
165 |
} catch (IOException e) { |
|
166 |
// TODO Auto-generated catch block |
|
167 |
e.printStackTrace(); |
|
168 |
} |
|
169 |
} |
|
170 |
return result; |
|
171 |
} |
|
172 |
|
|
173 |
private static String getmac() |
|
174 |
{ |
|
175 |
String str=null; |
|
176 |
try { |
|
177 |
InetAddress ia=getlocaladdr(); |
|
178 |
NetworkInterface nif=NetworkInterface.getByInetAddress(ia); |
|
179 |
byte[] bytes=nif.getHardwareAddress(); |
|
180 |
str=BasicMethod.byte2hexStr(bytes); |
|
181 |
} catch (SocketException e) { |
|
182 |
// TODO Auto-generated catch block |
|
183 |
e.printStackTrace(); |
|
184 |
} |
|
185 |
return str; |
|
186 |
} |
|
187 |
|
|
188 |
public static String getserial() |
|
189 |
{ |
|
190 |
if(iswindows()) |
|
191 |
{ |
|
192 |
String str=null; |
|
193 |
str=getboardnums(); |
|
194 |
if(str!=null&&!str.isEmpty()) |
|
195 |
{ |
|
196 |
System.out.println("主板序列号:"+str); |
|
197 |
return str; |
|
198 |
} |
|
199 |
str=getcpunums(); |
|
200 |
System.out.println("cpu num:"+str); |
|
201 |
if(str==null||str.isEmpty()) |
|
202 |
{ |
|
203 |
str=getmac(); |
|
204 |
} |
|
205 |
return str; |
|
206 |
}else |
|
207 |
{ |
|
208 |
System.out.println("非windows,返回mac"); |
|
209 |
return getmac(); |
|
210 |
} |
|
211 |
} |
|
212 |
} |