美文网首页
python-adb实现批命令

python-adb实现批命令

作者: 牵手生活 | 来源:发表于2023-11-12 10:08 被阅读0次

    subprocess 子进程管理文章收集

    subprocess — Subprocess management — Python 3.12.0 documentation

    subprocess --- 子进程管理 — Python 3.12.0 文档

    subprocess --- 子进程管理 — Python 3.12.0 文档
    subprocess --- 子进程管理 — Python 3.12.0 文档

    例子1-- adb 需要su权限,pull出手机文件

    先简单查看一下adb shell su的帮助

    C:\Users\Lenovo>adb shell su -h
    MagiskSU
    
    Usage: su [options] [-] [user [argument...]]
    
    Options:
      -c, --command COMMAND         pass COMMAND to the invoked shell
      -h, --help                    display this help message and exit
      -, -l, --login                pretend the shell to be a login shell
      -m, -p,
      --preserve-environment        preserve the entire environment
      -s, --shell SHELL             use SHELL instead of the default /system/bin/sh
      -v, --version                 display version number and exit
      -V                            display version code and exit
      -mm, -M,
      --mount-master                force run in the global mount namespace
    
    

    stdin, stdoutstderr 分别指定被执行程序的标准输入、标准输出和标准错误文件句柄。 合法的值包括 None, PIPE, DEVNULL, 现在的文件描述符(一个正整数),现存的具有合法文件描述符的 file object。 当使用默认设置 None 时,将不会进行任何重定向。 PIPE 表示应当新建一个连接子进程的管道。 DEVNULL 表示将使用特殊文件 os.devnull

    adb-pull-su 拉取文件
    # 进入adb shell
    import os
    
    import subprocess
    
    # 输入文件路径
    file_path = input("请输入文件路径:")
    # 进入adb shell并执行命令
    subprocess.run(['adb', 'shell', 'su', '-c', f'cp {file_path} /sdcard/'])
    
    # 从sdcard目录pull文件到当前文件夹
    subprocess.run(['adb', 'pull', f'/sdcard/{os.path.basename(file_path)}'])
    
    
    其中 f参数的说明

    例子2- adb pull文件

    手机 /sdcard/scrm/log/当前日期的指定文件log文件 ,如: /sdcard/scrm/log/2023-11-13log.txt

    # 拉取当天的日志
    import datetime
    import os.path
    import subprocess
    
    
    now = datetime.datetime.now()
    formatted_date = now.strftime("%Y-%m-%d")
    print(formatted_date)
    target_dir = os.path.dirname(os.path.abspath(__file__))
    cmd = 'adb pull /sdcard/scrm/log/' + formatted_date + 'Log.txt' + ' ' + target_dir
    
    # 创建子进程执行命令
    p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    
    # 获取命令执行结果
    stdout, stderr = p.communicate()
    
    # 打印结果
    print(stdout.decode('utf-8'))
    print(stderr.decode('utf-8'))
    
    

    向外部函数传入序列形式参数的一个例子如下:

    Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
    

    备注

    将 shell 命令拆分为参数序列的方式可能并不很直观,特别是在复杂的情况下。 shlex.split() 可以演示如何确定 args 适当的拆分形式:

    >>>import shlex, subprocess
    >>>command_line = input()
    /bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
    >>>args = shlex.split(command_line)
    >>>print(args)
    ['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
    >>>p = subprocess.Popen(args) # Success!
    

    特别注意,由 shell 中的空格分隔的选项(例如 -input)和参数(例如 eggs.txt )位于分开的列表元素中,而在需要时使用引号或反斜杠转义的参数在 shell (例如包含空格的文件名或上面显示的 echo 命令)是单独的列表元素。

    edge openAi 命令概述

    subprocess.run() 是 Python 的一个模块,它允许您生成新的进程,连接到它们的输入/输出/错误管道,并获取它们的返回代码。它是一个新的模块,替换了一些旧的模块和函数,如 os.systemos.spawn* 1

    调用子进程的推荐方法是对于所有它可以处理的用例都使用 run() 函数。对于更高级的用例,可以直接使用底层的 Popen 接口 1

    以下是使用 subprocess.run() 在 shell 中执行命令的示例:

    subprocess.run() is a Python module that allows you to spawn new processes, connect to their input/output/error pipes, and obtain their return codes. It is a newer module that replaces several older modules and functions such as os.system and os.spawn* 1.

    The recommended approach to invoking subprocesses is to use the run() function for all use cases it can handle. For more advanced use cases, the underlying Popen interface can be used directly 1.

    import subprocess
    
    result = subprocess.run(['ls', '-l'], stdout=subprocess.PIPE)
    print(result.stdout.decode('utf-8'))
    
    

    This code will execute the ls -l command in the shell and print the output to the console 2.

    I hope this helps! Let me know if you have any other questions.

    相关文章

      网友评论

          本文标题:python-adb实现批命令

          本文链接:https://www.haomeiwen.com/subject/gvwlwdtx.html