0

I am trying to run the following command in Runtime exec():

Process p;
try {
    p = Runtime.getRuntime().exec("arp -a");
    BufferedReader in = new BufferedReader(
            new InputStreamReader(p.getInputStream()));

    String inputLine;
    while ((inputLine = in.readLine()) != null) {
        if (!inputLine.contains("incomplete")) {

            found++;
            inputLine = cleanResult(inputLine);
            storeList(inputLine);
        }
    }
    in.close();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    Log.e("check",e.getMessage());
}

And the error that I'm getting is:

    07-14 12:23:16.548: W/System.err(14272): java.io.IOException: Error running exec(). Command: [arp, -n] Working Directory: null Environment: null
07-14 12:23:16.668: W/System.err(14272):    at java.lang.ProcessManager.exec(ProcessManager.java:211)
07-14 12:23:16.668: W/System.err(14272):    at java.lang.Runtime.exec(Runtime.java:168)
07-14 12:23:16.668: W/System.err(14272):    at java.lang.Runtime.exec(Runtime.java:241)
07-14 12:23:16.668: W/System.err(14272):    at java.lang.Runtime.exec(Runtime.java:184)
07-14 12:23:16.678: W/System.err(14272):    at com.app.president.FindDevices$8.run(FindDevices.java:225)
07-14 12:23:16.678: W/System.err(14272):    at android.os.Handler.handleCallback(Handler.java:725)
07-14 12:23:16.678: W/System.err(14272):    at android.os.Handler.dispatchMessage(Handler.java:92)
07-14 12:23:16.678: W/System.err(14272):    at android.os.Looper.loop(Looper.java:137)
07-14 12:23:16.678: W/System.err(14272):    at android.app.ActivityThread.main(ActivityThread.java:5099)
07-14 12:23:16.678: W/System.err(14272):    at java.lang.reflect.Method.invokeNative(Native Method)
07-14 12:23:16.678: W/System.err(14272):    at java.lang.reflect.Method.invoke(Method.java:511)
07-14 12:23:16.678: W/System.err(14272):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:803)
07-14 12:23:16.678: W/System.err(14272):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:570)
07-14 12:23:16.678: W/System.err(14272):    at dalvik.system.NativeStart.main(Native Method)
07-14 12:23:16.678: W/System.err(14272): Caused by: java.io.IOException: Permission denied
07-14 12:23:16.678: W/System.err(14272):    at java.lang.ProcessManager.exec(Native Method)
07-14 12:23:16.678: W/System.err(14272):    at java.lang.ProcessManager.exec(ProcessManager.java:209)
07-14 12:23:16.688: W/System.err(14272):    ... 13 more

The LOG that I have put for check reads:

07-14 12:23:19.971: E/check(14272): Error running exec(). Command: [arp, -a] Working Directory: null Environment: null

What this code does is, it tries to read the arp cache to fetch all MAC address with their respective IP addresses.

Can someone please help me with this and get my code running. I want to know why is the following code giving this error? Are there any permissions required for doing this? The permission I have granted till now are:

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.INTERNET" />

Thanks in advance :)

almightyGOSU
  • 3,731
  • 6
  • 31
  • 41
Ujjwal Syal
  • 377
  • 1
  • 3
  • 12

1 Answers1

2

Add this permission :

android.permission.WRITE_EXTERNAL_STORAGE
Arash GM
  • 10,316
  • 6
  • 58
  • 76
  • 3
    How will this help? I'm not writing anything to any external storage! Then why use this permission? – Ujjwal Syal Jul 14 '14 at 07:55
  • @UjjwalSyal : it's an IO exception so there would be IO permission related to Environment or a root access to execute such commands , maybe super user permission helps. – Arash GM Jul 14 '14 at 09:02
  • Does that mean I need to root my device? Or is there a way to do it without rooting? – Ujjwal Syal Jul 14 '14 at 09:21
  • @UjjwalSyal : yes it needs to root the device. but unfortunately i'm not very experienced with super user permissions!searching would be best! – Arash GM Jul 14 '14 at 09:27
  • What I'm trying to do is, make a net scanner like fing. For that I need to read the ARP cache. Can you suggest any other way to do so? – Ujjwal Syal Jul 14 '14 at 09:30
  • @UjjwalSyal : maybe this helps for reading ARP cache : http://www.flattermann.net/2011/02/android-howto-find-the-hardware-mac-address-of-a-remote-host/ – Arash GM Jul 14 '14 at 09:41
  • @Arash i have same problem, the link above is deprecated. can u help me? – Seyyed Dec 20 '17 at 10:13