如何在python中,在adb shell环境下执行命令?
import os
import subprocess
cmd = 'adb -s 8BHX1B399 shell "cd /data;ls"' # 将你要执行的子命令用引号写出来,命令间以';'分割
result = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
output, error = result.communicate(timeout=600)
print(output.decode())
另一种:
import subprocess
obj = subprocess.Popen('adb -s 8BHX1B399 shell', stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE,shell=True)
out,err = obj.communicate(input='cd /data/;ls;find . -type d'.encode()) # 通过communicate传递数据,返回(stdout,stderr)
print(out.decode())