diff --git a/update_pihole.sh b/update_pihole.sh old mode 100755 new mode 100644 index 85c2cdf..ba699d8 --- a/update_pihole.sh +++ b/update_pihole.sh @@ -1,4 +1,4 @@ -#!/usr/bin/env bash +#!/bin/bash # Verify pihole version (run pihole -v) and send a notfication to Gotify @@ -12,6 +12,9 @@ bold="\033[1m" box="\033[1;41m" reset="\033[0m" +export PATH="/usr/local/bin:$PATH" +# run by cron $PATH=/usr/bin:/bin + command -v pihole >/dev/null 2>&1 || { echo -e "${bold}pihole${reset} is not installed. Aborting..." >&2; exit 1; } curl -Is https://www.apple.com | head -1 | grep 200 1>/dev/null @@ -21,6 +24,14 @@ if [[ $? -eq 1 ]]; then exit 1 fi +host=$(hostname) +available=false +msg_md= +msg_html= +infos= +# Choose the notification +notif="pushover" + dotenv () { set -a # shellcheck disable=SC1091 @@ -30,11 +41,38 @@ dotenv () { dotenv -gotify_server="https://gotify.photos-nas.ovh" -host=$(hostname) -available=false -msg= -infos= +send_gotify_notification() { + now=$(date +"%d-%m-%Y %T") + gotify_server="https://gotify.maboiteverte.fr" + TITLE="Pi-hole on $host update" + MESSAGE="**A new version of Pi-hole is available:**\n\n $msg_md\n\n $infos\n\n Please run *pihole -up* on $host to update !" + PRIORITY=8 + URL="$gotify_server/message?token=$token_gotify&?format=markdown" + + echo -e "Sending notification to $gotify_server ..." + # -S, --show-error Show error even when -s is used + # -s, --silent Silent mode + # -v Verbose + + curl -s -S --output /dev/null --data '{"message": "'"${MESSAGE}"'", "title": "'"${TITLE}"'", "priority":'"${PRIORITY}"', "extras": {"client::display": {"contentType": "text/markdown"}}}' -H 'Content-Type: application/json' "$URL" + + [ $? -eq 0 ] && echo -e "${greenbold}Gotify notification sent successfully !${reset}" || echo -e "${redbold}error sending Gotify notification !${reset}" +} + +send_pushover_notification() { + echo -e "Sending Pushover notification ..." + curl -s -F "token=$BASH_APP" \ + -F "user=$USER_KEY" \ + -F "title=$1" \ + -F priority=2 \ + -F html=1 \ + -F retry=60 \ + -F expire=86400 \ + -F "message=$2" https://api.pushover.net/1/messages.json + + [ $? -eq 0 ] && echo -e "${greenbold}Pushover notification sent successfully !${reset}" || echo -e "${redbold}error sending Pushover notification !${reset}" +} + echo -e "${box}Find Pi-hole update on $host${reset}\n" @@ -51,21 +89,23 @@ while IFS= read -r line; do if [ "$current" != "$last" ]; then printf " \e[1;31m%-10s\e[0m \e[1;31m%-10s\e[0m \e[1;31m%-10s\e[0m \n" "${app}" "${current}" "${last}" - msg+="**${app}: current:${current} last:${last}**\n" + msg_md+="**${app}: current:${current} last:${last}**\n" + msg_html+="${app}: current:${current} last:${last}
" available=true infos+="https://github.com/pi-hole/${app}/releases/tag/${last}\n" else printf " \e[1;32m%-10s\e[0m \e[1;32m%-10s\e[0m \e[1;32m%-10s\e[0m \n" "${app}" "${current}" "${last}" - msg+="${app}: current:${current} last:${last}\n" + msg_md+="${app}: current:${current} last:${last}\n" + msg_html+="${app}: current:${current} last:${last}
" fi done <<< "$upd" echo -e "\n$infos" -if [ "$available" = true ];then - - if [ ! -t 0 ]; then +if [ ! "$available" = true ];then + + if [ -t 0 ]; then # "I'm on a TTY, this is interactive." a=$(echo -e "\nPlease run ${italic}pihole -up${reset} to update ! (y/n)") read -p "$a" choice @@ -74,13 +114,13 @@ if [ "$available" = true ];then fi else - echo -e "Sending notification to $gotify_server ..." - TITLE="Pi-hole on $host update" - MESSAGE="**A new version of Pi-hole is available:**\n\n $msg\n\n $infos\n\n Please run *pihole -up* on $host to update !" - PRIORITY=8 - URL="$gotify_server/message?token=$token&?format=markdown" - - curl -s -S --output /dev/null --data '{"message": "'"${MESSAGE}"'", "title": "'"${TITLE}"'", "priority":'"${PRIORITY}"', "extras": {"client::display": {"contentType": "text/markdown"}}}' -H 'Content-Type: application/json' "$URL" + # Run by cron + if [ "$notif" == "gotify" ];then + send_gotify_notification + elif [ "$notif" == "pushover" ];then + send_pushover_notification "A new version of Pi-hole is available" "$msg_html
$infos
Please run pihole -up on $host to update" + #pushover -a "bash" -m "A new version of Pi-hole is available:
$msg_html
$infos
Please run pihole -up on $host to update" -p 2 -f 1 + fi fi else