access ARP table without root in Android

Wei Ding picture Wei Ding · Mar 8, 2017 · Viewed 8.1k times · Source

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.

Answer

Oğuzhan Koşar picture Oğuzhan Koşar · Sep 8, 2017

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;
}