access ARP table without root in Android

11,203

Solution 1

I can access ARP table without root. Here is my code:

string GetMACAddressviaIP(string ipAddr)
{
    string result = "";
    ostringstream ss;
    ss << "/proc/net/arp";
    string loc = ss.str();

    ifstream in(loc);
    if(!in)
    {
        printf("open %s failed\n",loc.c_str());
        return result;
    }

    string line;
    while(getline(in, line)){
        if(strstr(line.c_str(), ipAddr.c_str()))
        {
            const char *buf = strstr(line.c_str(), ":") - 2;
            int counter = 0;
            stringstream ss;
            while(counter < 17)
            {
                ss << buf[counter];
                counter++;
            }
            result = ss.str();
        }
    }

    return result;
}

Solution 2

For better understanding, using Java language, you can retrieve the ARP table in android as:

    BufferedReader br = null;
    ArrayList<ClientScanResult> result = new ArrayList<ClientScanResult>();

    try {
        br = new BufferedReader(new FileReader("/proc/net/arp"));
        String line;
        while ((line = br.readLine()) != null) {
           System.out.println(line);
    }
    }Catch(Exception e){
    }finally {
        try {
            br.close();
        } catch (IOException e) {

        }
    }

Solution 3

As an alternative to previous answers a "no coding" solution is to install a linux terminal emulator on your device and use the arp -a command to view the arp table.

Solution 4

I recommend something like the following for Java. This combines the answers for the other two. It's not tested, so it might need some tweaks, but you get the idea.

public String getMacAddressForIp(final String ipAddress) {
    try (BufferedReader br = new BufferedReader(new FileReader("/proc/net/arp"))) {
        String line;
        while ((line = br.readLine()) != null) {
            if (line.contains(ipAddress)) {
                final int macStartIndex = line.indexOf(":") - 2;
                final int macEndPos = macStartIndex + 17;
                if (macStartIndex >= 0 && macEndPos < line.length()) {
                    return line.substring(macStartIndex, macEndPos);
                } else {
                    Log.w("MyClass", "Found ip address line, but mac address was invalid.");
                }
            }
        }
    } catch(Exception e){
        Log.e("MyClass", "Exception reading the arp table.", e);
    }
    return null;
}
Share:
11,203
Wei Ding
Author by

Wei Ding

Updated on July 28, 2022

Comments

  • Wei Ding
    Wei Ding over 1 year

    I am recently working on a Android project that requires to access the ARP table. One of the requirement is to avoid any methods that need a rooted device. So, my question is: is there any way to access the ARP table in android without rooting the device?

    Currently I have found that most of the approaches use the /proc/net/arp for the ARP table access which requires root permission, and I am not sure if this is the only way.