-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathadb-auto.py
executable file
·160 lines (117 loc) · 4.53 KB
/
adb-auto.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#!/usr/bin/python3
import adb_devices as adb
from adb_devices import print_output, monkeyrun
from argparse import ArgumentParser
import threading
from time import sleep
def display_device(dev_uuid, args):
device = adb.get_dev(dev_uuid)
abi_string = ""
for abi in device.abi_support:
abi_string += abi + ","
if len(abi_string) > 0:
abi_string = abi_string[:abi_string.rfind(",")]
print(
"""uuid=%s;abi=%s;sdk=%s;sdk-rel=%s;board=%s;platform=%s;product=%s;name=%s"""
% (device.dev, abi_string, device.sdk_ver, device.sdk_rel,
device.board, device.platform, device.product_name,
device.name))
def screenshot_device(dev_uuid, args):
target = args.extra_args[0].replace("%DEVICE_UUID%", dev_uuid)
device = adb.get_dev(dev_uuid)
# if device.get_screen_state():
# device.unlock_device()
device.get_screenshot(target)
# if not device.get_screen_state():
# device.lock_device()
def install_device(dev_uuid, args):
apks = args.extra_args
device = adb.get_dev(dev_uuid)
for apk in apks:
device.install_apk(apk)
def uninstall_device(dev_uuid, args):
apps = args.extra_args
device = adb.get_dev(dev_uuid)
for app in apps:
device.uninstall_app(app)
def launch_activity(dev_uuid, args):
activity_name = args.extra_args[0]
device = adb.get_dev(dev_uuid)
device.launch_app(activity_name)
def stop_activity(dev_uuid, args):
activity_name = args.extra_args[0]
device = adb.get_dev(dev_uuid)
device.stop_app(activity_name)
def simulate_input(dev_uuid, args):
device = adb.get_dev(dev_uuid)
print_output(device.execute(["shell", "input"] + args.extra_args))
def debug_app(dev_uuid, args):
device = adb.get_dev(dev_uuid)
pkg_name = args.extra_args[0]
device.stop_app(pkg_name)
# Start app while waiting for debugger
device.execute(["shell",
"am set-debug-app -w %s" % pkg_name])
device.launch_app(pkg_name)
sleep(0.1)
pid = device.pidof_activity(pkg_name)
if pid < 0:
raise RuntimeError("Failed to get PID of application")
device.execute(["forward",
"tcp:5039",
"localfilesystem:/data/data/%s/debug-socket" % pkg_name])
instructSet = device.isaof_activity(pkg_name)
device.execute(["shell", "run-as %s chmod a+x /data/data/%s %s" % (pkg_name, pkg_name, device.nlpof_activity(pkg_name))])
device.exec_shell(["shell",
"run-as %s %s/libgdbserver.so +%s --attach %s"
% (pkg_name, device.nlpof_activity(pkg_name),
'/data/data/%s/debug-socket' % pkg_name, pid)])
if __name__ == "__main__":
args = ArgumentParser("ADB automation",
description="Android automation tool")
args.add_argument("command", choices=["install", "uninstall", "info", "start", "stop",
"lock", "unlock", "simulate",
"list-devices", "screenshot",
"debug"])
args.add_argument("extra_args", nargs="*")
args.add_argument("-s", metavar="device-uuid",
dest="uuid", default=None,
help="device selection")
args = args.parse_args()
device_function = None
if args.command == "list-devices":
for uuid in adb.adb_get_devices():
print(uuid)
exit(0)
elif args.command == "info":
device_function = display_device
elif args.command == "screenshot":
device_function = screenshot_device
elif args.command == "install":
device_function = install_device
elif args.command == "uninstall":
device_function = uninstall_device
elif args.command == "start":
device_function = launch_activity
elif args.command == "stop":
device_function = stop_activity
elif args.command == "simulate":
device_function = simulate_input
elif args.command == "debug":
device_function = debug_app
if device_function is None:
exit(1)
if args.uuid is None:
tasks = []
for uuid in adb.adb_get_devices():
tasks.append(threading.Thread(name=uuid,
target=device_function,
args=(uuid, args)))
#device_function(uuid, args)
for task in tasks:
task.start()
for task in tasks:
task.join()
else:
device_function(args.uuid, args)
exit(0)