2

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.

Nayana_Das
  • 1,789
  • 4
  • 23
  • 48
Wei Ding
  • 21
  • 1
  • 1
  • 4
  • Did you find a different way to acces ARP table? – Oğuzhan Koşar Sep 07 '17 at 10:49
  • How do you access it as *root*??? When I run the `arp` command normally it works, but when I `su` so I can use the `-s` option and try to run it I get "arp: inaccessible or not found"!!! – Michael Jun 26 '22 at 00:18

5 Answers5

2

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;
}
Oğuzhan Koşar
  • 124
  • 1
  • 11
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) {

        }
    }
Vishal Sharma
  • 616
  • 1
  • 6
  • 21
1

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;
}
Dimitrios Desyllas
  • 9,082
  • 15
  • 74
  • 164
rjferguson
  • 603
  • 3
  • 12
1

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.

l-octopus
  • 11
  • 2
  • 4
    Hello and welcome to SO! Please read the [tour](https://stackoverflow.com/tour), and [How do I write a good answer?](https://stackoverflow.com/help/how-to-answer) For example you can separate the complete command to another line in a code block, and add guidance how to install that emulator. – Tomer Shetah Jan 09 '21 at 08:50
0

It seems that an application has not been able to access the arp file since Android 10. link

Saint
  • 1,492
  • 1
  • 11
  • 20