-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsadb.py
352 lines (272 loc) · 10.8 KB
/
sadb.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
"""Module for controlling multiple android devices at once with adb"""
#!/usr/bin/python3
#pylint: disable=subprocess-run-check, unspecified-encoding
# Created by: Seamus Sloan
# Last Edited: July 10, 2023
import argparse
import sys
import os
import subprocess
import time
def split_get_devices(result):
"""Split [adb devices] to gather each device's serial number"""
lines = result.strip().split("\n")[1:]
devices = [line.split()[0] for line in lines]
return devices
def get_devices():
"""Run [adb devices] to get all connected devices"""
result = subprocess.run(["adb", "devices"], capture_output=True, text=True, check=False)
return split_get_devices(result.stdout)
def select_device(devices, allow_all=False):
"""Allow the user to select a connected device or use the only device connected"""
# If there are no devices found...
if len(devices) == 0:
print("No devices found")
return None
# If there is only one device found...
if len(devices) == 1:
return devices[0]
# If there are multiple devices found...
print("Select a device:")
for i, device in enumerate(devices):
print(f"{i + 1}. {device}")
if allow_all:
print(f"{len(devices) + 1}. ALL")
while True:
try:
choice = input("Enter the number of the device: ")
index = int(choice) - 1
if 0 <= index < len(devices):
return devices[index]
elif allow_all and index == len(devices):
return devices
else:
print("Invalid choice")
except ValueError:
print("Invalid choice")
def call_function_on_devices(selected_devices, func, *args):
"""Run the command on the selected device(s)"""
if isinstance(selected_devices, list):
for device in selected_devices:
func(device, *args)
else:
func(selected_devices, *args)
def stop(device, package_name):
"""Run [adb shell am force-stop com.package.name] to stop a process"""
cmd = ["adb", "-s", device, "shell", "am", "force-stop", package_name]
subprocess.run(cmd)
def start(device, package_name):
"""Start a package using adb shell monkey and the intent launcher"""
cmd = ["adb", "-s", device, "shell", "monkey", "-p",
package_name, "-c", "android.intent.category.LAUNCHER", "1"]
with open("/dev/null", "w") as devnull:
subprocess.run(cmd, stdout=devnull, stderr=devnull)
def clear(device, package_name):
"""Run [adb shell pm clear com.package.name] to clear storage"""
cmd = ["adb", "-s", device, "shell", "pm", "clear", package_name]
subprocess.run(cmd)
def install(device, apk):
"""Run [adb install your.apk] to install an APK"""
cmd = ["adb", "-s", device, "install", apk]
subprocess.run(cmd)
def uninstall(device, package_name):
"""Run [adb uninstall your.package.name] to uninstall a package"""
cmd = ["adb", "-s", device, "uninstall", package_name]
subprocess.run(cmd)
def scrcpy(device):
"""Run [scrcpy] to start screen copy"""
cmd = ["scrcpy", "-s", device]
subprocess.run(cmd)
def get_ip(device):
"""Run [adb shell ip addr show wlan0] to get the device's IP"""
cmd = ["adb", "-s", device, "shell", "ip", "addr", "show", "wlan0"]
result = subprocess.run(cmd, capture_output=True, text=True)
lines = result.stdout.strip().split("\n")
for line in lines:
if "inet" in line and not "inet6" in line:
ip_address = line.strip().split()[1].split("/")[0]
print(f"{device}'s IP address is:\t {ip_address}")
return ip_address
def screenshot(device, filename):
"""Run [adb exec-out screencap -p] to capture a screenshot"""
if not filename:
filename = "screenshot.png"
cmd = ["adb", "-s", device, "exec-out", "screencap", "-p"]
with open(filename, "wb") as f:
result = subprocess.run(cmd, stdout=f)
if result.returncode == 0:
print(f"Screenshot saved to {filename}")
def record(device, filename):
"""Run [adb shell screenrecord video.mp4] to perform a screen record"""
if not filename:
filename = "video.mp4"
remote_path = f"/data/local/tmp/{filename}"
cmd = ["adb", "-s", device, "shell", f"screenrecord {remote_path}"]
proc = subprocess.Popen(cmd)
print("Recording... Press CTRL-C to stop.")
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
proc.terminate()
print("\nWaiting for recording to save to device...\n")
time.sleep(5)
cmd = ["adb", "-s", device, "pull", remote_path, filename]
result = subprocess.run(cmd)
if result.returncode == 0:
print(f"Success! Screen recording saved to {os.getcwd()}/{filename}")
delete = input("\nDelete video from device? (Y/n): ")
if delete.lower() == 'y':
cmd = ["adb", "-s", device, "shell", f"rm {remote_path}"]
subprocess.run(cmd)
def wifi(device):
"""Start adb server in tcpip and connect to it via IP address"""
ip_address = get_ip(device)
if not ip_address:
print("Could not get IP address of device")
return
cmd = ["adb", "-s", device, "tcpip", "5555"]
subprocess.run(cmd)
cmd = ["adb", "connect", f"{ip_address}:5555"]
result = subprocess.run(cmd)
if result.returncode == 0:
print(f"Connected to {device} via WiFi")
def search(device, search_term):
"""Search all packages for entered word"""
cmd = ["adb", "-s", device, "shell", "pm",
"list", "packages", "|", "grep", search_term]
subprocess.run(cmd)
def parse_args():
"""Parse all arguments with argparse"""
parser = argparse.ArgumentParser(
description="A wrapper for adb on multiple devices")
subparsers = parser.add_subparsers(dest="command")
# Stop
stop_parser = subparsers.add_parser("stop", help="Stop a package")
stop_parser.add_argument(
"package_name", help="The name of the package to stop")
# Start
start_parser = subparsers.add_parser("start", help="Start a package")
start_parser.add_argument(
"package_name", help="The name of the package to start")
# Clear
clear_parser = subparsers.add_parser(
"clear", help="Clear storage for a package")
clear_parser.add_argument(
"package_name", help="The name of the package to clear")
# Install
install_parser = subparsers.add_parser("install", help="Install an APK")
install_parser.add_argument(
"apk", help="The path to the APK to install")
# Uninstall
uninstall_parser = subparsers.add_parser(
"uninstall", help="Uninstall a package")
uninstall_parser.add_argument(
"package_name", help="The name of the package to uninstall")
# Screen Copy
subparsers.add_parser("scrcpy", help="Start scrcpy on a device")
# IP Address
subparsers.add_parser("ip", help="Get the selected device's IP address")
# Screenshot
screenshot_parser = subparsers.add_parser(
"screenshot", help="Take a screenshot of a device")
screenshot_parser.add_argument(
"-f", "--filename",
help="The name of the file to save the screenshot as (default: screenshot.png)")
# Record
record_parser = subparsers.add_parser(
"record", help="Record the screen of a device (Press CTRL-C to stop recording)")
record_parser.add_argument(
"-f", "--filename",
help="The name of the file to save the screen recording as (default: video.mp4)")
# WiFi
subparsers.add_parser("wifi", help="Connect to a device via WiFi")
# Search
search_parser = subparsers.add_parser(
"search", help="Search for an installed package")
search_parser.add_argument(
"search_term", help="The name of the package to search for")
# R (Raw)
r_parser = subparsers.add_parser("r", help="Run an adb command")
r_parser.add_argument(
"args", help="Any argument you would normally pass through adb", nargs=argparse.REMAINDER)
args = parser.parse_args()
if args.command is None:
parser.print_help()
sys.exit(0)
return args
def main():
"""Main function"""
try:
args = parse_args()
devices = get_devices()
if args.command == "stop":
selected_devices = select_device(devices, allow_all=True)
if selected_devices is None:
return
call_function_on_devices(
selected_devices, stop, args.package_name)
elif args.command == "start":
selected_devices = select_device(devices, allow_all=True)
if selected_devices is None:
return
call_function_on_devices(
selected_devices, start, args.package_name)
elif args.command == "clear":
selected_devices = select_device(devices, allow_all=True)
if selected_devices is None:
return
call_function_on_devices(
selected_devices, clear, args.package_name)
elif args.command == "install":
selected_devices = select_device(devices, allow_all=True)
if selected_devices is None:
return
call_function_on_devices(
selected_devices, install, args.apk)
elif args.command == "uninstall":
selected_devices = select_device(devices, allow_all=True)
if selected_devices is None:
return
call_function_on_devices(
selected_devices, uninstall, args.package_name)
elif args.command == "scrcpy":
device = select_device(devices)
if device is None:
return
scrcpy(device)
elif args.command == "ip":
device = select_device(devices)
if device is None:
return
get_ip(device)
elif args.command == "screenshot":
device = select_device(devices)
if device is None:
return
screenshot(device, args.filename)
elif args.command == "record":
device = select_device(devices)
if device is None:
return
record(device, args.filename)
elif args.command == "wifi":
device = select_device(devices)
if device is None:
return
wifi(device)
elif args.command == "search":
device = select_device(devices)
if device is None:
return
search(device, args.search_term)
elif args.command == "r":
device = select_device(devices)
if device is None:
return
cmd = ["adb", "-s", device] + args.args
subprocess.run(cmd)
except KeyboardInterrupt:
print("\nExiting...")
if __name__ == "__main__":
main()