1st commit

Usage: Créer une planifiée sur le NAS exécutant toutes les 3 heures le script bash /var/services/homes/bruno/monitor-ip.sh

Envoi un email si l’adresse IP a changé depuis la dernière exécution

Requiert msmtp ou ssmtp.
This commit is contained in:
2024-08-07 17:04:21 +02:00
commit 5d9dc36a56
3 changed files with 92 additions and 0 deletions

7
email_monitor_ip.txt Normal file
View File

@@ -0,0 +1,7 @@
From: router@clicclac.info
To: bruno@clicclac.info
Subject: DS923 IP has changed !
Your content goes here. Lorem ipsum dolor sit amet, consectetur adipisicing.
(Notice the blank space between the subject and the body.)

2
monitor-ip.pref Normal file
View File

@@ -0,0 +1,2 @@
86.235.4.116

83
monitor-ip.sh Normal file
View File

@@ -0,0 +1,83 @@
#!/usr/bin/env bash
#gotify_server="https://gotify.photos-nas.ovh"
host=$(hostname)
#available=false
#msg=
#infos=
MSMTP=$(which msmtp)
SSMTP=$(which ssmtp)
if [ -n $MSMPT ] ; then
MAIL=$MSMTP
elif [ -n $SSMTP ] && [ -z $MAIL ] ; then
MAIL=$SSMTP
else
echo "No msmtp or ssmtp binary found in PATH: $PATH" ; exit 1
fi
dotenv () {
set -a
# shellcheck disable=SC1091
[ -f ".env" ] && . ".env" || echo -e "${red}\nNo .env file found ! No token for gotify.${reset}"
set +a
}
send_notification() {
FromHost=$(hostname)
# User variables:
SendMailFrom="router@clicclac.info"
SendMailTo="router@clicclac.info"
SubjectTag="monitor_ip.sh"
printf "\nEnvoi d'une notification email ...\n"
$MAIL $SendMailTo << __EOF
From: "$FromHost" <$SendMailFrom>
date:$(date -R)
To: <$SendMailTo>
Subject: [$SubjectTag] L IP sur $FromHost a changée !
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Ancienne IP: $old_ip_externe
Nouvelle IP: $ip_externe
__EOF
}
dotenv
old_ip_externe=$(cat monitor-ip.pref)
ip_externe=$(dig @resolver4.opendns.com myip.opendns.com +short)
echo $ip_externe > ip-monitor.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
#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
else
echo "L'IP externe n'a pas changé"
echo "IP externe: $ip_externe"
fi