0

I try to run shell command on android app by using Runtime class and ndk system/exec functions.

First, i use Runtime class that was guided by other user. execute shell command from android

like this : Runtime.getRuntime().exec("/data/local/tmp/test.sh"); (i double checked file permission it was 777)

but following error happended when i run upper command.

try {
        Process process = Runtime.getRuntime().exec("/data/local/tmp/test.sh");
        BufferedReader reader = new BufferedReader(
                new InputStreamReader(process.getInputStream()));
        int read;
        char[] buffer = new char[4096];
        StringBuffer output = new StringBuffer();
        while ((read = reader.read(buffer)) > 0) {
            output.append(buffer, 0, read);
        }
        reader.close();
        process.waitFor();
        return output.toString();
    } catch (IOException e) {
        throw new RuntimeException(e);
    } catch (InterruptedException e) {
        throw new RuntimeException(e);
    }

error

at com.sec.sq.myapp.apis.test.tasks.test.MyTester.exec(MyTester.java:47)
    at com.sec.sq.myapp.apis.test.tasks.test.MyTester.run(MyTester.java:29) 
    at com.sec.sq.myapp.apis.manager.ThreadManager.doTask(ThreadManager.java:108) 
    at com.sec.sq.myapp.apis.manager.ThreadManager.doTest(ThreadManager.java:81) 
    at com.sec.sq.myapp.apis.manager.ThreadManager.run(ThreadManager.java:118) 
 Caused by: java.io.IOException: error=13, Permission denied

Second, i tried native-ndk system/exec functions but it's also not working.

  • execl("/system/bin/sh", "/data/local/tmp/test.sh", NULL);

: app crashed, and can see permission denied error=13.

system("/system/bin/sh /data/local/tmp/test.sh");

: noting happended and shell not run.

Can somebody help me to find a way how to run shell command on Android app?

Bon Jay
  • 1
  • 1
  • Bear in mind that the file system will also have selinux in force most probably: https://source.android.com/security/selinux/ – g00se Apr 19 '22 at 11:37

1 Answers1

0

You have problem in reading that file.Script is fine. Can you try using this:

chmod u+x /data/local/tmp/test.sh

You can also go through this this link.