Skip to content

Instantly share code, notes, and snippets.

@setiawanjemy88
Forked from gpchelkin/termux-pinger.md
Created February 19, 2022 12:10
Show Gist options
  • Save setiawanjemy88/81aa06b5fc0ea067a4bd47b83b3be877 to your computer and use it in GitHub Desktop.
Save setiawanjemy88/81aa06b5fc0ea067a4bd47b83b3be877 to your computer and use it in GitHub Desktop.
Termux Bash script for checking internet connection by periodically pinging some host and showing notifications with LED and vibration when connection is lost or established

termux-pinger.sh

Termux Bash script for checking internet connection by periodically pinging some host and showing notifications with LED and vibration when connection is lost or established using termux-notification API

Requirements

  • Termux
  • Termux:API add-on
  • Execute the following commands to place script termux-pinger.sh into the home directory with the proper permissions (get the link to the raw termux-pinger.sh below):
curl -O https://gist.githubusercontent.com/gpchelkin/b18916c56ff7c759860725a28a747242/raw/b8aa96bb2c7e1dac84f854980eaeb6cd194dde4f/termux-pinger.sh
chmod +x termux-pinger.sh
  • Now you can run it like this:
./termux-pinger.sh

Optional, for convenient use "just like an app"

  • Tasker
  • Termux:Task add-on
  • In Termux: mkdir -p ~/.termux/tasker && mv termux-pinger.sh ~/.termux/tasker
  • In Tasker: Tasks — Add a new task pinger — Add an action — Plugin — Termux:Task — Edit Configuration — Executable: termux-pinger.sh, Arguments: empty, Execute in a terminal session: disabled — Save — Exit.
  • In your launcher (I recommend Nova): Add Widget — Tasker — Task Shortcut — Choose task pinger and add icon — I dunno what's next
#!/data/data/com.termux/files/usr/bin/bash
### Configuration
SITE="ya.ru" # hostname to ping
INTERVAL=7 # ping interval in seconds
LED_ON_MS=800 # milliseconds for the LED to be on while it's flashing
LED_OFF_MS=600 # milliseconds for the LED to be off while it's flashing
LED_UP_COLOR="00ff00" # color of the blinking LED when there is connection as RRGGBB (green)
LED_DOWN_COLOR="ff0000" # color of the blinking LED when there is no connection as RRGGBB (red)
VIBRATE_UP_PAT=300 # vibrate pattern when connection is established, comma separated
VIBRATE_DOWN_PAT=150,150,150 # vibrate pattern when connection is lost, comma separated
TITLE_UP="Internet is UP" # notification title when connection is established
TITLE_DOWN="Internet is DOWN" # notification title when connection is lost
NOTIFY_ID=666 # notification ID
### Job
BTN_NAME="Stop pinging"
BTN_ACTION="termux-notification-remove '${NOTIFY_ID}' && kill $$"
NOTIFY_ARGS_COMMON="--id '${NOTIFY_ID}' --priority max --on-delete '${BTN_ACTION}' --action '${BTN_ACTION}' --button1 '${BTN_NAME}' --button1-action '${BTN_ACTION}' --led-on '${LED_ON_MS}' --led-off '${LED_OFF_MS}'"
NOTIFY_ARGS_UP="--led-color '${LED_UP_COLOR}' --sound --vibrate '${VIBRATE_UP_PAT}' --title '${TITLE_UP}'"
NOTIFY_ARGS_DOWN="--led-color '${LED_DOWN_COLOR}' --sound --vibrate '${VIBRATE_DOWN_PAT}' --title '${TITLE_DOWN}'"
PREV_STATUS=-1
while true
do
PING_MSG=`ping -c 1 "${SITE}" 2>&1 | tail -n 2 ; exit ${PIPESTATUS[0]}`
PING_STATUS=$?
NOTIFY_ARGS_CONTENT="--content '${PING_MSG}'"
if [[ ${PING_STATUS} -eq 0 ]]
then
if [[ ${PREV_STATUS} -ne 0 ]]
then eval termux-notification ${NOTIFY_ARGS_COMMON} ${NOTIFY_ARGS_CONTENT} ${NOTIFY_ARGS_UP}
fi
else
if [[ ${PREV_STATUS} -le 0 ]]
then eval termux-notification ${NOTIFY_ARGS_COMMON} ${NOTIFY_ARGS_CONTENT} ${NOTIFY_ARGS_DOWN}
fi
fi
PREV_STATUS=${PING_STATUS}
sleep $INTERVAL
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment