Android - Interactive shell (Runtime.getRuntime().exec())

 ̄綄美尐妖づ 提交于 2020-01-03 02:53:17

问题


How to run an interactive shell on android? (rooted device)

I need the nexts steps:

1 - Execute shell process (onCreate)

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

2 - Get the output (onCreate)

BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream));
//dos is a field (a class attribute)
dos = new DataOutputStream(p.getOutputStream());
new Thread(new Runnable() {

    @Override
    public void run() {
         String l;
         //wait the console output and write it
         while((l = br.readLine()) != null) {
              Log.v("info", l);
         }
    }
}).start();

3 - Execute a command (Button -> onClick())

cmd("wpa_cli");

The cmd method is:

public void cmd(String cmd) {
   dos.writeBytes(cmd + "\n");
   dos.flush();
}

The Log never shows the console output.

Next step:

4 - Get the output of a subcommand of wpa_cli (another Button -> onClick())

cmd("help");

Should show the wpa_cli help but it doesn't work.
If i press the button many times appears incomplete output (help)

What is the correct way to initialize a process when the Activity is creating and keep it active to interact?

Thanks.

PostData
I replaced the line Log.v("info", l);
with

fos.write(l);
Message msg = handlerTest.obtainMessage();
msg.obj = l;
handlerTest.sendMessage(msg);

fos -> FileOutputStream object

Handler handlerTest = new Handler() {
    @Override
    handleMessage(Message msg) {
       if (msg != null) {
         //alert... (String)msg.obj;
       }
    }
}; 

And only displays the alert from the direct commands, as help for example. The command status only work when the stream is closed. (after execute cmd("quit"); and cmd("exit");)

I don't understand. stdout is more that one? I can interact with the output without close the stream?

With adb shell i read the output file (created after closing the stream) and it's complete.

PostData2:

The problem is the buffer. Between the binary file (executed) output and java process i can disable the buffer without using Native I/O? The Android Terminal Emulator works fine, it's using NIO?


回答1:


Solution by OP.

Without using the tool stdbuf (of coreutils) in Android I found the next solution:

I'm using two processes called process1 and process2: both -> new ProcessBuilder(new String[]{"su", "-c", "sh"}).start();

In the first process I run wpa_cli

dos1.write("wpa_cli".getBytes());
dos1.flush();

In a thread I wait 5 seconds and run wpa_cli in the second process.

dos2.write("wpa_cli".getBytes());
dos2.flush();

To get the output from wpa_cli is necessary to close it with quit

dos1.write("quit".getBytes());
dos1.flush();

The main thread that work with both processes:

new Thread(new Runnable() {
                @Override
                public void run() {
                    boolean firstProcess = true;
                    while (follow) {
                        try {
                            Thread.sleep(5000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        if (follow) {
                            if (firstProcess) {
                                cmd("wpa_cli", dos2);
                                cmd("quit", dos1);
                            } else {
                                cmd("wpa_cli", dos1);
                                cmd("quit", dos2);
                            }
                            firstProcess = !firstProcess;
                        }
                    }
                }
            }).start();

Important: Both processes have this (when the process is created):

new Thread(new Runnable() {
                    @Override
                    public void run() {
                        BufferedReader br = new BufferedReader(new InputStreamReader(process1.getInputStream()));
                        String l;
                        try {
                            Message msg;
                            while ((l = br.readLine()) != null) {
                                msg = handlerStdout.obtainMessage();
                                msg.obj = l;
                                handlerStdout.sendMessage(msg);
                            }

                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }).start();

The handler handlerStdout does the desired job. It receives the output of wpa_cli every 5 seconds: the output comes from process1 and process2 alternatively


An alternative solution would be to compile coreutils or stdbuf for Android. Or install compiled coreutils from http://forum.xda-developers.com/showthread.php?t=2613243 (for me not work stdbuf because the libstdbuf.so not found)

With stdbuf -oL wpa_cli the output isn't buffered (buffer one line). Without it the output is buffered (4k).



来源:https://stackoverflow.com/questions/31296374/android-interactive-shell-runtime-getruntime-exec

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!