v 1.0 du 23-08-2024

-notification mail
-notification gotify (gotify.maboiteverte.fr)
-aide
This commit is contained in:
2024-08-23 14:03:37 +02:00
parent b0a548c2f5
commit b2ce61dc22
2 changed files with 64 additions and 27 deletions

1
.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
.DS_Store

View File

@@ -1,10 +1,41 @@
#!/usr/bin/env bash #!/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) host=$(hostname)
#available=false
#msg= redbold="\033[1;31m"
#infos= 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) MSMTP=$(which msmtp)
SSMTP=$(which ssmtp) SSMTP=$(which ssmtp)
@@ -17,6 +48,7 @@ else
echo "No msmtp or ssmtp binary found in PATH: $PATH" ; exit 1 echo "No msmtp or ssmtp binary found in PATH: $PATH" ; exit 1
fi fi
dotenv () { dotenv () {
set -a set -a
# shellcheck disable=SC1091 # shellcheck disable=SC1091
@@ -24,15 +56,15 @@ dotenv () {
set +a set +a
} }
send_notification() { send_mail_notification() {
FromHost=$(hostname) FromHost=$(hostname)
# User variables: # User variables:
SendMailFrom="router@clicclac.info" SendMailFrom="router@clicclac.info"
SendMailTo="router@clicclac.info" SendMailTo="router@clicclac.info"
SubjectTag="monitor_ip.sh" SubjectTag="monitor_ip.sh"
printf "\nEnvoi d'une notification email ...\n" printf "\nSending email notification ...\n"
$MAIL $SendMailTo << __EOF $MAIL $SendMailTo << __EOF
From: "$FromHost" <$SendMailFrom> From: "$FromHost" <$SendMailFrom>
@@ -48,36 +80,40 @@ Nouvelle IP: $ip_externe
__EOF __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 dotenv
old_ip_externe=$(cat monitor-ip.pref) old_ip_externe=$(cat monitor-ip.pref)
ip_externe=$(dig @resolver4.opendns.com myip.opendns.com +short) 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
#if [ "$ip_externe" == "$old_ip_externe" ]; then
echo "L'IP externe a changé" echo "L'IP externe a changé"
echo "IP externe: $ip_externe" echo "IP externe: $ip_externe"
#msmtp -d -t < email_monitor_ip.txt if [ $notif == true ]; then
send_notification send_mail_notification
#echo -e "Sending notification to $gotify_server ..." send_gotify_notification
#TITLE="IP has changed on $host" fi
#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
else else
echo "L'IP externe n'a pas changé" echo "L'IP externe n'a pas changé"
echo "IP externe: $ip_externe" echo "IP externe: $ip_externe"
fi fi