Skip to content

Instantly share code, notes, and snippets.

@mbauhardt
Last active December 10, 2015 20:28
Show Gist options
  • Select an option

  • Save mbauhardt/4488617 to your computer and use it in GitHub Desktop.

Select an option

Save mbauhardt/4488617 to your computer and use it in GitHub Desktop.
This shell script is an update client for dtdns.com. The client update the dynamic ip for a given host

Description

dtdnsbash is an update client (https://www.dtdns.com/dtsite/updateclients) which updates your dynamic ip on https://www.dtdns.com/ to a given hostname.

Requirements

Base
  • bash
  • grep
  • awk
Network
  • ifconfig
  • nslookup
  • openbsd-netcat

Usage

The best way to use dtdnsbash is to start the script in a screen session. Simply install screen and execute the following.

> screen -S dtdns

> bash dtdnsbash.sh <interface> <hostname> <update.intervall.in.seconds>

As an example when your external interface is eth0 and your hostname is blabla.blabla.com and your dynamic ip will change every hour you can use this command > bash dtdnsbash.sh eth0 blabla.blabla.com 3600

After start you have to type your password and the update will update every hour. To bring the process in background you can hit

> screen CTRL+a d

When you login again on your server you can re-attach your screen session with

> screen -r dtdns
#!/bin/bash
function assertArgs {
EXPECTED_ARGS=3
if [ $# -ne $EXPECTED_ARGS ]
then
echo "Usage: `basename $0` <interface> <host.name> <dns.update.interval>"
exit 1
fi
}
function readPassword {
read -se password
echo $password
}
function getIpFromInterface {
echo `ifconfig "${1}" | grep 'inet '| awk '{print $2}'`
}
function parseIp {
echo `echo "${1}." | grep -E '([0-9]{1,3}\.){4}'`
}
function assertIp {
if [ ! "$1" ]
then
echo $2
exit 1
fi
}
function lookupIpFromNet {
echo `nslookup "${1}" NS1.DTDNS.COM | grep "${1}" -A1 | grep 'Address' | awk '{print $2}'`
}
function updateDnsEntry {
echo -e "\n
GET /api/autodns.cfm?id=${1}&pw=${2}&client=dtdnsbash&ip=${3}
HTTP/1.1
Host: www.dtdns.com
User-Agent: bash
" | nc www.dtdns.com 80
echo "Done."
}
assertArgs $@
INTERFACE=$1
HOST_NAME=$2
DNS_UPDATE_INTERVAL=$3
echo -n "Please enter dtdns.com account password: "
DTDNS_PASSWORD=$(readPassword)
for (( ; ; ))
do
echo `date`
CURRENT_IP=`getIpFromInterface $INTERFACE`
echo "Lookup IP on interface ${INTERFACE}: ${CURRENT_IP}"
VALID_IP=`parseIp $CURRENT_IP`
assertIp "${VALID_IP}" "Invalid IP ${CURRENT_IP} on interface ${INTERFACE}"
LAST_IP=`lookupIpFromNet $HOST_NAME`
echo "Lookup IP for hostname ${HOST_NAME}: ${LAST_IP}"
VALID_LAST_IP=`parseIp $LAST_IP`
assertIp "${VALID_LAST_IP}" "Invalid IP ${LAST_IP} for host ${HOST_NAME}"
if [ $CURRENT_IP == $LAST_IP ]
then
echo "Your current IP ${CURRENT_IP} still points to ${HOST_NAME}. No dns update necessary."
else
echo "Your IP ${CURRENT_IP} does not point to ${HOST_NAME}/${LAST_IP}. Trigger a dns update."
updateDnsEntry $HOST_NAME $DTDNS_PASSWORD $CURRENT_IP
fi
sleep $DNS_UPDATE_INTERVAL
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment