Python调用adb命令实现对多台设备同时进行reboot的方法

时间:2018-10-15
今天小编就为大家分享一篇Python调用adb命令实现对多台设备同时进行reboot的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

首先,adb实现对设备的reboot命令是:adb reboot . 但是如果是两台/多台设备的时候,需要声明serial number: adb -s serial_no reboot.

那么,如何用python实现对多台设备进行adb操作呢(reboot)?

这里涉及到 python 下 subprocess model的使用:

import subprocess

adb device 获取所有设备的 serial number:

devices = subprocess.Popen(
 'adb devices'.split(),
 stdout=subprocess.PIPE,
 stderr=subprocess.PIPE
).communicate()[0]

这样adb device命令的返回信息都在devices下,但是我们只需要 serial number的:

serial_nos = []
for item in devices.split():
 filters = ['list', 'of', 'device', 'devices', 'attached']
 if item.lower() not in filters:
  serial_nos.append(item)

这样serial_nos 下保存的就是所有设备的 serial number 了,下面我们只需要依次对其进行adb -s [serial_number] reboot即可:

for serial_no in serial_nos:
 reboot_cmds.append('adb -s %s reboot' % serial_no)
for reboot_cmd in reboot_cmds:
 subprocess.Popen(
  reboot_cmd.split(),
  stdout=subprocess.PIPE,
  stderr=subprocess.PIPE
 ).communicate()[0]

这样,每个设备都进行了reboot的操作了……

以上这篇Python调用adb命令实现对多台设备同时进行reboot的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。