Browse Source

enable to use custom config file, and multiple domains to update at once

master
max.mehl 2 years ago
parent
commit
ddb6cda6df
Signed by: mxmehl GPG Key ID: 2704E4AB371E2E92
  1. 4
      .gitignore
  2. 16
      config.cfg.sample
  3. 110
      dnsupdate.sh

4
.gitignore

@ -1,4 +1,4 @@
config.cfg
old.ip
old*.ip
tor-ips.txt
update.log
update*.log

16
config.cfg.sample

@ -1,13 +1,15 @@
# Snap DynDNS server (ending with update.php)
SERVER=https://yourdyndnsservice.net
SERVER=https://yourdyndnsservice.net/update.php
# Your personal DynDNS domain which has been registered to your
# Snap DynDNS Server provider
DOMAIN=user1.example.com
# Snap DynDNS domain, username and password
DOMAIN[0]=user1.example.com
USER[0]=myuser
PASS[0]=mypass
# Easy DynDNS username and password
USER=myuser
PASS=mypassword
# You can other domains to be updated. Just increase the counter
#DOMAIN[1]=subdomain.user1.example.com
#USER[1]=myuser
#PASS[1]=mypass
# Service which shows own IP in plain text (notice that if you use IPv4
# and IPv6 parallely you will get an IPv6 address in many cases in

110
dnsupdate.sh

@ -26,9 +26,25 @@
cd "$(dirname "$(readlink -f "$0")")"
print_usage() {
echo "dnsupdate.sh [-c configfile]"
}
while getopts c:h OPT; do
case $OPT in
c) CONFIG=$OPTARG;; # type: normal or docker
h) print_usage; exit 0;;
*) echo "Unknown option: -$OPTARG"; print_usage; exit 1;;
esac
done
if [ -z $CONFIG ]; then
CONFIG="config.cfg"
fi
# Test if config.cfg exists and set needed variables
if [ ! -e config.cfg ]; then echo "Missing config.cfg file. Edit and rename config.cfg.sample"; exit 1; fi
source config.cfg
if [ ! -e "$CONFIG" ]; then echo "Missing config.cfg file. Edit and rename config.cfg.sample"; exit 1; fi
source "$CONFIG"
# Test whether necessary applications are installed
function testinst {
@ -41,54 +57,60 @@ function testinst {
testinst curl
testinst wget
# Does the old.ip file exist? If not, fill it with unrealistic input
if [ ! -e old.ip ]; then
echo "0.0.0.0" > old.ip
fi
# Loop over all hosts. $i will be key
for i in "${!USER[@]}"; do
oldip="old-$i.ip"
logfile="update-$i.log"
# Read old IP, gather new IP via IP return service
OLDIP=$(cat old.ip)
NEWIP=$(wget -T $TIMEOUT -q -O - $IPSERV)
# Does the old.ip file exist? If not, fill it with unrealistic input
if [ ! -e "$oldip" ]; then
echo "0.0.0.0" > "$oldip"
fi
# Post current timestamp in logfile
echo "[$(date "+%Y-%m-%d %H:%M:%S")]" > update.log
# Read old IP, gather new IP via IP return service
OLDIP=$(cat "$oldip")
NEWIP=$(wget -T $TIMEOUT -q -O - $IPSERV)
# check whether the IP has changed since the last update
if [ "$OLDIP" == "$NEWIP" ]; then
echo "IP ($OLDIP) did not change. Aborting." >> update.log
exit 0
fi
# Post current timestamp in logfile
echo "[$(date "+%Y-%m-%d %H:%M:%S")]" > "$logfile"
# Check whether IP is on ignore list
if [ $(echo "$IGNOREIP" | grep "$NEWIP") ]; then
echo "Current IP ($NEWIP) is ignored. Aborting without DNS update." >> update.log
exit 0
fi
# check whether the IP has changed since the last update
if [ "$OLDIP" == "$NEWIP" ]; then
echo "IP ($OLDIP) did not change. Aborting." >> "$logfile"
continue
fi
# Check whether user is using a Tor exit node IP
if [ "$CHECKTOR" = true ]; then
# Only download list of exit nodes if tor-ips.txt file isn't existing yet, or if it is older than 7 days
if [ ! -e tor-ips.txt ] || [ $[ $(date +%s) - $(stat -L -c %Y tor-ips.txt) ] -gt "604800" ]; then
# download list, extract plain IPs, move back to file
echo "download list"
wget -O tor-ips.txt $EXITNODES
TORIPS=$(grep "^ExitAddress" tor-ips.txt | cut -d" " -f2)
echo $TORIPS | sed 's/ /\n/g' > tor-ips.txt
# Check whether IP is on ignore list
if [ $(echo "$IGNOREIP" | grep "$NEWIP") ]; then
echo "Current IP ($NEWIP) is ignored. Aborting without DNS update." >> "$logfile"
continue
fi
# Grep current IP in list of exit node IPs
if [ $(grep "^$NEWIP$" tor-ips.txt) ]; then
echo "Current IP ($NEWIP) is ignored because it is a Tor exit IP. Aborting without DNS update." >> update.log
exit 0
# Check whether user is using a Tor exit node IP
if [ "$CHECKTOR" = true ]; then
# Only download list of exit nodes if tor-ips.txt file isn't existing yet, or if it is older than 7 days
if [ ! -e tor-ips.txt ] || [ $[ $(date +%s) - $(stat -L -c %Y tor-ips.txt) ] -gt "604800" ]; then
# download list, extract plain IPs, move back to file
echo "download list"
wget -O tor-ips.txt $EXITNODES
TORIPS=$(grep "^ExitAddress" tor-ips.txt | cut -d" " -f2)
echo $TORIPS | sed 's/ /\n/g' > tor-ips.txt
fi
# Grep current IP in list of exit node IPs
if [ $(grep "^$NEWIP$" tor-ips.txt) ]; then
echo "Current IP ($NEWIP) is ignored because it is a Tor exit IP. Aborting without DNS update." >> "$logfile"
continue
fi
fi
fi
# Run update if old IP and current IP are not equal
if [ ! "$OLDIP" == "$NEWIP" ]; then
echo $NEWIP > old.ip
curl -X POST -d "domain=$DOMAIN&user=$USER&pass=$PASS&ip=$NEWIP" "$SERVER" >> update.log
echo "" >> update.log
echo "Updated IP address from $OLDIP to $NEWIP." >> update.log
fi
# Run update if old IP and current IP are not equal
if [ ! "$OLDIP" == "$NEWIP" ]; then
echo $NEWIP > "$oldip"
curl -X POST -d "domain=${DOMAIN[$i]}&user=${USER[$i]}&pass=${PASS[$i]}&ip=$NEWIP" "$SERVER" >> "$logfile"
echo "" >> "$logfile"
echo "Updated IP address from $OLDIP to $NEWIP." >> "$logfile"
fi
done

Loading…
Cancel
Save