3

I want to install Python on an Android phone (the phone is connected to a Raspberry Pi so I don't have any GUI or physical access to the device). I can connect to the phone via adb shell and run/install packages via APK files easily. I want to install Python on the device to run speedtest-cli remotely via adb shell. https://github.com/sivel/speedtest-cli

speedtest-cli requires Python. So far I have tried the following based on online articles.

  1. Installed Termux app via APK. I can access the underlying files remotely. I tried to install python using pkg command but could not. Error below.
    1|:/data/data/com.termux/files/usr/bin # ./pkg install python                                                                                                                                               
    This must NOT be run as root as it will break your environment (root will be required for nearly everything due to changed file permissions)
    1|marlin:/data/data/com.termux/files/usr/bin # whoami
    root
    
  2. Installed Qpython on the device using adb. However, the issue is that when I launch adb command to launch Qpython, I see a prompt on the device - "Allow Qpython to access media Options - Accept or Deny". Since I don't have access to the device, I can't select either of the options. Is there a way to send a command via ADB to select - Accept?

I would appreciate any other ideas as well. My end goal is to run the speedtest-cli utility via adb shell on a device that is not physically accessible.

2 Answers 2

2

Solved this eventually. Here is what I did -

  1. Installed Termux via downloading the APK and performing adb install com.termux.apk

  2. Remotely launched the Termux app via ADB (monkey) (this is needed to complete the installation on the device) monkey -p com.termux -c android.intent.category.LAUNCHER

  3. Once Termux is installed, I installed python and speedtest-cli on the device using adb input text and keyevents -

    adb input text "pkg install python"
    adb input keyevent "66"
    adb input text "pip install speedtest-cli"
    
  4. Then navigate to /data/data/com.termux/files/usr/bin. Here you should have the speedtest-cli script that you can invoke from adb shell

    /data/data/com.termux/files/usr/bin # ./speedtest-cli                                                                                                                                                      
    Retrieving speedtest.net configuration...
    Testing from XXXXX
    Testing download speed................................................................................
    Download: 20.18 Mbit/s
    Testing upload speed......................................................................................................
    Upload: 3.98 Mbit/s
    
2
  • I had to add a COUNT param to the monkey command, i.e. monkey -p com.termux -c android.intent.category.LAUNCHER 1 Aug 21, 2023 at 8:07
  • And you can alternatively do am start -n com.termux/.HomeActivity to open Termux (though you may have to close the soft keyboard, e.g. input keyevent 4) Aug 21, 2023 at 8:34
1

I basically followed the solution given by rfguy.
But I had this error:

could not find a version that satisfies the requirement speedtest-cli (from versions: none)
pip is configured with locations that require tls/ssl

enter image description here

I solved installing openssl too on Termux, as follows:

# uninstall Termux
adb uninstall com.termux
sleep 2

# install the apk
adb install -r ~/Desktop/Termux.apk
sleep 2

# open the app
adb shell monkey --pct-syskeys 0 -p com.termux 1
sleep 8

# install python
adb shell input text 'pkg%sinstall%spython'
sleep 2

# enter
adb shell input keyevent 66
sleep 15

# yes
adb shell input text 'Y'
sleep 2

# enter
adb shell input keyevent 66
sleep 60

# install openssl
adb shell input text 'pkg%sinstall%sopenssl'
sleep 2

# enter
adb shell input keyevent 66
sleep 10

# yes
adb shell input text 'Y'
sleep 2

# enter
adb shell input keyevent 66
sleep 15

# install speedtest-cli
adb shell input text 'pip%sinstall%sspeedtest-cli'
sleep 2

# enter
adb shell input keyevent 66
sleep 30

# run speedtest-cli
adb shell /data/data/com.termux/files/usr/bin/speedtest-cli

# hide termux
adb shell pm hide com.termux

You can run this script with a sh .\this_script.sh if you are already connected to the device with an adb connect your_device_ip

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .