Python calls the adb command to implement reboot for multiple devices at the same time

  • 2020-12-19 21:07:08
  • OfStack

First, adb implements the reboot command for the device: adb reboot. However, for two/more devices, you need to declare serial number: adb-ES8en serial_no reboot.

So, how to implement adb operation on multiple devices with python?

Here refers to the use of subprocess model under python:


import subprocess

adb device Obtain serial number for all devices:


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

So the return information of adb device command is under devices, but we only need serial number's:


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

In this way, serial number is saved under serial_nos for all devices, and we only need to carry out ES44en-ES45en [serial_number] reboot in turn:


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]

Thus, each device has performed reboot operation...


Related articles: