python 与ADB shell交互

发布时间:2019-09-07 08:06:06编辑:auto阅读(1979)

    python交互

    使用subprocess
    使设备具有部分Linux命令
    python脚本:下载busybox,并且放在与脚本相同的目录下

    # coding:utf-8
    # 这个脚本用于使环境拥有busybox的环境
    import subprocess
    
    import os
    print "开始push文件"
    os.system(" adb push busybox /system/xbin/")
    print "开始root"
    os.system("adb root")
    print "开始remote"
    os.system("adb remount")
    
    #这里面的命令全部都会在Linux的环境下执行,最后需要退出。
    cmds = [
        "chmod  755 /system/xbin/busybox",
        "cd /system/xbin/",
        "busybox --install .",
        "awk",
        "ps |grep com.txznet.music|awk '{print $2}' | head -n 1",
        "exit",#这是是非常关键的,退出
    ]
    print "开始安装"
    pipe = subprocess.Popen("adb shell", stdin=subprocess.PIPE, stdout=subprocess.PIPE)
    code = pipe.communicate("\n".join(cmds) + "\n");
    print code
    print "安装结束"
    

    杀掉相应的进程

    # coding:utf-8
    # 这个脚本是用来杀掉music的进程的
    import os
    import string
    import subprocess
    def kill(result):
        print result.strip()
        code = os.system(" adb shell kill " + result.strip())
        print code
    
    processname = os.system('''adb shell ps|grep com.txznet.music|awk '{print$2","}''''')
    
    
    # print processname
    # result="["+str(processname)+"]"
    # print result
    # for processID in result:
    #     print processID.strip()
    #     code=os.system(" adb shell kill "+processID.strip())
    #     print code
    
    pipe = subprocess.Popen("adb shell ps|grep com.txznet.music|awk '{print$2}'", stdin=subprocess.PIPE,
                            stdout=subprocess.PIPE)
    
    # result=pipe.communicate("ps|grep com.txznet.music|awk '{print$2","}'")
    # print result
    result = pipe.stdout.readline()
    result1 = pipe.stdout.readline()
    result2 = pipe.stdout.readline()
    print result
    kill(result)
    print result1
    kill(result1)
    print result2
    kill(result2)
    

关键字