execute adb commands in cmd prompt from python script
up vote
0
down vote
favorite
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:Projectpython>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
python-2.7 adb
add a comment |
up vote
0
down vote
favorite
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:Projectpython>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
python-2.7 adb
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
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:Projectpython>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
python-2.7 adb
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:Projectpython>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
python-2.7 adb
python-2.7 adb
asked Feb 9 '17 at 7:35
wicked_snail
412
412
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
1
down vote
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
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:Python27libsubprocess.py", line 524, in call return Popen(*popenargs, **kwargs).wait() TypeError: __init__() got an unexpected keyword argument 'input'
– wicked_snail
Feb 9 '17 at 8:09
@wicked_snail I know, I just copied them over.input
is my error, I looked at the Python3 documentation.
– CodenameLambda
Feb 9 '17 at 8:43
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:ProjectMMCpython>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.
– wicked_snail
Feb 9 '17 at 9:17
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:Python27libsubprocess.py", line 1033, in _communicate stdout_thread.join() File "C:Python27libthreading.py", line 947, in join self.__block.wait() File "C:Python27libthreading.py", line 339, in wait waiter.acquire() KeyboardInterrupt
– wicked_snail
Feb 9 '17 at 9:43
I printed the stdout : 'rootrrntestrrndaterrnmdm9607 login: rrnLogin timed out after 60 seconds.rrn'
– wicked_snail
Feb 9 '17 at 10:56
|
show 4 more comments
up vote
0
down vote
I was getting below error with process.communicate('command_to_sendn')
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_CALLn')
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
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
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:Python27libsubprocess.py", line 524, in call return Popen(*popenargs, **kwargs).wait() TypeError: __init__() got an unexpected keyword argument 'input'
– wicked_snail
Feb 9 '17 at 8:09
@wicked_snail I know, I just copied them over.input
is my error, I looked at the Python3 documentation.
– CodenameLambda
Feb 9 '17 at 8:43
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:ProjectMMCpython>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.
– wicked_snail
Feb 9 '17 at 9:17
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:Python27libsubprocess.py", line 1033, in _communicate stdout_thread.join() File "C:Python27libthreading.py", line 947, in join self.__block.wait() File "C:Python27libthreading.py", line 339, in wait waiter.acquire() KeyboardInterrupt
– wicked_snail
Feb 9 '17 at 9:43
I printed the stdout : 'rootrrntestrrndaterrnmdm9607 login: rrnLogin timed out after 60 seconds.rrn'
– wicked_snail
Feb 9 '17 at 10:56
|
show 4 more comments
up vote
1
down vote
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
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:Python27libsubprocess.py", line 524, in call return Popen(*popenargs, **kwargs).wait() TypeError: __init__() got an unexpected keyword argument 'input'
– wicked_snail
Feb 9 '17 at 8:09
@wicked_snail I know, I just copied them over.input
is my error, I looked at the Python3 documentation.
– CodenameLambda
Feb 9 '17 at 8:43
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:ProjectMMCpython>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.
– wicked_snail
Feb 9 '17 at 9:17
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:Python27libsubprocess.py", line 1033, in _communicate stdout_thread.join() File "C:Python27libthreading.py", line 947, in join self.__block.wait() File "C:Python27libthreading.py", line 339, in wait waiter.acquire() KeyboardInterrupt
– wicked_snail
Feb 9 '17 at 9:43
I printed the stdout : 'rootrrntestrrndaterrnmdm9607 login: rrnLogin timed out after 60 seconds.rrn'
– wicked_snail
Feb 9 '17 at 10:56
|
show 4 more comments
up vote
1
down vote
up vote
1
down vote
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
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
edited Feb 9 '17 at 11:23
answered Feb 9 '17 at 7:55
CodenameLambda
1,116520
1,116520
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:Python27libsubprocess.py", line 524, in call return Popen(*popenargs, **kwargs).wait() TypeError: __init__() got an unexpected keyword argument 'input'
– wicked_snail
Feb 9 '17 at 8:09
@wicked_snail I know, I just copied them over.input
is my error, I looked at the Python3 documentation.
– CodenameLambda
Feb 9 '17 at 8:43
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:ProjectMMCpython>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.
– wicked_snail
Feb 9 '17 at 9:17
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:Python27libsubprocess.py", line 1033, in _communicate stdout_thread.join() File "C:Python27libthreading.py", line 947, in join self.__block.wait() File "C:Python27libthreading.py", line 339, in wait waiter.acquire() KeyboardInterrupt
– wicked_snail
Feb 9 '17 at 9:43
I printed the stdout : 'rootrrntestrrndaterrnmdm9607 login: rrnLogin timed out after 60 seconds.rrn'
– wicked_snail
Feb 9 '17 at 10:56
|
show 4 more 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:Python27libsubprocess.py", line 524, in call return Popen(*popenargs, **kwargs).wait() TypeError: __init__() got an unexpected keyword argument 'input'
– wicked_snail
Feb 9 '17 at 8:09
@wicked_snail I know, I just copied them over.input
is my error, I looked at the Python3 documentation.
– CodenameLambda
Feb 9 '17 at 8:43
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:ProjectMMCpython>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.
– wicked_snail
Feb 9 '17 at 9:17
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:Python27libsubprocess.py", line 1033, in _communicate stdout_thread.join() File "C:Python27libthreading.py", line 947, in join self.__block.wait() File "C:Python27libthreading.py", line 339, in wait waiter.acquire() KeyboardInterrupt
– wicked_snail
Feb 9 '17 at 9:43
I printed the stdout : 'rootrrntestrrndaterrnmdm9607 login: rrnLogin timed out after 60 seconds.rrn'
– wicked_snail
Feb 9 '17 at 10:56
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:Python27libsubprocess.py", line 524, in call return Popen(*popenargs, **kwargs).wait() TypeError: __init__() got an unexpected keyword argument 'input'
– wicked_snail
Feb 9 '17 at 8:09
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:Python27libsubprocess.py", line 524, in call return Popen(*popenargs, **kwargs).wait() TypeError: __init__() got an unexpected keyword argument 'input'
– wicked_snail
Feb 9 '17 at 8:09
@wicked_snail I know, I just copied them over.
input
is my error, I looked at the Python3 documentation.– CodenameLambda
Feb 9 '17 at 8:43
@wicked_snail I know, I just copied them over.
input
is my error, I looked at the Python3 documentation.– CodenameLambda
Feb 9 '17 at 8:43
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:ProjectMMCpython>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.
– wicked_snail
Feb 9 '17 at 9:17
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:ProjectMMCpython>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.
– wicked_snail
Feb 9 '17 at 9:17
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:Python27libsubprocess.py", line 1033, in _communicate stdout_thread.join() File "C:Python27libthreading.py", line 947, in join self.__block.wait() File "C:Python27libthreading.py", line 339, in wait waiter.acquire() KeyboardInterrupt
– wicked_snail
Feb 9 '17 at 9:43
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:Python27libsubprocess.py", line 1033, in _communicate stdout_thread.join() File "C:Python27libthreading.py", line 947, in join self.__block.wait() File "C:Python27libthreading.py", line 339, in wait waiter.acquire() KeyboardInterrupt
– wicked_snail
Feb 9 '17 at 9:43
I printed the stdout : 'rootrrntestrrndaterrnmdm9607 login: rrnLogin timed out after 60 seconds.rrn'
– wicked_snail
Feb 9 '17 at 10:56
I printed the stdout : 'rootrrntestrrndaterrnmdm9607 login: rrnLogin timed out after 60 seconds.rrn'
– wicked_snail
Feb 9 '17 at 10:56
|
show 4 more comments
up vote
0
down vote
I was getting below error with process.communicate('command_to_sendn')
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_CALLn')
add a comment |
up vote
0
down vote
I was getting below error with process.communicate('command_to_sendn')
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_CALLn')
add a comment |
up vote
0
down vote
up vote
0
down vote
I was getting below error with process.communicate('command_to_sendn')
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_CALLn')
I was getting below error with process.communicate('command_to_sendn')
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_CALLn')
answered Nov 7 at 11:13
nAQ
36549
36549
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f42130761%2fexecute-adb-commands-in-cmd-prompt-from-python-script%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown