python+uiautomator2实现termux的数据清除与初始化配置(待完善)

python+uiautomator2实现termux的数据清除与初始化配置,待完善

执行方式

1
python cfg.py

安装依赖

1
pip3 install pywinauto

cfg.sh

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
# 打开镜像选择界面
termux-change-repo

pkg update
pkg install -y termux-auth expect android-tools openssh

expect <<'EOF'
spawn passwd
expect "New password:" { send "..Qq1ssh..\r" }
expect "Retype new password:" { send "..Qq1ssh..\r" }
expect eof
EOF

cfg.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
# coding: utf-8
#
import uiautomator2 as u2
import subprocess
import os
import time

# 首先确保手机与电脑通过USB连接或者电脑通过ip连接手机adb,如:adb connect 192.168.3.178:5555
result = subprocess.check_output(['adb', 'devices']).decode('utf-8')
devices = [line.split('\t')[0]
           for line in result.splitlines()
           if '\tdevice' in line]
connect = devices[0]
d = u2.connect(connect)

# 清除app的数据
d.app_clear('com.termux')

# 直接通过包名打开app
d.app_start('com.termux')
time.sleep(1)
d(resourceId="com.termux:id/drawer_layout").click()

# 获取存储访问的权限
os.system(f'adb -s {connect} shell input text "termux-setup-storage"')
os.system(f'adb -s {connect} shell input keyevent 66')  # 发送回车键
# 弹窗请求允许,点击允许
d(resourceId="com.android.permissioncontroller:id/permission_allow_button").click()

# 拷贝脚本
os.system(f'adb -s {connect} push cfg.sh /storage/self/primary/Download/'),
time.sleep(1)
cmd = "cp -a storage\/shared\/Download\/cfg.sh ~/".replace(" ", "%s")
os.system(f'adb -s {connect} shell input text "{cmd}"')
os.system(f'adb -s {connect} shell input keyevent 66')  # 回车

# 运行脚本
cmd = "bash ~/cfg.sh".replace(" ", "%s")
os.system(f'adb -s {connect} shell input text "{cmd}"')
os.system(f'adb -s {connect} shell input keyevent 66')  # 回车

# 换源
# 模拟按键操作(需根据实际UI调整延迟)
# time.sleep(2)
os.system(f'adb -s {connect} shell input keyevent KEYCODE_SPACE')  # 全选
# time.sleep(2)
d.shell('input keyevent KEYCODE_ENTER')  # 确认
# time.sleep(2)
os.system(f'adb -s {connect} shell input keyevent KEYCODE_DPAD_DOWN 2')  # 选择中国镜像(按2次方向键下)
# time.sleep(2)
os.system(f'adb -s {connect} shell input keyevent KEYCODE_ENTER')  # 确认
time.sleep(30)
comments powered by Disqus
使用 Hugo 构建
主题 StackJimmy 设计