【问题标题】:Can't read and write from adb shell无法从 adb shell 读写
【发布时间】:2012-07-05 10:19:32
【问题描述】:

我正在尝试使用 Python 控制终端。根据我找到的文档,我应该能够使用 Popen 运行终端命令,并输出标准输入和标准输出。不幸的是,它不起作用:

import subprocess
o = subprocess.Popen("adb shell",stdin=subprocess.PIPE, stdout=subprocess.PIPE, shell=True)
#for line in o:
#print "I got",line
print o.communicate("ls /")
o.stdin.write("ls /")
o.stdin.flush()
print o.stdout.read()
print o.readline();
print o.communicate()

编辑 - 我尝试了 monkeyrunner,它似乎是为这个任务而制作的,但不幸的是它失败并出现错误:

>>> device.shell('su')
120706 08:29:28.433:S [main] [com.android.chimpchat.adb.AdbChimpDevice] Error executing command: su
120706 08:29:28.433:S [main] [com.android.chimpchat.adb.AdbChimpDevice]com.android.ddmlib.ShellCommandUnresponsiveException
120706 08:29:28.433:S [main] [com.android.chimpchat.adb.AdbChimpDevice] at com.android.ddmlib.AdbHelper.executeRemoteCommand(AdbHelper.java:408)
120706 08:29:28.433:S [main] [com.android.chimpchat.adb.AdbChimpDevice] at com.android.ddmlib.Device.executeShellCommand(Device.java:388)
120706 08:29:28.433:S [main] [com.android.chimpchat.adb.AdbChimpDevice] at com.android.chimpchat.adb.AdbChimpDevice.shell(AdbChimpDevice.java:269)
120706 08:29:28.433:S [main] [com.android.chimpchat.adb.AdbChimpDevice] at com.android.monkeyrunner.MonkeyDevice.shell(MonkeyDevice.java:217)
120706 08:29:28.433:S [main] [com.android.chimpchat.adb.AdbChimpDevice] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
120706 08:29:28.433:S [main] [com.android.chimpchat.adb.AdbChimpDevice] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
120706 08:29:28.433:S [main] [com.android.chimpchat.adb.AdbChimpDevice] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
120706 08:29:28.433:S [main] [com.android.chimpchat.adb.AdbChimpDevice] at java.lang.reflect.Method.invoke(Method.java:616)
120706 08:29:28.433:S [main] [com.android.chimpchat.adb.AdbChimpDevice] at org.python.core.PyReflectedFunction.__call__(PyReflectedFunction.java:175)
120706 08:29:28.433:S [main] [com.android.chimpchat.adb.AdbChimpDevice] at org.python.core.PyObject.__call__(PyObject.java:355)
120706 08:29:28.433:S [main] [com.android.chimpchat.adb.AdbChimpDevice] at org.python.core.PyMethod.__call__(PyMethod.java:215)
120706 08:29:28.433:S [main] [com.android.chimpchat.adb.AdbChimpDevice] at org.python.core.PyMethod.instancemethod___call__(PyMethod.java:221)
120706 08:29:28.433:S [main] [com.android.chimpchat.adb.AdbChimpDevice] at org.python.core.PyMethod.__call__(PyMethod.java:206)
120706 08:29:28.433:S [main] [com.android.chimpchat.adb.AdbChimpDevice] at org.python.core.PyObject.__call__(PyObject.java:397)
120706 08:29:28.433:S [main] [com.android.chimpchat.adb.AdbChimpDevice] at org.python.core.PyObject.__call__(PyObject.java:401)
120706 08:29:28.433:S [main] [com.android.chimpchat.adb.AdbChimpDevice] at org.python.pycode._pyx19.f$0(<stdin>:1)
120706 08:29:28.433:S [main] [com.android.chimpchat.adb.AdbChimpDevice] at org.python.pycode._pyx19.call_function(<stdin>)
120706 08:29:28.433:S [main] [com.android.chimpchat.adb.AdbChimpDevice] at org.python.core.PyTableCode.call(PyTableCode.java:165)
120706 08:29:28.433:S [main] [com.android.chimpchat.adb.AdbChimpDevice] at org.python.core.PyCode.call(PyCode.java:18)
120706 08:29:28.433:S [main] [com.android.chimpchat.adb.AdbChimpDevice] at org.python.core.Py.runCode(Py.java:1197)
120706 08:29:28.433:S [main] [com.android.chimpchat.adb.AdbChimpDevice] at org.python.core.Py.exec(Py.java:1241)
120706 08:29:28.433:S [main] [com.android.chimpchat.adb.AdbChimpDevice] at org.python.util.PythonInterpreter.exec(PythonInterpreter.java:147)
120706 08:29:28.433:S [main] [com.android.chimpchat.adb.AdbChimpDevice] at org.python.util.InteractiveInterpreter.runcode(InteractiveInterpreter.java:89)
120706 08:29:28.433:S [main] [com.android.chimpchat.adb.AdbChimpDevice] at org.python.util.InteractiveInterpreter.runsource(InteractiveInterpreter.java:70)
120706 08:29:28.433:S [main] [com.android.chimpchat.adb.AdbChimpDevice] at org.python.util.InteractiveInterpreter.runsource(InteractiveInterpreter.java:46)
120706 08:29:28.433:S [main] [com.android.chimpchat.adb.AdbChimpDevice] at org.python.util.InteractiveConsole.push(InteractiveConsole.java:110)
120706 08:29:28.433:S [main] [com.android.chimpchat.adb.AdbChimpDevice] at org.python.util.InteractiveConsole.interact(InteractiveConsole.java:90)
120706 08:29:28.433:S [main] [com.android.chimpchat.adb.AdbChimpDevice] at org.python.util.InteractiveConsole.interact(InteractiveConsole.java:60)
120706 08:29:28.433:S [main] [com.android.chimpchat.adb.AdbChimpDevice] at com.android.monkeyrunner.ScriptRunner.console(ScriptRunner.java:193)
120706 08:29:28.433:S [main] [com.android.chimpchat.adb.AdbChimpDevice] at com.android.monkeyrunner.MonkeyRunnerStarter.run(MonkeyRunnerStarter.java:73)
120706 08:29:28.433:S [main] [com.android.chimpchat.adb.AdbChimpDevice] at com.android.monkeyrunner.MonkeyRunnerStarter.main(MonkeyRunnerStarter.java:189)

