-
Notifications
You must be signed in to change notification settings - Fork 13
/
adb.py
480 lines (403 loc) · 12.3 KB
/
adb.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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
# Author: Chema Garcia (aka sch3m4)
# Contact: chema@safetybits.net | http://safetybits.net/contact
# Homepage: http://safetybits.net
# Project Site: http://github.com/sch3m4/pyadb
try:
import sys
import subprocess
except ImportError,e:
# should never be reached
print "[f] Required module missing. %s" % e.args[0]
sys.exit(-1)
class ADB():
PYADB_VERSION = "0.1.2"
__adb_path = None
__output = None
__error = None
__devices = None
__target = None
# reboot modes
REBOOT_RECOVERY = 1
REBOOT_BOOTLOADER = 2
# default TCP/IP port
DEFAULT_TCP_PORT = 5555
# default TCP/IP host
DEFAULT_TCP_HOST = "localhost"
def pyadb_version(self):
return self.PYADB_VERSION
def __init__(self,adb_path=None):
self.__adb_path = adb_path
def __clean__(self):
self.__output = None
self.__error = None
def __parse_output__(self,outstr):
ret = None
if(len(outstr) > 0):
ret = outstr.splitlines()
return ret
def __build_command__(self,cmd):
ret = None
if self.__devices is not None and len(self.__devices) > 1 and self.__target is None:
self.__error = "Must set target device first"
return ret
# Modified function to directly return command set for Popen
#
# Unfortunately, there is something odd going on and the argument list is not being properly
# converted to a string on the windows 7 test systems. To accomodate, this block explitely
# detects windows vs. non-windows and builds the OS dependent command output
if self.__target is None:
ret = self.__adb_path + " " + cmd
else:
ret = self.__adb_path + " -s " + self.__target + " " + cmd
if sys.platform.startswith('win'):
return ret
else:
ret = ret.split()
return ret
def get_output(self):
return self.__output
def get_error(self):
return self.__error
def lastFailed(self):
"""
Was failed the last command?
"""
if self.__output is None and self.__error is not None:
return True
return False
def run_cmd(self,cmd):
"""
Run a command against adb tool ($ adb <cmd>)
"""
self.__clean__()
if self.__adb_path is None:
self.__error = "ADB path not set"
return
# For compat of windows
cmd_list = self.__build_command__(cmd)
try:
(self.__output, self.__error) = subprocess.Popen(cmd_list, stdin = subprocess.PIPE, \
stdout = subprocess.PIPE, \
stderr = subprocess.PIPE, shell = False).communicate()
if( len(self.__output) == 0 ):
self.__output = None
if( len(self.__error) == 0 ):
self.__error = None
except:
pass
return
def get_version(self):
"""
Returns ADB tool version
adb version
"""
self.run_cmd("version")
try:
ret = self.__output.split()[-1:][0]
except:
ret = None
return ret
def check_path(self):
"""
Intuitive way to verify the ADB path
"""
if self.get_version() is None:
return False
return True
def set_adb_path(self,adb_path):
"""
Set ADB tool path
"""
self.__adb_path = adb_path
def get_adb_path(self):
"""
Returns ADB tool path
"""
return self.__adb_path
def start_server(self):
"""
Starts ADB server
adb start-server
"""
self.__clean__()
self.run_cmd('start-server')
return self.__output
def kill_server(self):
"""
Kills ADB server
adb kill-server
"""
self.__clean__()
self.run_cmd('kill-server')
def restart_server(self):
"""
Restarts ADB server
"""
self.kill_server()
return self.start_server()
def restore_file(self,file_name):
"""
Restore device contents from the <file> backup archive
adb restore <file>
"""
self.__clean__()
self.run_cmd('restore %s' % file_name)
return self.__output
def wait_for_device(self):
"""
Block until device is online
adb wait-for-device
"""
self.__clean__()
self.run_cmd('wait-for-device')
return self.__output
def get_help(self):
"""
Returns ADB help
adb help
"""
self.__clean__()
self.run_cmd('help')
return self.__output
def get_devices(self):
"""
Return a list of connected devices
adb devices
"""
error = 0
self.run_cmd("devices")
if self.__error is not None:
return ''
try:
print self.__output.partition('\n')
self.__devices = self.__output.partition('\n')[2].replace('device','').split()
if self.__devices[1:] == ['no','permissions']:
error = 2
self.__devices = None
except:
self.__devices = None
error = 1
return (error,self.__devices)
def set_target_device(self,device):
"""
Select the device to work with
"""
self.__clean__()
if device is None or not device in self.__devices:
self.__error = 'Must get device list first'
return False
self.__target = device
return True
def get_target_device(self):
"""
Returns the selected device to work with
"""
return self.__target
def get_state(self):
"""
Get ADB state
adb get-state
"""
self.__clean__()
self.run_cmd('get-state')
return self.__output
def get_serialno(self):
"""
Get serialno from target device
adb get-serialno
"""
self.__clean__()
self.run_cmd('get-serialno')
return self.__output
def reboot_device(self,mode):
"""
Reboot the target device
adb reboot recovery/bootloader
"""
self.__clean__()
if not mode in (self.REBOOT_RECOVERY,self.REBOOT_BOOTLOADER):
self.__error = "mode must be REBOOT_RECOVERY/REBOOT_BOOTLOADER"
return self.__output
self.run_cmd("reboot %s" % "recovery" if mode == self.REBOOT_RECOVERY else "bootloader")
return self.__output
def set_adb_root(self,mode):
"""
restarts the adbd daemon with root permissions
adb root
"""
self.__clean__()
self.run_cmd('root')
return self.__output
def set_system_rw(self):
"""
Mounts /system as rw
adb remount
"""
self.__clean__()
self.run_cmd("remount")
return self.__output
def get_remote_file(self,remote,local):
"""
Pulls a remote file
adb pull remote local
"""
self.__clean__()
self.run_cmd('pull \"%s\" \"%s\"' % (remote,local) )
if self.__error is not None and "bytes in" in self.__error:
self.__output = self.__error
self.__error = None
return self.__output
def push_local_file(self,local,remote):
"""
Push a local file
adb push local remote
"""
self.__clean__()
self.run_cmd('push \"%s\" \"%s\"' % (local,remote) )
return self.__output
def shell_command(self,cmd):
"""
Executes a shell command
adb shell <cmd>
"""
self.__clean__()
self.run_cmd('shell %s' % cmd)
return self.__output
def listen_usb(self):
"""
Restarts the adbd daemon listening on USB
adb usb
"""
self.__clean__()
self.run_cmd("usb")
return self.__output
def listen_tcp(self,port=DEFAULT_TCP_PORT):
"""
Restarts the adbd daemon listening on the specified port
adb tcpip <port>
"""
self.__clean__()
self.run_cmd("tcpip %s" % port)
return self.__output
def get_bugreport(self):
"""
Return all information from the device that should be included in a bug report
adb bugreport
"""
self.__clean__()
self.run_cmd("bugreport")
return self.__output
def get_jdwp(self):
"""
List PIDs of processes hosting a JDWP transport
adb jdwp
"""
self.__clean__()
self.run_cmd("jdwp")
return self.__output
def get_logcat(self,lcfilter=""):
"""
View device log
adb logcat <filter>
"""
self.__clean__()
self.run_cmd("logcat %s" % lcfilter)
return self.__output
def run_emulator(self,cmd=""):
"""
Run emulator console command
"""
self.__clean__()
self.run_cmd("emu %s" % cmd)
return self.__output
def connect_remote (self,host=DEFAULT_TCP_HOST,port=DEFAULT_TCP_PORT):
"""
Connect to a device via TCP/IP
adb connect host:port
"""
self.__clean__()
self.run_cmd("connect %s:%s" % ( host , port ) )
return self.__output
def disconnect_remote (self , host=DEFAULT_TCP_HOST , port=DEFAULT_TCP_PORT):
"""
Disconnect from a TCP/IP device
adb disconnect host:port
"""
self.__clean__()
self.run_cmd("disconnect %s:%s" % ( host , port ) )
return self.__output
def ppp_over_usb(self,tty=None,params=""):
"""
Run PPP over USB
adb ppp <tty> <params>
"""
self.__clean__()
if tty is None:
return self.__output
cmd = "ppp %s" % tty
if params != "":
cmd += " %s" % params
self.run_cmd(cmd)
return self.__output
def sync_directory(self,directory=""):
"""
Copy host->device only if changed (-l means list but don't copy)
adb sync <dir>
"""
self.__clean__()
self.run_cmd("sync %s" % directory )
return self.__output
def forward_socket(self,local=None,remote=None):
"""
Forward socket connections
adb forward <local> <remote>
"""
self.__clean__()
if local is None or remote is None:
return self.__output
self.run_cmd("forward %s %s" % (local,remote) )
return self.__output
def uninstall(self,package=None,keepdata=False):
"""
Remove this app package from the device
adb uninstall [-k] package
"""
self.__clean__()
if package is None:
return self.__output
cmd = "uninstall %s" % (package if keepdata is True else "-k %s" % package )
self.run_cmd(cmd)
return self.__output
def install(self,fwdlock=False,reinstall=False,sdcard=False,pkgapp=None):
"""
Push this package file to the device and install it
adb install [-l] [-r] [-s] <file>
-l -> forward-lock the app
-r -> reinstall the app, keeping its data
-s -> install on sdcard instead of internal storage
"""
self.__clean__()
if pkgapp is None:
return self.__output
cmd = "install -f "
if fwdlock is True:
cmd += "-l "
if reinstall is True:
cmd += "-r "
if sdcard is True:
cmd += "-s "
self.run_cmd("%s %s" % (cmd , pkgapp) )
return self.__output
def find_binary(self,name=None):
"""
Look for a binary file on the device
"""
self.shell_command("which %s" % name)
if self.__output is None: # not found
self.__error = "'%s' was not found" % name
elif self.__output.strip() == "which: not found": # which binary not available
self.__output = None
self.__error = "which binary not found"
else:
self.__output = self.__output.strip()
return self.__output