0

Currently I'am working on an aplication via Android Studio, that executes commands using the next sintaxis:

Process p = Runtime.getRuntime().exec(new String[] { "su", "-c", "ip link show"});

I'am working with the device IMX7D_PICO (it uses Android Things as SO). It is rooted, as is shown in the following pic:

enter image description here

But, when I run a command as root on Android Studio, I get the next error:

W/System.err: java.io.IOException: Cannot run program "su": error=13, Permission denied
W/System.err:     at java.lang.ProcessBuilder.start(ProcessBuilder.java:1048)
at java.lang.Runtime.exec(Runtime.java:692)
at java.lang.Runtime.exec(Runtime.java:560)

I don't know why su works only on adb. In other hand, I know that there are others ways to obtain the data of ip link show, but the next step, it's open a socket RAW with the command that I said. So, I need to run process as su via Android Studio.

Onik
  • 19,396
  • 14
  • 68
  • 91

2 Answers2

1

An app's process has less privileges compared to those of the shell's, resulting in

java.io.IOException: Cannot run program "su": error=13, Permission denied

For the permission to be granted you should install SuperSU app or alike and follow the app's prompts while your app is trying to su with Runtime.getRuntime().exec("su").

Once the process has gotten root you can grab the standard input of the root process and write the command(s) to it, reading its standard output. For more details see: execute shell command from android.

Onik
  • 19,396
  • 14
  • 68
  • 91
0

I don't know why su doesn't work. I 'suspect' that Android Things is using a single user that is root already, so maybe executing the command without using su would work.

On the other part of your question. Parsing the output of system commands is not the best way of getting info, in this case, you can probably get what you want using the Android class NetworkInterface https://developer.android.com/reference/java/net/NetworkInterface#getNetworkInterfaces().

You can manage raw sockets using the Android framework classes too.

shalafi
  • 3,926
  • 2
  • 23
  • 27
  • About the first commentary: without su doesn't work, it returns "request send failed: Permission denied". About the another, I will implement it. – Antonio Delgado Nov 23 '20 at 21:00