I am trying to execute some shell commands on an Android device programmatically. I am able to run some commands but failed to run all of them. As an example I am able to run the following commands:
executeCommandLine(“ls”)
executeCommandLine(“netstat –atun”)
but now I need to run the following commands which aren’t executed properly:
$ adb push netstat3 /data/local/tmp/
$ adb shell
$ chmod 755 /data/local/tmp/netstat3
$ /data/local/tmp/netstat3
I wrote a function to execute the above-mentioned commands in Android. The function gives me the correct output for commands like “ls” and “netstat –atun” but do not give me the right response for the next commands. My executeCommandLine function is as follow:
public String executeCommandLine(String commandLine) {
try {
Process process;
process = Runtime.getRuntime().exec(commandLine);
BufferedReader reader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
String read;
StringBuilder output=new StringBuilder();
while ((read = reader.readLine())!=null){
output.append(read);
output.append("\n");
Log.d("executed command ", output.toString());
}
reader.close();
process.waitFor();
return output.toString();
} catch (IOException e) {
throw new RuntimeException(e);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
I would like to know how can I get the response for all my commands.