Thursday, 12 October 2023

Run ADB Shell and then interact with console app in Python

I have a situation where I need to run the adb shell command which takes me to the root access of my device.

Then I run the interactive console app against my device using this command: /oem/console --no-logging.

The command returns this output:

  • Console Process: connecting to application...
  • Console Process: connected to application...

After this, I need to PRESS ENTER so that the console accepts events (The arrow cursor is ready to accept invoke events). This console app takes certain events that I can simulate my device's behavior. For example:

  1. invoke put.device.into.charge 1
  2. invoke call.cell.hangup 1
  3. invoke xyz

So my question is, how do I one-by-one enter these commands into this console app and wait a couple of seconds after each command?

This is the code I have so far:

def run_adb_shell_command(command, return_process=False):
    """Run an ADB shell command."""
    cmd = command.split()
    process = Popen(cmd, stdin=subprocess.PIPE)
    if return_process:
        return process



def send_command_to_console_app(command, process):
    """SEND Commands to Console App."""
    new_line = '\n'.encode()
    # encoded_command = str(command).encode()
    s0 = command
    s1 = 'event 1'
    s2 = 'event 2'
    s3 = 'event 3'
    concat_query = "{}\n".format(s0)

    process.communicate(input=concat_query.encode('utf-8'))[0]
    process.kill()

The problem is that when communicate() function is called for one of the events it just hangs up and never finishes so that I can run the next event.

What is the best approach for this?



from Run ADB Shell and then interact with console app in Python

No comments:

Post a Comment