def test_get_pid_on_device(self): adb = ADB() adb.enable_debug() adb.set_adb_path() adb.set_target_device() pid = utils.get_pid_on_device(adb, 'com.android.commands.monkey') utils.kill_proc_on_device(adb, pid) print(pid)
def test_install(self): adb = ADB() adb.set_adb_path() _, ds = adb.get_devices() device_id = ds[0] adb.set_target_device(device_id) adb.enable_debug() output = adb.install(reinstall=True, pkgapp='ctssetup.apk') print("output: " + str(output))
class ADB_Wrapper(object): def __init__(self, adb_path): self.adb_path = adb_path self.adb = None self.devices = [] self.adb = ADB(adb_path) if NEED_RESTART_ADB: self.restart_adb() def restart_adb(self): print '[+] Restarting ADB server as ROOT...' self.adb.set_adb_root(1) if self.adb.lastFailed(): print '\t Restart ADB ERROR\n' exit(-3) print 'Restart ADB as ROOT successed!' def get_detected_devices(self): self.devices = None dev = 0 while dev is 0: print '[+] Detecting devices...' error, devices = self.adb.get_devices() if error is 1: print 'No devices connnected' print '[+] Waiting for deices...' self.adb.wait_for_device() continue elif error is 2: print "You haven't enought permissions!" exit(-3) # If devices is ['*', 'deamon', 'start', 'successfully'.....], # means that we should restart adb untill we get valid devices if len(devices ) > 3 and devices[0] == '*' and devices[1] == 'daemon': print '[W] get devices error, we should restart the adb' continue print '[+] Get devices successfully' self.devices = devices dev = 1 def set_target_device(self): devices_count = 0 for dev in self.devices: print '\t%d: %s' % (devices_count, dev) if dev != 'offline': devices_count += 1 # For the cmd 'adb forward ......' can exec successly only when there is one device/emulator, # so if there are more than one devices, we exit. if devices_count > 1: print '[Error] More than one devices/emulators, please shut down others!' exit(-3) # set target device dev_index = 0 try: self.adb.set_target_device(self.devices[dev_index]) except Exception, e: print '[E] Set target devices error, error info:', e print '==== Do not warry, just try again!===' exit(-5) print "\n[+] Using '%s' as target device" % self.devices[dev_index]
def main(): path = r'adb' adb = ADB(path) # set ADB path #adb.set_adb_path('~/android-sdk-linux/platform-tools/adb') print "[+] Using PyADB version %s" % adb.pyadb_version() # verity ADB path print "[+] Verifying ADB path...", if adb.check_path() is False: print "ERROR" exit(-2) print "OK" # print ADB Version print "[+] ADB Version: %s" % adb.get_version() print "" # restart server (may be other instances running) print "[+] Restarting ADB server..." adb.restart_server() if adb.lastFailed(): print "\t- ERROR\n" exit(-3) # get detected devices dev = 0 while dev is 0: print "[+] Detecting devices..." , error,devices = adb.get_devices() if error is 1: # no devices connected print "No devices connected" print "[+] Waiting for devices..." adb.wait_for_device() continue elif error is 2: print "You haven't enought permissions!" exit(-3) print "OK" dev = 1 # this should never be reached if len(devices) == 0: print "[+] No devices detected!" exit(-4) # show detected devices i = 0 for dev in devices: print "\t%d: %s" % (i,dev) i += 1 # if there are more than one devices, ask to the user to choose one of them if i > 1: dev = i + 1 while dev < 0 or dev > int(i - 1): print "\n[+] Select target device [0-%d]: " % int(i - 1) , dev = int(stdin.readline()) else: dev = 0 # set target device try: adb.set_target_device(devices[dev]) except Exception,e: print "\n[!] Error:\t- ADB: %s\t - Python: %s" % (adb.get_error(),e.args) exit(-5)
class ADB_Wrapper(object): def __init__(self, adb_path): self.adb_path = adb_path self.adb = None self.devices = [] self.adb = ADB(adb_path) if NEED_RESTART_ADB: self.restart_adb() def restart_adb(self): print '[+] Restarting ADB server as ROOT...' self.adb.set_adb_root(1) if self.adb.lastFailed(): print '\t Restart ADB ERROR\n' exit(-3) print 'Restart ADB as ROOT successed!' def get_detected_devices(self): self.devices = None dev = 0 while dev is 0: print '[+] Detecting devices...' error, devices = self.adb.get_devices() if error is 1: print 'No devices connnected' print '[+] Waiting for deices...' self.adb.wait_for_device() continue elif error is 2: print "You haven't enought permissions!" exit(-3) # If devices is ['*', 'deamon', 'start', 'successfully'.....], # means that we should restart adb untill we get valid devices if len(devices) > 3 and devices[0] == '*' and devices[1] == 'daemon': print '[W] get devices error, we should restart the adb' continue print '[+] Get devices successfully' self.devices = devices dev = 1 def set_target_device(self): devices_count = 0 for dev in self.devices: print '\t%d: %s' % (devices_count, dev) if dev != 'offline': devices_count += 1 # For the cmd 'adb forward ......' can exec successly only when there is one device/emulator, # so if there are more than one devices, we exit. if devices_count > 1: print '[Error] More than one devices/emulators, please shut down others!' exit(-3) # set target device dev_index = 0 try: self.adb.set_target_device(self.devices[dev_index]) except Exception, e: print '[E] Set target devices error, error info:', e print '==== Do not warry, just try again!===' exit(-5) print "\n[+] Using '%s' as target device" % self.devices[dev_index]