Gather IP address of a computer and push it to an Snap DynDNS Server instance.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

116 lines
3.8 KiB

#!/bin/bash
########################################################################
# Copyright (C) 2016 Max Mehl <mail [at] mehl [dot] mx>
########################################################################
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
########################################################################
#
# Client part of the "Snap DynDNS Server", grabbing public IP address
# and pushing it to the server handling the more complicated API stuff
#
########################################################################
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" ]; 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 {
APP=$1
if [ $(which $APP | wc -l) != 1 ]; then
echo "$APP does not seem to be installed. Aborting."
exit 1
fi
}
testinst curl
testinst wget
# Loop over all hosts. $i will be key
for i in "${!USER[@]}"; do
oldip="old-$i.ip"
logfile="update-$i.log"
# Does the old.ip file exist? If not, fill it with unrealistic input
if [ ! -e "$oldip" ]; then
echo "0.0.0.0" > "$oldip"
fi
# Read old IP, gather new IP via IP return service
OLDIP=$(cat "$oldip")
NEWIP=$(wget -T $TIMEOUT -q -O - $IPSERV)
# Post current timestamp in logfile
echo "[$(date "+%Y-%m-%d %H:%M:%S")]" > "$logfile"
# 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 IP is on ignore list
if [ $(echo "$IGNOREIP" | grep "$NEWIP") ]; then
echo "Current IP ($NEWIP) is ignored. Aborting without DNS update." >> "$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
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
# 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