how can i passs input in adb shell using python script?

Viewed 75

I am facing a problem, have to pass an input after I run command: adb shell libtest_ip through python:

 import subprocess
 command = 'adb shell libtest_ip'
 p = subprocess.Popen(command, shell=True,
                         stdout=subprocess.PIPE, stderr=subprocess.PIPE)

after this I have to pass input like 1 or en_us etc.. but as soon as the command to sun binary(libtest_ip is a binary), is executed, it gets stuck.

Please help me if anyone have idea how to solve this?

1 Answers

I think your best bet is pexpect . Specially you can take a look at script.py that would help you creating the interactive script.

Basically, you should end up with something like this

    ...
    self.child.expect('Whatever')
    self.child.sendline('1')
    self.child.expect('Whatever 2')
    self.child.sendline('en-us')
    ...

update

Your example should work, try

#! /usr/bin/env python3

import pexpect
print(pexpect.run('/bin/echo hello'))

and running it should output

% ./test-pexpect.py 
b'hello\r\n'
Related