Skip to content

Instantly share code, notes, and snippets.

@parkerlreed
Last active June 13, 2024 17:00
Show Gist options
  • Save parkerlreed/ebc3c24a08b2d94dd510f502f3d5edd7 to your computer and use it in GitHub Desktop.
Save parkerlreed/ebc3c24a08b2d94dd510f502f3d5edd7 to your computer and use it in GitHub Desktop.
betteradbcopy - Multi-threaded SCP to/from Android device over USB/WiFi with Termux
#!/bin/bash
# Make sure to have OpenSSH setup in Termux as well as setting a password with `passwd`
# Requires https://github.com/upa/mscp and adb platform tools on your local machine
init_sshd() {
adb shell -t "run-as com.termux files/usr/bin/bash -lic 'export PATH=/data/data/com.termux/files/usr/bin:$PATH; export LD_PRELOAD=/data/data/com.termux/files/usr/lib/libtermux-exec.so; sshd'"
adb forward tcp:8022 tcp:8022;
}
stop_sshd() {
adb shell -t "run-as com.termux files/usr/bin/bash -lic 'export PATH=/data/data/com.termux/files/usr/bin:$PATH; export LD_PRELOAD=/data/data/com.termux/files/usr/lib/libtermux-exec.so; killall sshd'"
adb forward --remove tcp:8022;
}
pull_path="$2"
case "$1" in
pull)
init_sshd
mscp -P 8022 parker@127.0.0.1:"$pull_path" .
stop_sshd
;;
push)
push_path="$3"
init_sshd
mscp -P 8022 "$pull_path" parker@127.0.0.1:"$push_path"
stop_sshd
;;
*)
echo "Invalid option"
;;
esac
@parkerlreed
Copy link
Author

[unawesome@unawesomePC]$ ./betteradbcopy pull /sdcard/
bash: syntax error near unexpected token `('
8022
Password:
stat: /sdcard/ Permission denied
total 0 byte transferred
bash: syntax error near unexpected token `('

I'm having a bit of trouble running it. I have installed mscp and set passwd but it doesn't seem to work. Any possible solution?

So you have Termux on the phone, OpenSSH installed, password set, and granted storage? termux-setup-storage

Not sure why the adb invocation is producing the unexpected token.

After verifying everything above post your output for

bash -x ./betteradbcopy pull /sdcard/

So we can get an idea of what is being built out as it's running.

@TheUnawsomeGuy
Copy link

Termux on phone, openssh installed, password set and termux-setup-storage has been run

[unawesomeguy@unawesomepc unawesomeguy]$ bash -x ./betteradbcopy pull /sdcard/
+ pull_path=/sdcard/
+ case "$1" in
+ init_sshd
+ adb shell -t 'run-as com.termux files/usr/bin/bash -lic '\''export PATH=/data/data/com.termux/files/usr/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/usr/lib/wsl/lib:/mnt/c/Program Files (x86)/Eclipse Adoptium/jdk-8.0.322.6-hotspot/bin:/mnt/c/Program Files (x86)/Eclipse Adoptium/jre-8.0.322.6-hotspot/bin:/mnt/c/Program Files (x86)/Eclipse Adoptium/jre-11.0.14.101-hotspot/bin:/mnt/c/Windows/system32:/mnt/c/Windows:/mnt/c/Windows/System32/Wbem:/mnt/c/Windows/System32/WindowsPowerShell/v1.0/:/mnt/c/Windows/System32/OpenSSH/:/mnt/c/Program Files (x86)/NVIDIA Corporation/PhysX/Common:/mnt/c/Program Files/NVIDIA Corporation/NVIDIA NvDLISR:/mnt/c/ProgramData/chocolatey/bin:/mnt/c/Program Files/OpenJDK/openjdk-8u312-b07-jre/bin:/mnt/c/Program Files/OpenJDK/openjdk-11.0.13_8-jre/bin:/mnt/g/Util/A/platform-tools:/mnt/c/Program Files/dotnet/:/mnt/c/WINDOWS/system32:/mnt/c/WINDOWS:/mnt/c/WINDOWS/System32/Wbem:/mnt/c/WINDOWS/System32/WindowsPowerShell/v1.0/:/mnt/c/WINDOWS/System32/OpenSSH/:/mnt/g/mmm:/mnt/c/Program Files/Git/cmd:/mnt/c/Program Files (x86)/ZeroTier/One/:/mnt/c/Program Files/nodejs/:/mnt/c/Program Files/Docker/Docker/resources/bin:/mnt/c/Program Files/gsudo/Current:/mnt/c/Program Files/WireGuard/:/mnt/c/Users/unawesomeguy/AppData/Local/Programs/Python/Python310/Scripts/:/mnt/c/Users/unawesomeguy/AppData/Local/Programs/Python/Python310/:/mnt/c/Users/unawesomeguy/AppData/Local/Microsoft/WindowsApps:/mnt/c/Users/unawesomeguy/AppData/Roaming/npm:/mnt/c/Users/unawesomeguy/Desktop/SillyTavern - Copy/TavernAI-extras/ss; export LD_PRELOAD=/data/data/com.termux/files/usr/lib/libtermux-exec.so; sshd'\'''
bash: syntax error near unexpected token `('
+ adb forward tcp:8022 tcp:8022
8022
+ mscp -P 8022 parker@127.0.0.1:/sdcard/ .
Password:
stat: /sdcard/ Permission denied
total 0 byte transferred
+ stop_sshd
+ adb shell -t 'run-as com.termux files/usr/bin/bash -lic '\''export PATH=/data/data/com.termux/files/usr/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/usr/lib/wsl/lib:/mnt/c/Program Files (x86)/Eclipse Adoptium/jdk-8.0.322.6-hotspot/bin:/mnt/c/Program Files (x86)/Eclipse Adoptium/jre-8.0.322.6-hotspot/bin:/mnt/c/Program Files (x86)/Eclipse Adoptium/jre-11.0.14.101-hotspot/bin:/mnt/c/Windows/system32:/mnt/c/Windows:/mnt/c/Windows/System32/Wbem:/mnt/c/Windows/System32/WindowsPowerShell/v1.0/:/mnt/c/Windows/System32/OpenSSH/:/mnt/c/Program Files (x86)/NVIDIA Corporation/PhysX/Common:/mnt/c/Program Files/NVIDIA Corporation/NVIDIA NvDLISR:/mnt/c/ProgramData/chocolatey/bin:/mnt/c/Program Files/OpenJDK/openjdk-8u312-b07-jre/bin:/mnt/c/Program Files/OpenJDK/openjdk-11.0.13_8-jre/bin:/mnt/g/Util/A/platform-tools:/mnt/c/Program Files/dotnet/:/mnt/c/WINDOWS/system32:/mnt/c/WINDOWS:/mnt/c/WINDOWS/System32/Wbem:/mnt/c/WINDOWS/System32/WindowsPowerShell/v1.0/:/mnt/c/WINDOWS/System32/OpenSSH/:/mnt/g/mmm:/mnt/c/Program Files/Git/cmd:/mnt/c/Program Files (x86)/ZeroTier/One/:/mnt/c/Program Files/nodejs/:/mnt/c/Program Files/Docker/Docker/resources/bin:/mnt/c/Program Files/gsudo/Current:/mnt/c/Program Files/WireGuard/:/mnt/c/Users/unawesomeguy/AppData/Local/Programs/Python/Python310/Scripts/:/mnt/c/Users/unawesomeguy/AppData/Local/Programs/Python/Python310/:/mnt/c/Users/unawesomeguy/AppData/Local/Microsoft/WindowsApps:/mnt/c/Users/unawesomeguy/AppData/Roaming/npm:/mnt/c/Users/unawesomeguy/Desktop/SillyTavern - Copy/TavernAI-extras/ss; export LD_PRELOAD=/data/data/com.termux/files/usr/lib/libtermux-exec.so; killall sshd'\'''
bash: syntax error near unexpected token `('
+ adb forward --remove tcp:8022

I guess it doesn't expect to be ran on WSL2 but I don't understand why permission is denied. I tried passwd only and passwd parker. I'm also connecting adb using WiFi.

@parkerlreed
Copy link
Author

You need to escape the path in the adb command. Mine was pulling in the computer path as well, but because it was all Unix, it didn't mess with it.

files/usr/bin/bash -lic 'export PATH=/data/data/com.termux/files/usr/bin:"$PATH"; export

It's either double or single quotes I don't recall exactly.

@parkerlreed
Copy link
Author

Worst comes to worse. I guess it's not really needed since mine was all garbage too.

files/usr/bin/bash -lic 'export PATH=/data/data/com.termux/files/usr/bin; export

@TheUnawsomeGuy
Copy link

That solved one problem but stat: /sdcard/ Permission denied is still an issue.

@parkerlreed
Copy link
Author

And you can list the SD card within termux directly?

Only thing I can think of is maybe try removing the trailing forward slash.

pull /sdcard

@TheUnawsomeGuy
Copy link

I can cd into sdcard and make it show the files. I tried what you suggested and it's just the same.

@parkerlreed
Copy link
Author

Ok this must be something new with Android 14. I'm seeing the same error here. I can list in Termux but not over the adb shell termux wrapper or SSH.

I'll let you know if I get it working.

@parkerlreed
Copy link
Author

In the meantime, the proper fix for path is \ escaping the $

adb shell -t "run-as com.termux files/usr/bin/bash -lic 'export PATH=/data/data/com.termux/files/usr/bin:\$PATH; export LD_PRELOAD=/data/data/com.termux/files/usr/lib/libtermux-exec.so; bash'"```

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment