Home > Mobile >  Send multiple terminal commands with inputs, in one shell. On Mac
Send multiple terminal commands with inputs, in one shell. On Mac

Time:07-27

I want access a device via adb shell commands. The device is connected via USB. Manually I can access that via the terminal with the following commands in the sequence:

  1. cd /Users/xx/xx/ # Change to path
  2. ./adb shell # access the add shell
  3. enter password # the terminal expects a input of a password
  4. run some more commands within the adb shell

What I have tried:

subprocess.run('cd /Users/michael/Documents/platform-tools/ ;./adb shell', shell=True)

it requires me to input the password, for that I wanted to use the input function:

subprocess.run('cd /Users/michael/Documents/platform-tools/ ;./adb shell', shell=True, input=*password*)

When I run that I get the error for the input variable: "TypeError: a bytes-like object is required, not 'str'".

For what I have understood, the 'commands' I send in step 3&4 are not real terminal commands, but inputs for the adb shell.

Can someone tell me, how I can solve that problem and automate this process in python?

CodePudding user response:

The error asks for a bytes-like object, not a string.

To turn a string literal into a bytes-like object, append b before the string. Put b in front of the string that contains the password, something like b"password".

CodePudding user response:

If I correctly understand your problem, what you only need to do is giving inputs the correct type of argument. For that just convert your string to bytes using for example str.encode('UTF-8') or you can use the bytes() method.

You can even just add a 'b' before your string like b'password'.

Here is a tutorial.

  • Related