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
@FloKnapp
Copy link

Hi there,

i didnt tried that Script at all, but i think the access to AUTO-DNS Startpage has changed. I´m logged in so i removed the username and password variables and change "$DOMAIN" to my requested Domain Name.

If i call this URL in my Browser it responds with the Error Message "Invalid Mode":
https://kis.hosteurope.de/administration/domainservices/index.php?menu=1&submode=edit&mode=autodns&domain=$DOMAIN

Changing it to...(menu=2)
https://kis.hosteurope.de/administration/domainservices/index.php?menu=2&submode=edit&mode=autodns&domain=$DOMAIN

will gave me access to the requested Site.

I didnt tried the Script at all, but i want to... first i try to correctly understand what its doing at all. Additionally i would say "Thank you" for this Idea and great peace of Code. This is really nice to automatically point your Domains to your own Server. Thank you!

/edit:
Ok, i forked yours to provide you a running one -> https://gist.github.com/FloKnapp/512b128684481829ae5b
Please forgive me, there is some unethic Code (because of the new Design of HostEurope) - it was a fast fix... maybe i try to port it to PHP because there are some useful classes like DOMDocument which makes it a lot easier to access the fields and a lot easier to maintain it PLUS i can easily provide Arguments to prevent adding the Domain Names directly in the code (maybe its an easy going in BASH too but i dont know it... its almost the first time i work with it this way ;-))

@Aikhjarto
Copy link
Author

Hi!

Thanks for the hint with menu=2. Now it works again.

I have chosen bash since it's available on more systems (especially my dd-wrt based router) than php.
However, for another project (where php is available) I will need a more advanced script not only updating A entries but also adding, updating and deleting TXT entries. So the script will get more complex and php might be a better choice. I've researched the corresponding URLs already:

Add a TXT entry
curl -k --url https://kis.hosteurope.de/administration/domainservices/index.php?hostadd=test&record=11&pointeradd=diesisteintest&menu=2&mode=autodns&domain=$DOMAIN&submode=edit&truemode=host&action=add&submit=Neu+anlegen
Change a txt entry
curl -k --url https://kis.hosteurope.de/administration/domainservices/index.php?record=11&pointer=changetxt&submit=Update&menu=2&mode=autodns&domain=$DOMAIN&submode=edit&truemode=host&hostid=123456789
Delete a TXT entry
curl -k --url https://kis.hosteurope.de/administration/domainservices/index.php?record=11&pointer=changetxt&submit=L%F6schen&menu=2&mode=autodns&domain=$DOMAIN&submode=edit&truemode=host&hostid=123456789
curl -k --url https://kis.hosteurope.de/administration/domainservices/index.php?record=11&pointer=changetxt&submit=L%F6schen&menu=2&mode=autodns&domain=$DOMAIN&submode=edit&truemode=host&hostid=123456789&nachfrage=1

How are your plans for a port to php going?

@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