• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

python-uiautomator运行shell命令

武飞扬头像
古墙白
帮助2

一、不需要root时

1.os.system()

#ls /mnt/文件,不需要root

d=conn_device()
ex_out = os.popen('adb shell ls -l /mnt/')
out = ex_out.readlines()
print(out)

2. d.shell()

#ls /mnt/文件,不需要root

d=conn_device()
#列表写shell命令
out,exit_code = d.shell(["ls",'-l','/mnt/'],timeout=60)
print(out)
#直接写shell命令
out2,e1 = d.shell('ls -s /mnt/', timeout=60)
print(out2)

二、需要root时

1.os.system()

#ls下/mnt/usb_storage文件,需要root,用adb root 或 adb shell su

os.system('adb root')
ex_out = os.popen('adb shell ls -s /mnt/usb_storage')
out = size.readlines()
print(out)

2. d.shell()

#用d.shell执行命令,需要root时,root、su分次执行不可以

os.system('adb root')
d.shell('su')
output,exit1 = d.shell(' ls -s /mnt/usb_storage',timeout=60)
print(output) #du: No /mnt/usb_storage: Permission denied

#需要一次性切换root并执行命令 可用su -c 切换为root用户 并接后续指令

su -c CMD
output,e3 = d.shell('su -c ls -s /mnt/usb_storage',timeout=60)
print(output) #0	/mnt/usb_storage1

三、实战

判断以USB开头的文件大小,进而判断U盘是否挂载

os.system('adb root')
size = os.popen('adb shell du -h /mnt/usb_storage*')
size_n = size.readlines()
for i in size_n:
    if int(i[0]) > 0:
        print('U盘文件已挂载!')
        #return 1
    else:
        print('U盘文件未挂载!')
        #return 0
output,e5 = d.shell('su -c du -h /mnt/usb_storage*',timeout=60)
a = output.strip().split('\n') #去除字符串前后的空格str.strip()
for i in a:
    if int(i[0]) > 0:
        print('U盘文件已挂载!')
        #return 1
    else:
        print('U盘文件未挂载!')
        #return 0

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgbjfge
系列文章
更多 icon
同类精品
更多 icon
继续加载