Last active
January 12, 2026 21:17
-
-
Save NTICompass/6dcab7ca187012cd81dde6646e4752cb to your computer and use it in GitHub Desktop.
SSH to Termux via ADB
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| declare -a START_COMMANDS=('sshd' 'termux-wake-lock') | |
| declare -a STOP_COMMANDS=('pkill\ sshd' 'termux-wake-unlock') | |
| # https://stackoverflow.com/a/8483797 | |
| KEYEVENT_HOME=3 | |
| KEYEVENT_ENTER=66 | |
| # Set up the port forwarding | |
| PORT=$(adb forward tcp:0 tcp:8022) | |
| # Run Termux and start the SSH server | |
| adb shell am start -n com.termux/.app.TermuxActivity | |
| sleep 1 | |
| for c in "${START_COMMANDS[@]}"; do | |
| adb shell input text "$c" | |
| adb shell input keyevent $KEYEVENT_ENTER | |
| done | |
| adb shell input keyevent $KEYEVENT_HOME | |
| # Connect to Termux's SSH server | |
| ssh -p"$PORT" localhost | |
| # Remove the port forward | |
| adb forward --remove-all | |
| # Run Termux and stop the SSH server | |
| adb shell am start -n com.termux/.app.TermuxActivity | |
| sleep 1 | |
| for c in "${STOP_COMMANDS[@]}"; do | |
| adb shell input text "$c" | |
| adb shell input keyevent $KEYEVENT_ENTER | |
| done | |
| adb shell input keyevent $KEYEVENT_HOME |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment