1. 程式人生 > >手機adb devices 裝置管理器指令碼

手機adb devices 裝置管理器指令碼

import rethinkdb as r
# connect_devices_list=[ 'DY8SROFQ496PYLKN', '4998e018', '810BBMH225MC', 'ee1ec7d7d83', '52F6T16604013599', 'DWH9X17619W00040', 'BY3AME1554162198', 'HEQGQGYTAAAQTCRG', '721QACRD33QV3', '810EBLT2EJDD', '582af7aca89d', 'AVY9KA95B1603072', 'accf85567518', '4ebba4d5', '4LFIBA6PJJV8TGNZ', '50049bbe7ce4', 'b491cadc', 'F8UDU16411003817', 'M960BDQH22CRM', 'XKG4C16A25001527', 'CYSBBBA6C2313732', '0123456789ABCDEF', '8DF6R17324000371', 'TWGDU17322002764']
def get_device():
    #adb devices 可顯示的裝置有
    import subprocess,os
    '''
    :return: devices_list 裝置列表  device_num  裝置數量
    '''

    devices_result=subprocess.getoutput("adb devices")
    devices_list=devices_result.replace("no permissions","").replace("device","").replace("\t","").split("\n")[1:-1]
    device_num=len(devices_list)
    unauthorized_devices_list=[]
    for i in devices_list:
        if "unauthorized" in i :
            devices_list.remove(i)
            unauthorized_devices_list.append(i.replace("unauthorized",""))
    return devices_list, device_num

def get_devices_db():
    #返回讀資料庫得到連線上的裝置
    connect_devices_list=[]
    dbname = 'stf'
    conn = r.connect('172.27.82.35', db=dbname).repl()
    tablename = 'devices'
    db = r.db('stf')
    if tablename not in db.table_list().run():  #db.table_list().run()  列舉資料表
        db.table_create(tablename).run()
    res = r.db('stf').table('devices').filter(({"present": "true"})&(r.row["serial"])).run(conn)
    for i ,n in enumerate(res):
        if n["present"]==True:
            connect_devices_list.append(n["serial"])
    return connect_devices_list


def filter_not_connect_devices():
    adb_devices_list, device_num = get_device()
    print("ab devices 連上的裝置有",len(adb_devices_list),"臺:",adb_devices_list)
    connect_devices_list=get_devices_db()
    print("資料庫裡連線的裝置有",len(connect_devices_list),"臺:",connect_devices_list)
    not_connect_devices_list=[]
    for i in adb_devices_list:
        if i not in connect_devices_list:
            not_connect_devices_list.append(i)
    return not_connect_devices_list
not_connect_devices_list=filter_not_connect_devices()
print("沒有連線上的裝置有",len(not_connect_devices_list),"臺:",not_connect_devices_list)