Skip to content

Instantly share code, notes, and snippets.

@FloKnapp
Forked from Aikhjarto/update_DNS_A_hosteurope.sh
Last active February 28, 2016 13:49
Show Gist options
  • Save FloKnapp/512b128684481829ae5b to your computer and use it in GitHub Desktop.
Save FloKnapp/512b128684481829ae5b to your computer and use it in GitHub Desktop.
DNS A update script for hosteurope
#!/bin/bash
# Script for automatically update a DNS-A entry for domains hosted by http://hosteurope.de
# The purpose of this script is to have a DNS update functionality similar to dyndns, no-ip, or afraid.org.
DOMAIN="domain.de" # Domain name
HOST="subdomain" # Host Name (the subdomain, leave blank to update the main domain)
#NEW_IP="3.2.1.2" # Desired IP address (if not set, external IP will be used)
HOSTEUROPE_kdnummer="123456" # Hosteurope "Kundennmmer"
HOSTEUROPE_passwd="xsecretx" # Hosteurope password (must be urlencoded)
# uncomment first line if you have curl and second line if you have wget
FETCH_BIN="curl -s --url"
#FETCH_BIN="wget -qO-"
# Regular expression for valid IPv4 adresses
REGEX_IS_IP="(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])"
# file to temporary store data
TMP_FILE="/tmp/hosteurope_update_DNS_A.tmp"
# URL-start (use variable since it occurs so often
URL_START="https://kis.hosteurope.de/administration/domainservices/index.php?menu=2&submode=edit&mode=autodns&domain=$DOMAIN&kdnummer=$HOSTEUROPE_kdnummer&passwd=$HOSTEUROPE_passwd"
if [ -z $HOST ]; then
HOSTANDDOMAIN="http://$DOMAIN"
LINESUB=5
else
HOSTANDDOMAIN="$HOST.$DOMAIN"
LINESUB=0
fi
# get list of all DNS entries (more information is stored on the webpage than it's visible). Purge unneeded stuff to save space (important on embedded devices)
$FETCH_BIN "$URL_START" | grep -e "$HOSTANDDOMAIN" -e "hidden" -e "record" > $TMP_FILE
# fine line where information about $HOST starts
START_LINE=$(grep -n "$HOSTANDDOMAIN" $TMP_FILE | cut -f1 -d: )
if [ -z $START_LINE ]; then
logger -s "DNS Update Hosteurope: can't find $HOSTANDDOMAIN as vaild entry in https://kis.hosteurope.de/administration/domainservices/index.php?menu=2&mode=autodns&submode=edit&domain=$DOMAIN"
exit 1
fi
# from $START_LINE search in the next 10 lines for a hostid and eliminate clutter like "value=". The limit of 10 lines is necessary to avoid fetching hostid of wrong host.
HOSTID=$(tail -n +$START_LINE $TMP_FILE | head -n 10 | grep hostid | awk '{ print $4 }' | sed -e 's/value="//' -e 's/"//')
if [ -z $HOSTID ]; then
logger -s "DNS Update Hosteurope: can't fetch HOSTID for host $HOSTANDDOMAIN"
exit 1
fi
# the main host (.$DOMAIN) is slightly different to access
if [ -z $HOST ]; then
START_LINE=$(($START_LINE - $LINESUB))
OLD_IP=$(tail -n +$START_LINE $TMP_FILE | head -n 10 | grep -e "select name=\"record" | awk '{ print $17 }' | sed -e 's/value="//' -e 's/"><br//' | head -n 1 | grep -E "$REGEX_IS_IP" )
else
OLD_IP=$(tail -n +$START_LINE $TMP_FILE | head -n 10 | grep -e "select name=\"record" | awk '{ print $21 }' | sed -e 's/value="//' -e 's/"><br//' | head -n 1 | grep -E "$REGEX_IS_IP" )
fi
# same for getting old IP
if [ -z $OLD_IP ]; then
logger -s "DNS Update Hosteurope: can't fetch OLD_IP for host $HOSTANDDOMAIN"
exit 1
fi
# get IP if not already set by $NEW_IP
if [ -z $NEW_IP ]; then
NEW_IP=$($FETCH_BIN "http://ifconfig.me/ip" | grep -E "$REGEX_IS_IP" )
if [ -z $NEW_IP ]; then
logger -s "DNS Update Hosteurope: can't fetch CURRENT_IP from http://ifconfig.me/ip"
exit 1
fi
fi
# update only if something had changed (hosteurope gets annoyed if you spam updates too frequently)
if [ $OLD_IP != $NEW_IP ]; then
$FETCH_BIN "$URL_START&record=0&pointer=$NEW_IP&submit=Update&truemode=host&hostid=$HOSTID" > /dev/null
else
logger -s "DNS Update Hosteuroe: Nothing to change because of already actual IP Address"
exit 1
fi
# delete temp file
rm $TMP_FILE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment