70404820885cf6718ff3451d0fd0b28051fadf00.svn-base
2.54 KB
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
package com.thinkgem.jeesite.tools;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.google.common.collect.Lists;
public class Test {
public static void main(String[] args) {
List<String> list = getIPList();
if(list.contains("172.16.58.79")) {
System.out.println("true" );
}
}
public static List<String> getIPList() {
List<String> res_list = Lists.newArrayList();
Enumeration<NetworkInterface> e1;
try {
e1 = NetworkInterface.getNetworkInterfaces();
while (e1.hasMoreElements()) {
NetworkInterface ni = e1.nextElement();
List<String> ips = Lists.newLinkedList();
Enumeration<InetAddress> e2 = ni.getInetAddresses();
while (e2.hasMoreElements()) {
InetAddress ia = e2.nextElement();
if(ia instanceof Inet6Address) {
continue; // omit IPv6 address
}
ips.add(ia.getHostAddress());
}
res_list.addAll(ips);
}
} catch (SocketException e) {
e.printStackTrace();
}
return res_list;
}
public static Map<String, List<String>> getLocalIP() {
Map<String, List<String>> map = new HashMap<String, List<String>>();
Enumeration<NetworkInterface> e1;
try {
e1 = NetworkInterface.getNetworkInterfaces();
while (e1.hasMoreElements()) {
NetworkInterface ni = e1.nextElement();
List<String> ips = Lists.newLinkedList();
map.put(ni.getName(), ips);
Enumeration<InetAddress> e2 = ni.getInetAddresses();
while (e2.hasMoreElements()) {
InetAddress ia = e2.nextElement();
if(ia instanceof Inet6Address) {
continue; // omit IPv6 address
}
ips.add(ia.getHostAddress());
}
}
} catch (SocketException e) {
e.printStackTrace();
}
return map;
}
}