Find all IP addresses on the LAN

I want to find all the IP addresses of devices on the local network. I am currently connected to Java code. The useful Advanced IP Scanner utility is able to find various IP addresses in subnet 192.168.178/24 :

According to this answer, I built my code as follows:

 import java.io.IOException; import java.net.InetAddress; public class IPScanner { public static void checkHosts(String subnet) throws IOException { int timeout = 100; for (int i = 1; i < 255; i++) { String host = subnet + "." + i; if (InetAddress.getByName(host).isReachable(timeout)) { System.out.println(host + " is reachable"); } } } public static void main(String[] arguments) throws IOException { checkHosts("192.168.178"); } } 

Unfortunately, this does not print any results, which means that IP addresses are not available. What for? There are devices on my local network, for example, in Advanced IP Scanner scanning.

+7
java networking network-scan
source share
3 answers

InetAddress.isReachable will use ICMP ECHO REQUEST (as with ping) or a request for port 7 (echo port): http://docs.oracle.com/javase/7/docs/api/java/net/InetAddress.html#isReachable % 28int% 29

The pre-IP scanner may use a different method of locating hosts (for example, a request for the radmin port or a request for http).

The host may be turned on but not responding to an ICMP ECHO request.

Are you trying to ping one of the host from the command line?

+2
source share

Try increasing the timeout. I used about 5000 ms, it helped me. If you do not want to wait 5000 ms * 254 = 21 minutes, try also this code with parallel ping to addresses:

 public static void getNetworkIPs() { final byte[] ip; try { ip = InetAddress.getLocalHost().getAddress(); } catch (Exception e) { return; // exit method, otherwise "ip might not have been initialized" } for(int i=1;i<=254;i++) { final int j = i; // i as non-final variable cannot be referenced from inner class new Thread(new Runnable() { // new thread for parallel execution public void run() { try { ip[3] = (byte)j; InetAddress address = InetAddress.getByAddress(ip); String output = address.toString().substring(1); if (address.isReachable(5000)) { System.out.println(output + " is on the network"); } else { System.out.println("Not Reachable: "+output); } } catch (Exception e) { e.printStackTrace(); } } }).start(); // dont forget to start the thread } } 

Worked great for me.

+1
source share

Perhaps try using InetAddress.getByAddress(host) instead of getByName , for example:

  InetAddress localhost = InetAddress.getLocalHost(); byte[] ip = localhost.getAddress(); for (int i = 1; i <= 254; i++) { try { ip[3] = (byte)i; InetAddress address = InetAddress.getByAddress(ip); if (address.isReachable(100)) { output = address.toString().substring(1); System.out.print(output + " is on the network"); } } 

I took this sample for autodetection code from here

0
source share

All Articles