#!/bin/bash # Verify pihole version (run pihole -v) and send a notfication to Gotify italic="\033[3m" underline="\033[4m" ita_under="\033[3;4m" bgd="\033[1;4;31m" red="\033[1;31m" green="\033[1;32m" 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 if [[ $? -eq 1 ]]; then echo -e "\n${red}No Internet connection !${reset}" echo -e "Exit !" exit 1 fi host=$(hostname) available=false msg_md= msg_html= infos= # Choose the notification notif="pushover" dotenv () { set -a # shellcheck disable=SC1091 [ -f ".env" ] && . ".env" || echo -e "${red}\nNo .env file found ! No token for gotify.${reset}" set +a } dotenv 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" upd=$(pihole -v) #upd=$(cat 'update_pihole.txt') printf " \e[1m%-10s \e[1m%-10s \e[1m%-10s \n" "Pi-hole" "Current" "Last" while IFS= read -r line; do app=$(echo "${line}" | awk '{print $1}') current=$(echo "${line}" | awk '{print $4}') last=$(echo "${line}" | awk '{print $6}' | sed 's/.$//') 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_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_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 # "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 if [ "$choice" == "y" ] || [ "$choice" == "Y" ]; then pihole -up fi else # 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 echo -e "\nNo available update !" fi