1 | |
package liquibase.util; |
2 | |
|
3 | |
import java.net.InetAddress; |
4 | |
import java.net.NetworkInterface; |
5 | |
import java.net.SocketException; |
6 | |
import java.net.UnknownHostException; |
7 | |
import java.util.Enumeration; |
8 | |
|
9 | 0 | public class NetUtil { |
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
public static InetAddress getLocalHost() throws UnknownHostException, SocketException { |
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | 0 | String osName = System.getProperty("os.name"); |
21 | 0 | if (osName != null && osName.toLowerCase().contains("windows")) { |
22 | 0 | return InetAddress.getLocalHost(); |
23 | |
} |
24 | |
|
25 | 0 | InetAddress lch = null; |
26 | 0 | Enumeration<NetworkInterface> e = NetworkInterface.getNetworkInterfaces(); |
27 | |
|
28 | 0 | while (e.hasMoreElements()) { |
29 | 0 | NetworkInterface i = e.nextElement(); |
30 | |
|
31 | 0 | Enumeration<InetAddress> ie = i.getInetAddresses(); |
32 | 0 | if (!ie.hasMoreElements()) { |
33 | 0 | break; |
34 | |
} |
35 | 0 | lch = ie.nextElement(); |
36 | 0 | if (!lch.isLoopbackAddress()) |
37 | 0 | break; |
38 | 0 | } |
39 | 0 | return lch; |
40 | |
} |
41 | |
|
42 | |
} |