【问题讨论】:

  • 试试shell=True,但这可能不安全,而且您所做的“获取root权限”可能是错误的......
  • 你是对的,那是愚蠢的例子。查看更新的脚本,它至少以 shell=True 开头,但仍然不起作用。
  • 什么不起作用?你试过使用.communicate吗?
  • 它只显示一个空屏幕。是的,我尝试过交流。
  • 你可能想要pexpect

标签: android python shell unix subprocess


【解决方案1】:

试试:

import subprocess
o = subprocess.Popen(["adb", "shell"],stdin=subprocess.PIPE, stdout=subprocess.PIPE)
o.stdin.write("ls /")
o.stdin.write("\n")
o.stdin.flush()
o.stdout.readline()

【讨论】:

    【解决方案2】:

    你可以试试:

    import os
    ls=os.popen('ls').read()
    

    【讨论】:

      【解决方案3】:

      嗯。 monkeyrunner 可以做你展示的例子:

      #! /usr/bin/env monkeyrunner
      
      from com.android.monkeyrunner import MonkeyRunner
      
      cmd = 'ls /'
      print cmd+':', MonkeyRunner.waitForConnection().shell(cmd)
      

      【讨论】:

      • 我在哪里可以得到这个包?我有 Android SDK,但无法导入。
      • 你应该使用 monkeyrunner 作为解释器
      • 不错的发现。它适用于某些命令,但我无法运行“su”,即使超级用户说它是允许的。
      • 虽然 su 不起作用,但 shell("su -c 'ls /data/'") 可以。我猜 MonkeyRunner 在等待输入时会失败?
      【解决方案4】:

      尝试:

      o = subprocess.Popen(["adb", "shell"],stdin=subprocess.PIPE, stdout=subprocess.PIPE, shell=True)
      

      我相信该命令必须是一个列表。确保 adb 在 python 进程的路径中。

      【讨论】:

      • 但前提是shell=False(默认)。 shell 需要一个命令行。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-09-25
      • 1970-01-01
      • 2019-03-01
      • 1970-01-01
      • 2014-02-21
      • 2014-10-18
      • 1970-01-01
      相关资源
      最近更新 更多