Created
July 2, 2025 18:45
-
-
Save dizzzz/572c3cfdbb30abe50e45438dd090b8c4 to your computer and use it in GitHub Desktop.
Find loopback with enumeration loop
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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