Skip to content

Instantly share code, notes, and snippets.

@shawnfeng0
Created November 26, 2018 07:47
Show Gist options
  • Save shawnfeng0/c6f4c72fcc986d6bc28a425f40a66b0a to your computer and use it in GitHub Desktop.
Save shawnfeng0/c6f4c72fcc986d6bc28a425f40a66b0a to your computer and use it in GitHub Desktop.
public static List<String> getIpAddressString() {
List<String> result = new ArrayList<>();
try {
for (Enumeration<NetworkInterface> enNetI = NetworkInterface
.getNetworkInterfaces(); enNetI.hasMoreElements(); ) {
NetworkInterface netI = enNetI.nextElement();
if (netI.getDisplayName().matches("wlan(.*)") || netI.getDisplayName().matches("eth(.*)")) {
for (Enumeration<InetAddress> enumIpAddr = netI
.getInetAddresses(); enumIpAddr.hasMoreElements(); ) {
InetAddress inetAddress = enumIpAddr.nextElement();
if (inetAddress instanceof Inet4Address && !inetAddress.isLoopbackAddress()) {
result.add(inetAddress.getHostAddress());
}
}
}
}
} catch (SocketException e) {
e.printStackTrace();
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment