2012-07-05 181 views
1

我試圖用Python來控制終端。根據我發現的文檔,我應該可以使用Popen以stdin和stdout輸出運行終端命令。遺憾的是它不工作:無法讀寫adb shell

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) 
+0

嘗試'殼= TRUE',但未必是安全的可能你正在做的「獲得root權限」是錯誤的...... – JBernardo

+0

你說得對,那是個愚蠢的例子。看到更新後的腳本,它至少以shell = True開始,但仍然不起作用。 – NoBugs

+1

什麼不起作用?你有沒有嘗試過使用'.communicate'? – JBernardo

回答

2

好吧。 monkeyrunner可以做你表現爲例:

#! /usr/bin/env monkeyrunner 

from com.android.monkeyrunner import MonkeyRunner 

cmd = 'ls /' 
print cmd+':', MonkeyRunner.waitForConnection().shell(cmd) 
+0

我從哪裏得到這個包?我有Android SDK,但無法導入。 – NoBugs

+0

你應該使用monkeyrunner作爲解釋器 –

+0

很好找。它適用於某些命令,但我不能運行'su',即使超級用戶說它被允許。 – NoBugs

1

嘗試:

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

該命令必須是我相信的列表。確保adb位於python進程的路徑中。

+1

該命令必須是*序列號* – cdarke

+1

但只有在'shell = False'(默認值) 。一個shell需要一個命令行。 – glglgl

0

你可以嘗試一下:

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

嘗試:

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()