import subprocess, time import sys import re deviceId = "" def ExecuteCmd_callback(cmd, codeType='gbk'): print(cmd) cmdb = str.encode(cmd) p = subprocess.Popen(cmdb.decode(codeType), shell=True,stdout=subprocess.PIPE,stderr=subprocess.STDOUT) result = p.stdout.read().decode(codeType) return result def ExecuteCmd(cmd, codeType='gbk'): """python shell子进程形式运行自动化脚本""" cmdb = str.encode(cmd) p = subprocess.Popen(cmdb.decode(codeType), shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) while p.poll() is None: line = p.stdout.readline() line = line.strip() if line: print('Subprogram output: [{}]'.format(line)) if p.returncode == 0: print('Subprogram success') else: print('Subprogram failed') def openApk(apkname): ExecuteCmd_callback(r"adb -s " + deviceId + " shell monkey -p {} -c android.intent.category.LAUNCHER 1".format(apkname)) def change_ime(): if deviceId is not None: ExecuteCmd_callback("adb -s " + deviceId +" shell ime set com.android.adbkeyboard/.AdbIME") #切换到adbkeyboard time.sleep(2) def input_ime(inputText): if deviceId is not None: ExecuteCmd("adb -s " + deviceId +" shell am broadcast -a ADB_INPUT_TEXT --es msg '{}'".format(inputText), 'UTF-8') #输入 time.sleep(3) def ime_search(): if deviceId is not None: ExecuteCmd("adb -s " + deviceId + " shell am broadcast -a ADB_EDITOR_CODE --ei code 3") # 切换到adbkeyboard time.sleep(2) def get_ip(): device_raw = ExecuteCmd_callback("ifconfig") print('device_str: ', device_raw) a = re.findall(r"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}", device_raw, re.M | re.I) return a def get_device_id(): device_raw = ExecuteCmd_callback("adb devices") device_str = device_raw.split("\n") print('device_str: ',device_str) del device_str[0] for element in device_str: if "device" in element: devece_target = element.split("\t") return devece_target[0] return None def ExecuteCmd_sys(cmd, codeType='gbk'): """python shell子进程形式运行自动化脚本""" print(cmd) cmdb = str.encode(cmd) p = subprocess.Popen(cmdb.decode(codeType), shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) while p.poll() is None: line = p.stdout.readline() line = line.strip() if line: print('Subprogram output: [{}]'.format(line)) if p.returncode == 0: print('Subprogram success') else: print('Subprogram failed') if __name__ == "__main__": time.sleep(10) # iplist = get_ip() # for ip in iplist: # ExecuteCmd_callback("adb connect " + ip, codeType='gbk') # time(2) deviceId = sys.argv[1] openApk("com.termux") time.sleep(2) ExecuteCmd_sys("chmod 777 update.sh") time.sleep(5) change_ime() input_ime("/data/data/com.termux/files/home/pyclient/update.sh") ime_search()