6

I am trying to execute adb shell commands in python using subprocess.Popen

Example: Need to execute 'command' in adb shell. While executing manually, I open the command window and execute as below and it works.

>adb shell
#<command>

In Python I am using as below but the process is stuck and doesn't give output

subprocess.Popen('adb shell <command>)

Tried executing manually in command window, same result as python code,stuck and doesn't give output

>adb shell <command>

I am trying to execute a binary file in background(using binary file name followed by &) in the command.

3 Answers 3

6

Found a way to do it using communicate() method in subprocess module

procId = subprocess.Popen('adb shell', stdin = subprocess.PIPE)
procId.communicate('command1\ncommand2\nexit\n')
Sign up to request clarification or add additional context in comments.

1 Comment

I don't know why it worked, but it doesn't work anymore. Now only those commands work. cmd = ['adb', 'shell'] procId = subprocess.Popen(cmd, stdin=subprocess.PIPE) And I have to point out that procId.communicate('command1\ncommand2\nexit\n') doesn't work in Python3, because this exception is thrown: TypeError: a bytes-like object is required, not 'str'. Use this command instead: procId.communicate(b'command1\ncommand2\nexit\n') (with a "b" before the string), because it works both in Python2 and Python3.
1

use pexpect (https://pexpect.readthedocs.io/en/stable/)

adb="/Users/lishaokai/Library/Android/sdk/platform-tools/adb"
import pexpect
import sys, os
child = pexpect.spawn(adb + " shell")
child.logfile_send = sys.stdout

while True:
  index = child.expect(["$","@",pexpect.TIMEOUT])
  print index
  child.sendline("ls /storage/emulated/0/")
  index = child.expect(["huoshan","google",pexpect.TIMEOUT])
  print index, child.before, child.after
  break

4 Comments

got error child = pexpect.spawn(adb + " shell") AttributeError: 'module' object has no attribute 'spawn'
easy_install pexpect to install this package
I am using python2.7. Installed using pip install pexpect. but it seems module spawn is not present.
in command line mode, import pexpect;help(pexpect) to see what pexpect exactly you are installing
0

Ankur Kabra, try the code below:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import subprocess
command = 'adb devices'
p = subprocess.Popen(command, shell=True,
                         stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = p.communicate()
print 'standard output: %s \n error output: %s \n',(stdout,stderr)

and you will see the error output.

Usually it will tell you:

 /bin/sh: adb: command not found

which means, shell can not excute adb command. so, adding adb to your PATH or writing the full path of adb will solve the problem.

May help.

5 Comments

Hi,I tried the above code. I got no error. The output was list of devices. I want to open adb shell using python and then again execute commands in that shell using python. subprocess.Popen('adb shell')->This opens the shell as expected. Next I want to execute statements in this shell. How can I do this?
hi, if you want to open adb shell using python and then again execute commands in that shell using python, use subprocess.Popen('adb shell shell_command'). You do not have to make an long connection, just connect to shell every time you execute a shell command.
yes it generally works but there seems to be some issue with my command. If I give it in a single line then there is no output.
subprocess.Popen('adb shell setenforce 0')---->works subprocess.Popen('adb shell webserver &')----->program stuck at this step. here webserver is abinary file
First, check out if webserver binary file is in your phone storage. Second, try out adb shell, then run your webserver to checkout if you have the execute access right of the file.

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.