Skip to content

Instantly share code, notes, and snippets.

@alexfu
Created January 4, 2019 18:09
Show Gist options
  • Save alexfu/124dc0ca022b5e37f6b1acaf5db88001 to your computer and use it in GitHub Desktop.
Save alexfu/124dc0ca022b5e37f6b1acaf5db88001 to your computer and use it in GitHub Desktop.
ADB that prompts for which device when multiple devices are available
#!/usr/bin/env python
import sys
import subprocess
def get_devices():
pipe = subprocess.PIPE
process = subprocess.Popen(["adb", "devices"], stdin=pipe, stdout=pipe, stderr=pipe)
output, error = process.communicate()
output_lines = output.strip().split("\n")
list_of_devices = []
for line in output_lines[1:]:
list_of_devices.append(line.split("\t")[0])
return list_of_devices
def prompt_for_device(devices):
for index,device in enumerate(devices):
print("[{0}]: {1}".format(index, device))
result = raw_input("Which device? [0]: ") or "0"
return int(result)
def adb(args, device = None):
if device:
subprocess.call(["adb", "-s", device] + args)
else:
subprocess.call(["adb"] + args)
adb_args = sys.argv[1:]
devices = get_devices()
if len(devices) > 1:
device_index = prompt_for_device(devices)
device = devices[device_index]
adb(adb_args, device)
else:
adb(adb_args)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment