Search code examples
pythonshellterminalsubprocessadb

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


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?


Solution

  • 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".