Advertisement
pchel48

termux-pinger.sh

Jan 29th, 2017
432
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.82 KB | None | 0 0
  1. #!/data/data/com.termux/files/usr/bin/bash
  2.  
  3. # Termux bash script for checking internet connection by periodically pinging some host
  4. # Requirements:
  5. #   Termux - https://termux.com/
  6. #   Termux:API add-on - https://termux.com/add-on-api.html
  7. # Optional, for convenient use "just as an app":
  8. #   Tasker - https://tasker.dinglisch.net/
  9. #   Termux:Task add-on - https://termux.com/add-on-task.html
  10. #   Place the script under /data/data/com.termux/files/home/.termux/tasker/
  11. #   In Tasker: Add a new task, create an action - Plugin - Termux:Task - Edit the configuration and select the script
  12. #   In your launcher: add Widget - Tasker - Task Shortcut and choose your task
  13. # Script is using termux-notification
  14. # https://github.com/termux/termux-packages/blob/master/packages/termux-api/termux-notification
  15.  
  16. ### Config
  17.  
  18. SITE="ya.ru"                    # hostname to ping
  19. INTERVAL=10                     # ping interval in seconds
  20. LED_ON_MS=800                   # milliseconds for the LED to be on while it's flashing
  21. LED_OFF_MS=600                  # milliseconds for the LED to be off while it's flashing
  22. LED_UP_COLOR=00ff00             # color of the blinking LED when there is connection as RRGGBB (green)
  23. LED_DOWN_COLOR=ff0000           # color of the blinking LED when there is no connection as RRGGBB (red)
  24. VIBRATE_UP_PAT=300              # vibrate pattern when connection is established, comma separated
  25. VIBRATE_DOWN_PAT=150,150,150    # vibrate pattern when connection is lost, comma separated
  26. TITLE_UP="Inet is UP"           # notification title when connection is established
  27. TITLE_DOWN="Inet is DOWN"       # notification title when connection is lost
  28. NOTIFY_ID=666                   # notification ID
  29.  
  30. ### Script
  31.  
  32. PREV_STATUS=-1
  33. BTN_NAME="Stop"
  34. BTN_ACTION="termux-notification-remove '${NOTIFY_ID}' && kill -9 $$"
  35. NOTIFY_BIN="termux-notification"
  36. NOTIFY_ARGS_COMMON="--id '${NOTIFY_ID}' --priority max --action '${BTN_ACTION}' --button1 '${BTN_NAME}' --button1-action '${BTN_ACTION}' --led-on '${LED_ON_MS}' --led-off '${LED_OFF_MS}'"
  37. NOTIFY_ARGS_UP="--led-color '${LED_UP_COLOR}' --vibrate '${VIBRATE_UP_PAT}' --title '${TITLE_UP}'"
  38. NOTIFY_ARGS_DOWN="--led-color '${LED_DOWN_COLOR}' --vibrate '${VIBRATE_DOWN_PAT}' --title '${TITLE_DOWN}'"
  39.  
  40. while true
  41. do
  42.     PING_MSG=`ping -c 1 "${SITE}" 2>&1 | tail -n 2 ; exit ${PIPESTATUS[0]}`
  43.     PING_STATUS=$?
  44.     NOTIFY_ARGS_CONTENT="--content '${PING_MSG}'"
  45.     if [[ ${PING_STATUS} -eq 0 ]]
  46.     then
  47.         if [[ ${PREV_STATUS} -ne 0 ]]
  48.         then
  49.         eval ${NOTIFY_BIN} ${NOTIFY_ARGS_COMMON} ${NOTIFY_ARGS_CONTENT} ${NOTIFY_ARGS_UP}
  50.         fi
  51.     else
  52.         if [[ ${PREV_STATUS} -le 0 ]]
  53.         then
  54.         eval ${NOTIFY_BIN} ${NOTIFY_ARGS_COMMON} ${NOTIFY_ARGS_CONTENT} ${NOTIFY_ARGS_DOWN}
  55.         fi
  56.     fi
  57.     PREV_STATUS=${PING_STATUS}
  58.     sleep $INTERVAL
  59. done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement