Skip to content

Instantly share code, notes, and snippets.

@dizzzz
Created July 2, 2025 18:45
Show Gist options
  • Save dizzzz/572c3cfdbb30abe50e45438dd090b8c4 to your computer and use it in GitHub Desktop.
Save dizzzz/572c3cfdbb30abe50e45438dd090b8c4 to your computer and use it in GitHub Desktop.
Find loopback with enumeration loop
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
public class Enumerators {
public static void main(final String[] args) {
try {
final List<String> localLoopbackAddresses = new ArrayList<>();
for (final NetworkInterface networkInterface : NetworkInterface.networkInterfaces().toList()) {
if (networkInterface.isLoopback()) {
final Enumeration<InetAddress> inetAddresses = networkInterface.getInetAddresses();
while(inetAddresses.hasMoreElements()){
final String hostAddress = inetAddresses.nextElement().getHostAddress();
if(hostAddress!=null){
if(hostAddress.contains("%")) {
final int position=hostAddress.indexOf("%");
localLoopbackAddresses.add(hostAddress.substring(0,position));
} else {
localLoopbackAddresses.add(hostAddress);
}
}
}
}
}
localLoopbackAddresses.forEach(System.out::println);
} catch (final SocketException e) {
System.err.println("Failed to enumerate interfaces: " + e.getMessage());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment