Inhaltsverzeichnis:

dynDNS change logging

2023-03-11 / 2023-03-22

Ermittelt die aktuelle öffentlichen IPv4 Adresse und meldet wenn sich diese ändert.
Genutzt wird loopia.se um die IP zu ermitteln.

#!/bin/bash

# Editieren/Erstellen und Ausführbar machen
#   sudo rm /usr/local/bin/dyndns-change-logging.sh
#   sudo nano /usr/local/bin/dyndns-change-logging.sh
#   sudo chmod +x /usr/local/bin/dyndns-change-logging.sh
#
# Minütlicher Job
#       crontab -e
#               */5      *       *       *       *       /usr/local/bin/dyndns-change-logging.sh >> /tmp/dyndns-change-logging.log

#IP von Angeschlossenen Geräten, die nicht der Loopback-Adresse entsprechen
IP_ACTUAL=$(/usr/bin/curl -s dyndns.loopia.rs/checkip/checkip.php | sed 's/^.*: \([^<]*\).*$/\1/');
TIMESTAMP=$(date "+%d.%m.%Y %H:%M:%S");

IP_ACTUAL_FILE='/tmp/dyndns-change-logging.actual';
echo ${IP_ACTUAL} > ${IP_ACTUAL_FILE};
IP_ACTUAL=$(cat ${IP_ACTUAL_FILE});

IP_LAST_FILE='/tmp/dyndns-change-logging.last';
if [ ! -f ${IP_LAST_FILE} ]; then
    echo ${TIMESTAMP};
    echo 'NEW IP:';
    echo ${IP_ACTUAL};
    echo -e "Timestamp: ${TIMESTAMP}\nNEW IP: ${IP_ACTUAL}" | /usr/bin/mail -s "WAN new IP - host restart" ipchange@heister.email;
    echo ${IP_ACTUAL} > ${IP_LAST_FILE};
    exit
fi

IP_LAST=$(cat ${IP_LAST_FILE});

if [[ ${IP_ACTUAL} != ${IP_LAST} ]]; then
    echo ${TIMESTAMP};
    echo 'NEW IP:';
    echo ${IP_ACTUAL};
    echo 'OLD IP:';
    echo ${IP_LAST};
    echo -e "Timestamp: ${TIMESTAMP}\nNEW IP: ${IP_ACTUAL}\nOLD IP: ${IP_LAST}" | /usr/bin/mail -s "WAN new IP" ipchange@heister.email;
    echo ${IP_ACTUAL} > ${IP_LAST_FILE};
fi