linux  scripts

  Inhaltsverzeichnis:

WAN-IP change logging

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 nano -n /usr/local/bin/wanip-change-logging.sh
#   sudo chmod +x /usr/local/bin/wanip-change-logging.sh
#
# Minütlicher Job
#       crontab -e
#               */5      *       *       *       *       /usr/local/bin/wanip-change-logging.sh >> /tmp/wanip-change-logging.log

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

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

IP_LAST_FILE='/tmp/wanip-change-logging.last';
IP_HIS_FILE='/tmp/wanip-change-logging.his';
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};
    echo "${TIMESTAMP}: ${IP_ACTUAL}" >> ${IP_HIS_FILE};
    exit
fi

IP_LAST=$(cat ${IP_LAST_FILE});
IP_HIS=$(cat ${IP_HIS_FILE});

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