-1

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.

Kamran Ahmed
  • 7,661
  • 4
  • 30
  • 55
Bouba
  • 1
  • 1
  • 3

1 Answers1

1

As you can read there

Android Debug Bridge (adb) is a versatile command line tool that lets you communicate with an emulator instance or connected Android device

You are trying to execute adb commands on android devices, which doesn't make sense, because adb is command line util which runs on computer and there is no adb on android device.

Divers
  • 9,531
  • 7
  • 45
  • 88
  • _"there is no adb on android device"_ Not quite correct. Ether starting from a specific OS version or depending on a manufacturer `adb` binary has been shipped under `system/bin`. _"You are trying to execute adb commands on android devices, which doesn't make sense"_ In most cases it doesn't, unless you know what you're doing... @Bouba [This answer](http://stackoverflow.com/questions/34104119/why-does-exec-start-a-adb-daemon/34112381#34112381) might give you further clarification in addition to the current answer. – Onik Jan 23 '17 at 21:12
  • Any links on that from specific version of android they started to ship it with adb? I don't see how it can be useful. – Divers Jan 23 '17 at 21:20
  • Sure! The link is in my answer provided (namely, [this one](http://www.slideshare.net/tetsu.koba/adbandroid-debug-bridge-how-it-works)). And there is the explanation (starting from slide 36) on how it can be used. – Onik Jan 23 '17 at 21:30
  • Not sure about how relevant that info right now - they are talking about Nexus One device – Divers Jan 23 '17 at 21:41