forked from cirosantilli/java-cheat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGetIps.java
47 lines (42 loc) · 1.89 KB
/
GetIps.java
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
/*
# InetAddress
# NetworkInterface
http://stackoverflow.com/questions/494465/how-to-enumerate-ip-addresses-of-all-enabled-nic-cards-from-java?lq=1
http://stackoverflow.com/questions/8083479/java-getting-my-ip-address
*/
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Enumeration;
public class GetIps {
public static void main(String[] args) {
try {
InetAddress localhost = InetAddress.getLocalHost();
System.out.println("InetAddress.getLocalHost().getHostAddress = " + localhost.getHostAddress());
// Just in case this host has multiple IP addresses....
InetAddress[] allMyIps = InetAddress.getAllByName(localhost.getCanonicalHostName());
if (allMyIps != null && allMyIps.length > 1) {
System.out.println("InetAddress.getAllByName(localhost.getCanonicalHostName())");
for (InetAddress inetAddress : allMyIps) {
System.out.println(" " + inetAddress);
}
}
} catch (UnknownHostException e) {
System.out.println(e);
}
try {
System.out.println("NetworkInterface.getNetworkInterfaces()");
Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces();
while (en.hasMoreElements()) {
NetworkInterface intf = en.nextElement();
System.out.println(" getName() = " + intf.getName() + ", getDisplayName() = " + intf.getDisplayName());
for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements(); ) {
System.out.println(" " + enumIpAddr.nextElement().toString());
}
}
} catch (SocketException e) {
System.out.println(e);
}
}
}