1

I need to write a java program, that gets all the computer network interfaces and scans for IP addresses and MAC addresses within the subnet.

I'm not exactly sure how to do that, but I found there's a method called:

Arping.scan(deviceName, network, mask, timeout);

Is it what I'm looking for? What does it return? There aren't a lot of comments, and I'm new to this, so I can't figure out myself. Please help!

1 Answers1

1

public void checkHosts(String subnet){
   int timeout=1000;
   for (int i=1;i<254;i++){
       String host=subnet + "." + i;
       if (InetAddress.getByName(host).isReachable(timeout)){
           System.out.println(host + " is reachable");
       }
   }
}
  • But this works only if LAN is class D, right? This won't work for IP like 87.249.5.218 – Max Khabrat Nov 12 '14 at 17:34
  • I did something like this once, if you heavily thread it you can scan a class B range in a few minutes (with 3 retries each and incremental backoff). I ran somewhere in the area of 10k threads (You can run many because most of the time you're waiting for isReachable to time out). – Bill K Nov 12 '14 at 19:06
  • 2
    Just a note that this uses ICMP ECHO, not ARP. http://docs.oracle.com/javase/7/docs/api/java/net/InetAddress.html#isReachable(int) – Michael Nov 04 '16 at 01:43