Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

access ARP table without root in Android

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.

like image 444
Wei Ding Avatar asked Jan 19 '26 02:01

Wei Ding


1 Answers

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;
}
like image 173
Oğuzhan Koşar Avatar answered Jan 20 '26 19:01

Oğuzhan Koşar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!