Skip to content

Instantly share code, notes, and snippets.

@NTICompass
Last active January 12, 2026 21:17
Show Gist options
  • Select an option

  • Save NTICompass/6dcab7ca187012cd81dde6646e4752cb to your computer and use it in GitHub Desktop.

Select an option

Save NTICompass/6dcab7ca187012cd81dde6646e4752cb to your computer and use it in GitHub Desktop.
SSH to Termux via ADB
#!/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