You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

通过ADB与PowerShell在Termux中执行speedtest-go并导出结果的问题

问题

我是一名初学者,正在开展项目:需在未root的三星S24 Ultra设备上,通过Termux运行speedtest-go命令,再借助PowerShell和ADB将结果下载到Windows电脑。具体目标:

  • 启动Android设备的Termux
  • 在Termux内执行speedtest-go并将输出重定向到设备文件
  • 通过ADB将结果文件拉取到Windows PC

现有环境:

  • Windows端运行PowerShell
  • 已安装配置ADB
  • Android设备的Termux已安装speedtest-go

我编写的PowerShell脚本(无法正常工作):

# Launch Termux
./adb shell monkey -p com.termux -c android.intent.category.LAUNCHER 1
Start-Sleep -Seconds 10  # Wait for Termux to launch

# Run the speedtest-go command and save the result to a file
./adb shell input text "speedtest-go > /sdcard/speedtest_result.txt" #The \ does not help
Start-Sleep -Seconds 10  # Wait for command input
./adb shell input keyevent 66  # Simulate Enter key press

# Wait for the command to complete
Start-Sleep -Seconds 60

# Pull the result file from the Android device to the PC
./adb pull /sdcard/speedtest_result.txt C:\Users\User\Desktop\platform-tools-latest-windows\platform-tools\Speedtest

Write-Output "File saved: C:\Users\User\Desktop\platform-tools-latest-windows\platform-tools\Speedtest"

遇到的问题:

  1. adb shell input text命令无法正确处理重定向符号(>),不清楚如何格式化命令实现Termux内的重定向
  2. 不确定是否有更优的实现流程或高效方法
  3. 存在权限问题:Termux与adb shell用户不同,手动执行命令可生成文件,但ADB无法访问/data/data/com.termux/files/home路径拉取文件

疑问:

  1. 如何通过ADB和PowerShell在Termux中正确执行speedtest-go并将输出重定向到文件?
  2. 能否改进现有脚本确保正常运行?或有更简便的方法在Android设备测试上下行速率并导出输出到Windows?

解决方案

一、修复原脚本的重定向问题

adb shell input text会把>识别为普通字符,无法触发Termux的shell重定向逻辑。可以通过Unicode编码输入>(编码为U+003E)绕过这个问题,同时把输出文件写到ADB和Termux都有权限访问的公共目录(比如/sdcard/Download),避免权限冲突。

改进后的PowerShell脚本:

# 1. 启动Termux应用
./adb shell monkey -p com.termux -c android.intent.category.LAUNCHER 1
Start-Sleep -Seconds 8  # 等待时间可根据设备启动速度调整

# 2. 输入测速命令,用Unicode编码输入>实现重定向
./adb shell input text "speedtest-go `u{003E} /sdcard/Download/speedtest_result.txt"
Start-Sleep -Seconds 3  # 等待命令输入完成
./adb shell input keyevent 66  # 模拟回车执行命令

# 3. 等待测速完成(根据网络情况调整,40秒足够大部分场景)
Start-Sleep -Seconds 40

# 4. 拉取文件到Windows指定路径
$targetDir = "C:\Users\User\Desktop\platform-tools-latest-windows\platform-tools\Speedtest"
# 确保目标目录存在
if (-not (Test-Path $targetDir)) {
    New-Item -ItemType Directory -Path $targetDir | Out-Null
}
./adb pull "/sdcard/Download/speedtest_result.txt" "$targetDir\"

Write-Output "文件已保存到: $targetDir"

二、更高效的后台执行方案(无需模拟界面操作)

模拟输入回车的方法稳定性差,推荐直接通过ADB切换到Termux用户权限,后台执行命令,完全跳过界面操作:

核心逻辑

Termux的私有shell路径为/data/data/com.termux/files/usr/bin/bash,通过adb shell run-as com.termux可以切换到Termux用户权限,直接执行命令并输出到公共目录。

对应PowerShell脚本:

# 直接在Termux环境下执行测速,输出到ADB可访问的公共目录
./adb shell run-as com.termux /data/data/com.termux/files/usr/bin/bash -c "speedtest-go > /sdcard/Download/speedtest_result.txt"

# 等待测速完成
Start-Sleep -Seconds 40

# 拉取文件到Windows
$targetDir = "C:\Users\User\Desktop\platform-tools-latest-windows\platform-tools\Speedtest"
if (-not (Test-Path $targetDir)) {
    New-Item -ItemType Directory -Path $targetDir | Out-Null
}
./adb pull "/sdcard/Download/speedtest_result.txt" "$targetDir\"

Write-Output "文件已保存到: $targetDir"

此方法无需启动Termux界面,稳定性和效率更高,完全避免模拟输入的各类问题。

三、权限问题说明

  • Termux的私有目录/data/data/com.termux/files/home只有Termux用户能访问,ADB默认用户无权限读取,因此必须将输出文件写到公共存储目录(如/sdcard/Download、/sdcard/Pictures等)。
  • 若一定要使用Termux私有目录,可先将文件复制到公共目录再拉取:
# 测速到Termux私有目录,再复制到公共目录
./adb shell run-as com.termux /data/data/com.termux/files/usr/bin/bash -c "speedtest-go > ~/speedtest_result.txt && cp ~/speedtest_result.txt /sdcard/Download/"

内容的提问来源于stack exchange,提问作者Karlitos Bemowo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.19 19:23:23