python 加載adb實現對支付寶登錄及後臺監控,與手機自身(電量,網絡狀態、進程後臺)監控,及實現手機任意位置模擬點擊。
#!/usr/bin/env python
# encoding: utf-8
"""
@version: v1.0
@author: W_H_J
@license: Apache Licence
@contact: [email protected]
@software: PyCharm
@file: aliPayShell.py
@time: 2019/1/25 10:28
@describe: 通過adb命令,使用shell操控手機,登錄支付寶
1. 模擬登錄支付寶
2. 對支付寶或手機後臺進行監控
3. shell input tap x, y :x,y 屏幕座標點,用該命令可點擊屏幕任意位置
指令大全:https://github.com/mzlogin/awesome-adb
"""
from subprocess import Popen, PIPE, STDOUT
import os
import time
import re
currentPath = os.path.dirname(os.path.realpath(__file__))
# 1. 連接手機
CMD_CONTENT_PHONE = "adb devices"
def cmd_return(mod, cmd_phone_id):
"""
執行cmd命令--公共指令
:param mod: 執行哪條命令,參數同以下if mod 判斷
:param cmd_phone_id: 手機動態id
:return: cmd命令
"""
if mod == "unlock":
# 2. 26: 電源鍵;123456:鎖屏密碼;66:確認
cmd_unlock_phone = "adb -s %s shell input keyevent 26 && adb shell input text 123456 && adb shell input keyevent 66" % (
cmd_phone_id)
return cmd_unlock_phone
if mod == "start":
# 3. 啓動支付寶
cmd_start_alipay = "adb -s %s shell am start com.eg.android.AlipayGphone/.AlipayLogin" % cmd_phone_id
return cmd_start_alipay
if mod == "kill":
# 4. 殺死支付寶
cmd_kill_alipay = "adb -s %s shell am force-stop com.eg.android.AlipayGphone" % cmd_phone_id
return cmd_kill_alipay
if mod == "wifi":
# 5. 打開WiFi,需要root
cmd_wifi_open_list = ["adb -s %s shell" % cmd_phone_id, "su", "svc wifi enable"]
return cmd_wifi_open_list
if mod == "callback":
# 6. 返回上級目錄
cmd_callback_phone = "adb -s %s shell input keyevent 4" % cmd_phone_id
return cmd_callback_phone
if mod == "ps":
# 7. 查看支付寶進程
cmd_ps_aliPay = "adb -s %s shell ps | findstr com.eg.android.AlipayGphone" % cmd_phone_id
return cmd_ps_aliPay
if mod == "enter":
# 8. 回車
cmd_enter_phone = "adb -s %s shell input keyevent 66" % cmd_phone_id
return cmd_enter_phone
if mod == "battery":
# 9. 電量監控
cmd_battery_phone = "adb -s %s shell dumpsys battery" % cmd_phone_id
return cmd_battery_phone
def content_phone(cmd):
"""
連接手機
:param cmd: 加載驅動
:return: 返回手機ID列表
"""
p = Popen(cmd, shell=True, stdin=PIPE, stdout=PIPE, stderr=STDOUT, cwd=currentPath)
output, errors = p.communicate()
if errors:
print("ERROR===> ", errors)
list_output = output.decode().replace("List of devices attached ", "").split(" ")
list_id = []
for id in list_output:
list_id.append(id.replace("\r", "").replace("\n", "").replace("\t", "").replace("device", ""))
return list_id
def common_popen(cmd):
"""
公共執行命令
:param cmd: 執行命令
:return: 返回結果 stdout=PIPE, stderr=STDOUT
"""
p = Popen(cmd, shell=True, stdin=PIPE, stdout=PIPE, stderr=STDOUT, cwd=currentPath)
output, errors = p.communicate()
if errors:
print("ERROR===> ", errors)
return output
def ps_aliPay(cmd_content):
"""
支付寶進程監控
:param cmd_content:
:return:
"""
for cmd_id in cmd_content:
list_ps = []
back = common_popen(cmd_return("ps", cmd_id))
for i in str(back).split("\\r\\r\\n"):
if 'AlipayGphone' in i:
print("==>", i)
list_ps.append(i)
print(len(list_ps))
return cmd_content, len(list_ps)
def open_aliPay(cmd_content):
"""
打開支付寶APP
:param cmd_content: 手機ID
:return:
"""
content_id, int_status = ps_aliPay(cmd_content)
if int_status == 3:
print("==> aliPay is already open!")
else:
for cmd_id in cmd_content:
common_popen(cmd_return("start", cmd_id))
time.sleep(5) # 打開界面需要等待
content_id_new, int_status_new = ps_aliPay(cmd_content)
if int_status_new == 3:
print("==> aliPay successful open!")
else:
print("==> aliPay open fail!")
def kill_aliPay(cmd_content):
"""
殺死支付寶進程
:param cmd_content: 手機ID
:return:
"""
for cmd_id in cmd_content:
common_popen(cmd_return("kill", cmd_id))
content_id, int_status = ps_aliPay(cmd_content)
if int_status != 3 or int_status == 0:
print("==> aliPay successful kill!")
def battert_phone(cmd_content):
"""
手機電量監控
:param cmd_content:
:return: 參數: {"powered": "供電方式:USB/AC", "status": "電池充放電狀態", "health": "電池狀態", "level": "電量百分比"}
"""
dict_batter = {"powered": "", "status": "", "health": "", "level": ""}
for cmd_id in cmd_content:
str_batter = common_popen(cmd_return("battery", cmd_id))
for k in str(str_batter).split("\\r\\r\\n"):
if 'status' in k:
dict_batter['status'] = re.sub("\D", "", k) # status: 電池狀態,2:充電狀態,其他數字爲非充電狀態
# print(k)
if 'health' in k:
dict_batter['health'] = re.sub("\D", "", k) # health: 電池充電狀態,2:表示good
# print(k)
if 'level' in k:
dict_batter['level'] = re.sub("\D", "", k) # level: 電池電量:百分比
# print(k)
if 'USB powered' in k:
if 'true' in k: # usb powered: usb供電方式
dict_batter['powered'] = 'USB'
else:
dict_batter['powered'] = 'another'
print(dict_batter)
def unlock_phone(cmd_content):
"""
解鎖手機
:param cmd_content: 手機ID
:return:
"""
for cmd_id in cmd_content:
common_popen(cmd_return("unlock", cmd_id))
def run_alipay_phone(cmd_content):
open_aliPay(cmd_content)
while True:
ps_aliPay(cmd_content)
battert_phone(cmd_content)
time.sleep(30)
if __name__ == '__main__':
cmd_content = content_phone(CMD_CONTENT_PHONE)
open_aliPay(cmd_content)