1

i have developed a code which enables me to ping a range of IP addresses. The results from my ping sweep identifies what local machines are reachable, if reachable there hostname and also if they are not reachable.

I am having trouble at the moment retrieving the MAC address of the reachable IP addresses. Has anyone got a solution for this?

package networkping;

import java.io.IOException;
import java.net.InetAddress;

/**
*
* @author Learner
*/
public class Networkping {


public static void main(String[] args) throws IOException {

    InetAddress localhost = InetAddress.getLocalHost();
    // this code assumes IPv4 is used
    byte[] ip = localhost.getAddress();

    for (int i = 1; i <= 254; i++)
    {
        ip[3] = (byte)i;
        InetAddress address = InetAddress.getByAddress(ip);
        if (address.isReachable(1000))
        {
            System.out.println(address + " Address is reachable" );

        }
        else if (!address.getHostAddress().equals(address.getHostName()))
        {
            System.out.println(address + " Address is known in a DNS lookup and is reachable ");
        }
        else
        {
            System.out.println(address + " Address is unreachable");
        }
    }

}

Thanks

Bob Dalgleish
  • 8,167
  • 4
  • 32
  • 42
  • 1
    Java unfortunately cannot do this without JNI, and depending on network config/OS, perhaps not at all. – nanofarad Dec 09 '13 at 23:07
  • @hexafraction Thank you for your comment and input but i have been giving this as an assignment and the tutor assures me it is possible to do – user3071069 Dec 09 '13 at 23:21
  • @user3071069 Have a look at the link in my answer. It is possible but you need to go by a roundabout way. – Menelaos Dec 09 '13 at 23:23

1 Answers1

1

You cannot do this just using java.

There are two options:

  • Execute another process through java and communicate through standard output with your original java application. Such a process is ARP for example.
  • use JNI as suggested in the comments.

Have a look also at the following resources/answers:

The second resource even has an implemented method calling ARP named private String getMac(String ip)

Community
  • 1
  • 1
Menelaos
  • 23,508
  • 18
  • 90
  • 155