Skip to content

Instantly share code, notes, and snippets.

@Aikhjarto
Created April 17, 2014 19:33
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Aikhjarto/11006704 to your computer and use it in GitHub Desktop.
Save Aikhjarto/11006704 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="my.domain" # Domain name
HOST="updatetest" # Host Name
#NEW_IP="3.2.1.2" # Desired IP address (if not set, external IP will be used)
HOSTEUROPE_kdnummer="123456" # Hosteurope "Kundennmmer"
HOSTEUROPE_passwd="xgeheimx" # 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=1&submode=edit&mode=autodns&domain=$DOMAIN&kdnummer=$HOSTEUROPE_kdnummer&passwd=$HOSTEUROPE_passwd"
# 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 "$HOST.$DOMAIN" -e "hidden" -e "record" > $TMP_FILE
# fine line where information about $HOST starts
START_LINE=$(grep -n "$HOST.$DOMAIN" $TMP_FILE | cut -f1 -d:)
if [ -z $START_LINE ]; then
logger -s "DNS Update Hosteurope: can't find $HOST.$DOMAIN as vaild entry in https://kis.hosteurope.de/administration/domainservices/index.php?menu=1&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 $HOST.$DOMAIN"
exit 1
fi
# same for getting old IP
OLD_IP=$(tail -n +$START_LINE $TMP_FILE | head -n 10 | grep -e "select name=\"record" | awk '{ print $19 }' | sed -e 's/value="//' -e 's/"><br//' | head -n 1 | grep -E "$REGEX_IS_IP" )
if [ -z $OLD_IP ]; then
logger -s "DNS Update Hosteurope: can't fetch OLD_IP for host $HOST.$DOMAIN"
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
fi
# delete temp file
rm $TMP_FILE
@Aikhjarto
Copy link
Author

@FloKnapp
Copy link

FloKnapp commented Oct 15, 2016

Hi,

i´ve not forgotten you :-).

Unfortunately i had really spare time the last few months so i couldn´t work on the php port. But i sat down yesterday and today to try and find out what is possible... And now i´ve that script nearly finished. I provide you a link to it´s repository as soon as possible. I hope in the next hour ;-).

I experienced some strange things, especially because of your links to create a txt entry. You wrote there the value for "pointer" is "changetxt" but i found out that this should be the ip address... is this a typo or a missing variable what you wanted to write? Anyway... it works now. You can add/modify and/or delete entries. You can even change existing dns record types to other ones (currently A, AAAA and TXT). It should be easy to extend them i.e. to provide CNAME entries. Currently an opportunity where you can edit the main hostname is missing... This is why i´m not done yet... but i think this should be working in the next few minutes...

Aaaaand there it is: https://bitbucket.org/FloKnapp/hosteurope-dns-update

@FloKnapp
Copy link

Hi,

i wanna update you about the last features.

Currently you have:

  • the ability to add CNAME entries (with validation of correct domain format)
  • the ability to add TXT entries (validation on string value)
  • a much smarter cache logic (lasts at least for 1 day; if no changes (i.e. type/value changes) are registered
  • a debug mode to show additional information (currently just for cache hit/miss notification)
  • the ability to detect either you are logged in or not

Future updates were planned for:

  • add support for IPv6 addresses and thereby the AAAA record type
  • updating more than currently one entry at once
  • add option parameter to control the new feature of customizing TTL value
  • add more flexible dom manipulation logic to act more like current powerful javascript methods (i.e. querySelector)
  • add log mechanisms (and/or email notifications) to track any problems or maybe even unexpected behavior
  • continous improvements on code quality and readability

This are the plans for the time being... if you have any suggestions, wishes or improvements let me know about them :-).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment