0

I am trying to execute adb commands in an automated way from python script. Please note that I am using python 2.7 in Windows. If I manually do it, the sequence is like this:

C:\Project\python>adb shell
login:<enter login id e.g. root>
Password: <enter password e.g. test>
Last login: Thu Feb  9 12:29:46 UTC 2017 on pts/0
~ # date
date
Thu Feb  9 12:55:06 UTC 2017

I am trying to handle this sequence from python script. I have tried using subprocess.call("adb shell date") but it fails saying that cannot execute commands without logging in. I am not sure how to to pass the login id and password. Sorry for the noob question, as I am very new to Python.

Appreciate your help guys !!

Cheers

2 Answers 2

1

Try subprocess.Popen:

import subprocess

cmd_input = """<enter login id e.g. root>
<enter password e.g. test>
date"""

process = subprocess.Popen(
    "adb shell",
    shell=True,
    stdin=subprocess.PIPE,
    stdout=subprocess.PIPE
)
process.communicate()
for i in cmd_input.split("\n"):
    process.communicate(i + "\n")

Or:

process = subprocess.Popen(
    "adb shell",
    shell=True,
    stdin=subprocess.PIPE,
    stdout=subprocess.PIPE
)
process.communicate(user + "\n")
process.communicate(pwd + "\n")
process.communicate(cmd + "\n")

Another option is to use google/python-adb or adb via pip

Sign up to request clarification or add additional context in comments.

7 Comments

Just to clarify, comments in <..> are only for representation of what I do manually. I tried cmd_input = "root test date" and then subprocess.call("adb shell", shell=True, input=cmd_input). But I am getting this error Traceback (most recent call last): File "subprocess_login.py", line 4, in <module> subprocess.call("adb shell", shell=True, input=cmd_input) File "C:\Python27\lib\subprocess.py", line 524, in call return Popen(*popenargs, **kwargs).wait() TypeError: __init__() got an unexpected keyword argument 'input'
@wicked_snail I know, I just copied them over. input is my error, I looked at the Python3 documentation.
I tried user = "root" pwd = "test" cmd = "date" . On executing subprocess.Popen("adb shell", shell=True).communicate("{}\n{}\n{}\n".format(user,pwd,cmd)), I get C:\Project\MMC\python>python subprocess_login.py mdm9607 login: So it is asking to enter the credentials manually. But I want it to login and execute the command from the script.
I tried the second option, but it hangs after execution. After 60 seconds, it exits from execution. Probably because the login timeout is 60 seconds. If I quit in middle , it gives: File "C:\Python27\lib\subprocess.py", line 1033, in _communicate stdout_thread.join() File "C:\Python27\lib\threading.py", line 947, in join self.__block.wait() File "C:\Python27\lib\threading.py", line 339, in wait waiter.acquire() KeyboardInterrupt
I printed the stdout : 'root\r\r\ntest\r\r\ndate\r\r\nmdm9607 login: \r\r\nLogin timed out after 60 seconds.\r\r\n'
|
0

I was getting below error with process.communicate('command_to_send\n')

TypeError: a bytes-like object is required, not 'str'

Using the subprocess communicate command in below way resolved the TypeError:

process.communicate(b'input keyevent KEYCODE_CALL\n')

1 Comment

Please keep in mind that this does only work for Python3, Python2 handles everything as a bytestring (instead of a unicode one) by default.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.