0

I'm trying to write a program that will show a DHCP client list in Java. I want to get the IP addresses, MAC addresses and the host names of all the devices connected to my wifi network.

I have a Belkin router. Its homepage has a 'DHCP client list' option which when clicked shows me this table :

enter image description here

That's exactly what I'm looking for. But I want to show all this data in the form of a list in a Java Swing program. I also want to be able to update this list by pressing a refresh button. Is there any way to achieve this?

It should look something like this :

enter image description here

I've written a basic java program that shows all the IP addresses that are online. Here's the code :

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))
            {
            // machine is turned on and can be pinged
            System.out.println(address + "is online");
            }
            else if (!address.getHostAddress().equals(address.getHostName()))
            {
            // machine is known in a DNS lookup
            System.out.println(address + "is in a DNS lookup");
            }
            else
            {
            // the host address and host name are equal, meaning the host name could not be resolved
            System.out.println(address + " is not online");
        }
        }
}

But this doesn't serve the purpose and it's really slow. I want to write a Swing program that'll show me the DHCP client list as seen in the image above.

Any help is appreciated.

Chinmay Dabke
  • 5,070
  • 11
  • 40
  • 63
  • 1
    Your question appears to be a bit broad. You want to create a Swing program to display data, fine, but what step in particular are you stuck on? Where's your Swing code attempt? Have you broken the problem down into small constituent steps and tried to solve each step one at a time? If not, you will want to start with this, I think. – Hovercraft Full Of Eels Jun 25 '15 at 20:49
  • @HovercraftFullOfEels I'm stuck on the step where I fetch the data from the router's web interface and send it to my Java Swing program. I don't know how to get started. Please guide me with the code. – Chinmay Dabke Jun 26 '15 at 17:14
  • @Camickr please help me out – Chinmay Dabke Jun 26 '15 at 20:18

1 Answers1

1

I would think about three possible alternatives:

1-The one you implemented that is slow but it can work. You need to find a JAVA API to get the MAC addresses of received messages (I don't know if it exists or not). You can also send ARP messages asking "who has this IP address) and obtain the MAC address from the response. Use some Java interface for pcap library: jNetPcap vs Jpcap , http://jnetpcap.com/

2-Create an app that accesses your router web interface using HTTP and sending the appropriate messages with data as if you were using the UI. In this way you can programatically follow the steps a human would go and get the list that you browser shows, parse it and obtain the data.

3-If the router/access point provides a web API, which I doubt, you can use it.

Community
  • 1
  • 1
rodolk
  • 5,606
  • 3
  • 28
  • 34
  • The second option is best for me. The app which you're talking about, can it be written in java? I'd want to run it in the background. This way that app will communicate with the web interface and send my Java Swing program the required data. Can you please help me with the code? – Chinmay Dabke Jun 26 '15 at 13:31
  • 1
    I agree. It can very well be written in Java. You have to use client HTTP library for have and you will learn a little of HTTP and HTML if you don't know yet. You can do it completely in Java you generate your own HTTP messages. The only complication is if you have to use HTTPS but that is also possible with Java. In order to learn the messages you have to send and the data you can use Fiddler to sniff your HTTP messages. – rodolk Jun 26 '15 at 13:36
  • Okay. I'm pretty new to this. Can you help me with the code? – Chinmay Dabke Jun 26 '15 at 20:11
  • There are plenty of examples in the web. For HTTP client, here you have one link: http://www.mkyong.com/java/apache-httpclient-examples/ . For Fiddler, download it from here: http://www.telerik.com/fiddler . This is a good opportunity to learn. Read the documentation, install Fiddler and then you will see all HTTP requests. – rodolk Jun 27 '15 at 22:58