From 6f725e53bc2c06704082fdc0f2141185237fb3df Mon Sep 17 00:00:00 2001 From: Roman Bazalevsky Date: Tue, 8 Sep 2015 15:58:57 +0300 Subject: [PATCH 1/1] Release --- ddns | 80 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ ddns.conf | 24 +++++++++++++++++ 2 files changed, 104 insertions(+) create mode 100755 ddns create mode 100644 ddns.conf diff --git a/ddns b/ddns new file mode 100755 index 0000000..b3f6736 --- /dev/null +++ b/ddns @@ -0,0 +1,80 @@ +#!/bin/bash + +. /etc/ddns/ddns.conf + +IFS=, HOSTS=($HOSTNAME) +MAINHOST=$HOSTS + +if [ -z "$SLEEP" ] ; then + $SLEEP=60 +fi + +while : + +do + +echo "Sleeping..." +sleep $SLEEP + +new_ip_address=$(curl -4 $URL | sed 's/[ \t\n]*$//') + +result=$(whois $new_ip_address) + +if [ $? = 0 ] +then + +# Ignoring, if after mobile NAT +cnt=`echo "$result" | grep descr | grep -i 'beeline\|mts\|megafon' | wc -l` + +if [ $cnt -gt 0 ] +then + echo "Mobile NAT detected, backup channel, no action taken" + continue +fi + +if [ -z "$new_ip_address" ] ; then + echo "New IP unknown" + continue +fi + +if [ -z "$old_ip_address" ] ; then + old_ip_address=$(host -W 1 -R 4 -4 -t A $MAINHOST $SERVER | grep "has address" | awk '{print $4}') + old_ip_address=$(echo $old_ip_address) + if [ -z "$old_ip_address" ] ; then + echo "Old IP unknown" + continue + fi +fi + +echo "[$old_ip_address],[$new_ip_address]" + +if [ "$old_ip_address" = "$new_ip_address" ] ; then + + echo "IP not changed, no action" + +else + + for host in ${HOSTS[@]}; do + nsupdate -v -k $KEYFILE << EOF +server $SERVER +zone $ZONE +update delete $host A +update add $host $TTL A $new_ip_address +send +EOF + + echo "DNS update for $host" + done + + unset old_ip_address + + if [ ! -z "$EXEC" ] + then + $EXEC + fi + +fi + +fi + +done diff --git a/ddns.conf b/ddns.conf new file mode 100644 index 0000000..fade014 --- /dev/null +++ b/ddns.conf @@ -0,0 +1,24 @@ +# URL, возвращающий IP-адрес клиента +URL="http://my.server/ip.php" + +# TTL для записи в DNS +TTL=120 + +# DNS-сервер +SERVER=my.server + +# Имена хостов, первый - "главный", по нему проверяется адрес +HOSTNAME=my.host.,another.my.host.,yet-another.my.host. + +# Зона DNS +ZONE=my.host + +# Ключ для nsupdate +KEYFILE=/etc/ddns/my.host.private + +# Пауза между обновлениями +SLEEP=30 + +# Скрипты, вызываемый при обнаружении смены адресов (рестарт сервисов и т.п.) +EXEC=/usr/local/bin/restart-ip + -- 2.34.1