0

I have written a code that saves "arp -a" data into notepad file. The problem is that this output comes like a single string but I would like to display the arp cache table into rows and columns format (like most network monitoring tools do). I need a way to get the arp table in rows and columns so I can use that data structure to perform further tasks. Below is some code I came through:

public String getMacAddress() throws SocketException, UnknownHostException{
    NetworkInterface netint = NetworkInterface.getByInetAddress(InetAddress.getLocalHost());
    StringBuilder sb = new StringBuilder();

    byte [] mac=netint.getHardwareAddress();
    for (int i = 0; i < mac.length; i++) {
        sb.append(String.format("%02X%s", mac[i],""));      
    }
    return sb.toString();
}
public  StringBuilder IPtoHex(String IP){
    String [] address  = IP.replace(".", ":").split(":");
    StringBuilder str = new StringBuilder();
    for(String s : address){
        s=Integer.toHexString(Integer.parseInt(s));
        s=(s.length()<2)?"0"+s:s;
        str.append(s);
    }
    ...
}

I want the output to look like something like this:

expected arp table output

Guido
  • 46,642
  • 28
  • 120
  • 174

1 Answers1

1

Create a Java class that represents the structure you need. Something like this:

public class ArpEntry {
   private String ip;
   private String mac;

   // getters and setters
}

When you run arp -a (this question has a lot of options, from using a third-party library to executing the command and parsing the output), you only need to create a new ArpEntryobject for every arp entry and set its properties to make them available to perform any further tasks you need.

Guido
  • 46,642
  • 28
  • 120
  • 174