From b2ce61dc221bacd459c26c8261c56b21368addd7 Mon Sep 17 00:00:00 2001 From: Bruno21 Date: Fri, 23 Aug 2024 14:03:37 +0200 Subject: [PATCH] v 1.0 du 23-08-2024 -notification mail -notification gotify (gotify.maboiteverte.fr) -aide --- .gitignore | 1 + monitor-ip.sh | 90 +++++++++++++++++++++++++++++++++++---------------- 2 files changed, 64 insertions(+), 27 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e43b0f9 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.DS_Store diff --git a/monitor-ip.sh b/monitor-ip.sh index d594ff7..158271f 100644 --- a/monitor-ip.sh +++ b/monitor-ip.sh @@ -1,10 +1,41 @@ #!/usr/bin/env bash -#gotify_server="https://gotify.photos-nas.ovh" +# monitor-ip.sh checks whether the external IP address has changed, and if so, sends a mail notification and Gotify. + host=$(hostname) -#available=false -#msg= -#infos= + +redbold="\033[1;31m" +greenbold="\033[1;32m" +green="\033[0;32m" +yellow="\033[0;33m" +yellowbold="\033[1;33m" +bold="\033[1m" +#bold_under="\033[1;4m" +underline="\033[4m" +reset="\033[0m" + +notif=true + +### Help Function: +Help() { + echo "monitor-ip.sh checks whether the external IP address has changed," + echo " and if so, sends a mail notification and Gotify." + echo + echo "Syntax: monitor-ip.sh [OPTION]" + echo "Example: monitor-ip.sh -h display this help !" + echo + echo "Options:" + echo "-h Print this Help." + echo "-n Just check IP. Don't send notification'." +} + +while getopts "hn" options; do + case "${options}" in + n) notif=false ;; + h|*) Help ; exit 2 ;; + esac +done +shift "$((OPTIND-1))" MSMTP=$(which msmtp) SSMTP=$(which ssmtp) @@ -17,6 +48,7 @@ else echo "No msmtp or ssmtp binary found in PATH: $PATH" ; exit 1 fi + dotenv () { set -a # shellcheck disable=SC1091 @@ -24,15 +56,15 @@ dotenv () { set +a } -send_notification() { -FromHost=$(hostname) +send_mail_notification() { + FromHost=$(hostname) -# User variables: -SendMailFrom="router@clicclac.info" -SendMailTo="router@clicclac.info" -SubjectTag="monitor_ip.sh" + # User variables: + SendMailFrom="router@clicclac.info" + SendMailTo="router@clicclac.info" + SubjectTag="monitor_ip.sh" -printf "\nEnvoi d'une notification email ...\n" + printf "\nSending email notification ...\n" $MAIL $SendMailTo << __EOF From: "$FromHost" <$SendMailFrom> @@ -48,36 +80,40 @@ Nouvelle IP: $ip_externe __EOF } +send_gotify_notification() { + now=$(date +"%d-%m-%Y %T") + gotify_server="https://gotify.maboiteverte.fr" + TITLE="IP has changed on $host" + MESSAGE="**L'IP externe a changé:**\n\n - ancienne IP: $old_ip_externe\n - **nouvelle IP: $ip_externe**\n\n [^]: $now\n" + PRIORITY=8 + URL="$gotify_server/message?token=$token_gotify&?format=markdown" + + echo -e "Sending notification to $gotify_server ..." + + curl -L -S -s --data '{"message": "'"${MESSAGE}"'", "title": "'"${TITLE}"'", "priority":'"${PRIORITY}"', "extras": {"client::display": {"contentType": "text/markdown"}}}' -H 'Content-Type: application/json' "$URL" > /dev/null + + [ $? -eq 0 ] && echo -e "${greenbold}Gotify notification sent successfully !${reset}" || echo -e "${redbold}error sending Gotify notification !${reset}" +} dotenv old_ip_externe=$(cat monitor-ip.pref) ip_externe=$(dig @resolver4.opendns.com myip.opendns.com +short) -echo $ip_externe > ip-monitor.pref +echo $ip_externe > monitor-ip.pref if [ "$ip_externe" != "$old_ip_externe" ]; then -#if [ "$ip_externe" == "$old_ip_externe" ]; then echo "L'IP externe a changé" echo "IP externe: $ip_externe" - #msmtp -d -t < email_monitor_ip.txt - send_notification + if [ $notif == true ]; then + send_mail_notification - #echo -e "Sending notification to $gotify_server ..." - #TITLE="IP has changed on $host" - #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" - #URL="$gotify_server/message?token=$token" - - # --output /dev/null - #curl --with-ca-bundle /opt/etc/ssl/cert.pem -L -S -v --data '{"message": "'"${MESSAGE}"'", "title": "'"${TITLE}"'", "priority":'"${PRIORITY}"', "extras": {"client::display": {"contentType": "text/markdown"}}}' -H 'Content-Type: application/json' "$URL" - #curl -k -L -S -v --data '{"message": "'"${MESSAGE}"'", "title": "'"${TITLE}"'", "priority":'"${PRIORITY}"', "extras": {"client::display": {"contentType": "text/markdown"}}}' -H 'Content-Type: application/json' "$URL" - - #wget "https://gotify.photos-nas.ovh/message?token=$token" --ca-certificate=/usr/syno/etc/certificate/_archive/mqkgNA/chain.pem --no-check-certificate --post-data "title=$TITLE&message=$MESSAGE&priority=$PRIORITY" -O /dev/null + send_gotify_notification + fi else echo "L'IP externe n'a pas changé" echo "IP externe: $ip_externe" + fi