from androguard.core.bytecodes.apk import APK
import os
class ADB:
def __init__(self, log):
self.log = log
def get_devices(self) -> list:
"""获取链接的设备"""
all_devices = []
cmd = "adb devices"
reslut = os.popen(cmd).readlines()[1:]
for item in reslut:
if item != "\n":
all_devices.append(str(item).split("\t")[0])
self.log.info("获取的设备:%s" % str(all_devices))
return all_devices
def get_platform_version(self):
"""获取系统版本"""
return os.popen('adb shell getprop ro.build.version.release').read()
def get_apkname(self, apk):
"""
获取apk 的名字
:param apk: Android apk包路径
:return:
"""
a = APK(apk, False, "r")
self.log.info("获取到的包名: %s" % a.get_package())
return a.get_package()
def get_apk_lautc(self, apk):
"""
获取apk 的主activity
:param apk: Android apk包路径
:return:
"""
a = APK(apk, False, "r")
self.log.info("获取到的主activity: %s" % a.get_main_activity())
return a.get_main_activity()
def installapk(self, paknamepath: str, device: str) -> bool:
"""
安装apk
:param paknamepath: apk 包路径
:param devname: 设备名
:return:
"""
cmd = 'adb -s %s install %s' % (device, paknamepath)
self.log.info("安装信息:%s" % str(os.popen(cmd).read()))
return True
def isinstallapk(self, packname: str, device: str) -> bool:
"""
是否安装apk
:param packname:
:param device:
:return:
"""
cmd = "adb -s {} shell pm list packages -3".format(device)
reslut = os.popen(cmd).readlines()
all_apkname = []
for i in reslut:
apkname = str(i).split('\n')[0].split(":")[1]
all_apkname.append(apkname)
if packname in all_apkname:
return True
return False
def uninstallapk(self, packname: str, device: str) -> bool:
"""
卸载app, 如果村长就卸载
:param packname: apk 名
:param devname: 设备名
:return:
"""
cmd = "adb -s {} shell pm list packages -3".format(device)
reslut = os.popen(cmd).readlines()
all_apkname = []
for i in reslut:
apkname = str(i).split('\n')[0].split(":")[1]
all_apkname.append(apkname)
if packname in all_apkname:
cmd = 'adb -s %s uninstall %s ' % (device, packname)
os.system(cmd)
return True
return False
def push_file(self, file):
"""往手机push文件"""
cmd = "adb push {} /sdcard".format(file)
self.log.info("执行push 命令:%s" % cmd)
try:
os.popen(cmd).readlines()
return True
except Exception as e:
self.log.error(e)
return False
def adb_shell_get(self, file1, file2):
"""
手机往电脑传送文件
:param file1: 手机上的文件
:param file2: 放在电脑上什么地方
:return:
"""
cmd = "adb pull {} {}".format(file1, file2)
self.log.info("执行pull 命令:%s" % cmd)
try:
os.popen(cmd).readlines()
return True
except Exception as e:
self.log.error(e)
return False
def get_device_frim(self):
"""
获取手机厂商
ro.product.manufacturer
ro.product.vendor.brand
ro.product.system.brand
:return:
"""
info = os.popen("adb shell getprop ro.product.manufacturer").read()
self.log.info("get_device_frim %s" % info)
# 手机厂商不一样,可能获取不到,兼容
if len(info) == 1:
info = os.popen("adb shell getprop ro.product.vendor.brand").read()
return str(info).replace(" ", "").lower()
def get_device_model(self):
"""
获取手机型号
ro.product.odm.marketname
ro.product.product.marketname
ro.product.system.marketname
ro.product.vendor.marketname
ro.product.vendor.model
ro.product.model
:return:
"""
info = os.popen("adb shell getprop ro.product.model").read()
if len(info) == 1:
info = os.popen("adb shell getprop ro.product.system.model").read()
return str(info).replace(" ", "").lower()
adb 常用命令封装
原创
©著作权归作者所有:来自51CTO博客作者wx5954bcf74aaea的原创作品,请联系作者获取转载授权,否则将追究法律责任
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
Docker 常用命令
docker常用命令;包含image、container、network、volume、dockerfile、compose file等常用操作
docker image container network -
linux常用命令
linux基础:常用指令
linux常用命令 ls -
adb 常用命令、adb shell 常用命令
查看版本adb version
android Android 包名 -
adb常用命令介绍
adb常见实用命令
adb命令 重启 复制文件 包名 -
adb常用命令总结及.bat文件封装
adb常用命令Android 调试桥adb工作原理启动adb调试adb常用命令查看版本查看设备
adb android Android Desktop 包名