Dynamic DNS service: Difference between revisions

From WickyWiki
mNo edit summary
Line 17: Line 17:
* http://www.dnsexit.com/Direct.sv?cmd=userIpClients
* http://www.dnsexit.com/Direct.sv?cmd=userIpClients


WAN IP update script using Linux Upstart:
Bash shell script to update:


<syntaxhighlight lang=bash>
<syntaxhighlight lang=bash>
sudo /etc/init/dyndns-ip-update.conf
#!/bin/bash
</syntaxhighlight>
#dyndns update
 
Contents:
 
<syntaxhighlight lang=bash>
 
# upstart script
#
# Copy script:
#   sudo cp $HOME/Scripts/dyndns-ip-update.conf.sh /etc/init/dyndns-ip-update.conf
 
# Test with:
#  sudo stop dyndns-ip-update
#  sudo start dyndns-ip-update
 
# Check log:
#  sudo cat /var/log/upstart/dyndns-ip-update.log
 
author "Wilbert Volkers"
description "Update WAN IP address at DynDNS service www.dnsexit.com"
 
#start when network is up
#does not seem wait for the network so implemented the retries
start on runlevel [2345]
 
stop on runlevel [!023456]


# write stdout to log in /var/log/upstart/dyndns-ip-update.log:
# settings
console log
host="HOSTNAME.linkpc.net"
login="LOGINNAME"
passtoken="***"
storedipfile="/var/tmp/dyndns-ip-update.ip"
forceafterdays=30


pre-start script
# get current IP using checkip.dyndns.com, try again if error
   logger "pre-start for dyndns-ip-update"
tries=4
end script
retrywait=1
i=0
while [ ${i} -lt ${tries} ]; do
   sleep ${retrywait}s;
  currentIP=$( wget --quiet --output-document=- http://checkip.dyndns.com | sed -ne "s/.*[^0-9]\([0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\)[^0-9].*/\1/p" )
  if [ -n "${currentIP}" ]; then break; fi;
  echo try ${i}/${retries} failed, waiting ${retrywait}
  currentIP="unknown"
  i=$((i + 1))
  retrywait=$((retrywait + retrywait))
done;


post-start script
echo current IP = '${currentIP}'
  logger "post-start for dyndns-ip-update"
end script


script
# get previous IP from file
echo "$(date '+%Y-%m-%d %H.%M.%S') dyndns : start ..."
storedIP="unknown"
storedipfiledate=$( stat -c %y ${storedipfile} | cut -d ' ' -f1 )
storedipdays=$(( (`date +%s -d $(date '+%Y%m%d')` - `date +%s -d ${storedipfiledate}`)/86400 ))
if [ -f "${storedipfile}" ] ; then
  #force update if stored IP older then ${forceafterdays} days
  if [ ${storedipdays} -gt ${forceafterdays} ] ; then
    storedIP="old"
  else
    storedIP=$(cat ${storedipfile})
  fi
fi


# settings
echo previous IP = '${storedIP}' (${storedipfiledate})
host="wilbertvolkers.linkpc.net"
login="LOGIN"
passtoken="PASSTOKEN"
storedipfile="/opt/dyndns-ip-update.ip"


# get current IP using checkip.dyndns.com
# update the dyndns IP if needed
# ${retries} retries, every ${retrywait}
if [ "${currentIP}" = "${storedIP}" ] ; then
retries=5
  echo update not needed
retrywait=5s
else
i=0
  updateurl="http://update.dnsexit.com/RemoteUpdate.sv?login=${login}&password=${passtoken}&host=${host}"
while [ ${i} -lt ${retries} ]; do
  response=$( wget --quiet --output-document=- ${updateurl} | tail -1 )
  currentIP=$( wget --quiet --output-document=- http://checkip.dyndns.com | sed -ne "s/.*[^0-9]\([0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\)[^0-9].*/\1/p" )
  echo updated : ${response}
  if [ -n ${currentIP} ]; then break; fi;
  echo ${currentIP} > ${storedipfile}
  i=$((i + 1))
fi
  echo "$(date '+%Y-%m-%d %H.%M.%S') dyndns : get IP, try ${i}/${retries} failed, waiting ${retrywait}"
  sleep ${retrywait};
done;
 
echo "$(date '+%Y-%m-%d %H.%M.%S') dyndns : current  IP = '${currentIP}'"
 
# get previous IP from file
storedIP="unknown"
forceafterdays=3
if [ -f "${storedipfile}" ] ; then
  #force update if stored IP older then ${forceafterdays} days
  storedipfiledate=$( stat -c %y ${storedipfile} | cut -d ' ' -f1 )
  storedipdays=$(( (`date +%s -d $(date '+%Y%m%d')` - `date +%s -d ${storedipfiledate}`)/86400 ))
  if [ ${storedipdays} -gt ${forceafterdays} ] ; then
    storedIP="old"
  else
    storedIP=$(cat ${storedipfile})
  fi
fi
 
echo "$(date '+%Y-%m-%d %H.%M.%S') dyndns : previous IP = '${storedIP}' (${storedipfiledate})"
 
# update the dyndns IP if needed
if [ "${currentIP}" = "${storedIP}" ] ; then
  echo "$(date '+%Y-%m-%d %H.%M.%S') dyndns : update not needed"
else
  updateurl="http://update.dnsexit.com/RemoteUpdate.sv?login=${login}&password=${passtoken}&host=${host}"
  response=$( wget --quiet --output-document=- ${updateurl} | tail -1 )
  echo "$(date '+%Y-%m-%d %H.%M.%S') dyndns : updated : ${response}"
  echo ${currentIP} > ${storedipfile}
fi
 
echo "$(date '+%Y-%m-%d %H.%M.%S') dyndns : done."
 
end script
</syntaxhighlight>
</syntaxhighlight>
Test script with:
<syntaxhighlight lang=bash>
sudo start dyndns-ip-update
</syntaxhighlight>
Check log:
<syntaxhighlight lang=bash>
sudo cat /var/log/upstart/dyndns-ip-update.log | tail -10
</syntaxhighlight>
More information on Linux Upstart:
* http://upstart.ubuntu.com/getting-started.html


== Installation ddclient (dyndns and others) ==
== Installation ddclient (dyndns and others) ==

Revision as of 08:28, 11 May 2018

Dynamic DNS allows you to have a DNS name while your IP address changes frequently. You will need to register for an account with one of the dynamic DNS providers. This service is often provided freely.

More information:

Popular dynamic DNS registration sites:

Installation ipUpdate (dnsexit.com)

The interface uses an (unsecure) http protocol and it is strongly recommended to configure a different password (token) for this service. Logon at http://www.dnsexit.com and go to My Account -> Account profile -> IP Update Password.

Bash shell script to update:

#!/bin/bash
#dyndns update

# settings
host="HOSTNAME.linkpc.net"
login="LOGINNAME"
passtoken="***"
storedipfile="/var/tmp/dyndns-ip-update.ip"
forceafterdays=30

# get current IP using checkip.dyndns.com, try again if error
tries=4
retrywait=1
i=0
while [ ${i} -lt ${tries} ]; do
  sleep ${retrywait}s;
  currentIP=$( wget --quiet --output-document=- http://checkip.dyndns.com | sed -ne "s/.*[^0-9]\([0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\)[^0-9].*/\1/p" )
  if [ -n "${currentIP}" ]; then break; fi;
  echo try ${i}/${retries} failed, waiting ${retrywait}
  currentIP="unknown"
  i=$((i + 1))
  retrywait=$((retrywait + retrywait))
done;

echo current IP = '${currentIP}'

# get previous IP from file
storedIP="unknown"
storedipfiledate=$( stat -c %y ${storedipfile} | cut -d ' ' -f1 )
storedipdays=$(( (`date +%s -d $(date '+%Y%m%d')` - `date +%s -d ${storedipfiledate}`)/86400 ))
if [ -f "${storedipfile}" ] ; then
  #force update if stored IP older then ${forceafterdays} days
  if [ ${storedipdays} -gt ${forceafterdays} ] ; then
    storedIP="old"
  else
    storedIP=$(cat ${storedipfile})
  fi
fi

echo previous IP = '${storedIP}' (${storedipfiledate})

# update the dyndns IP if needed
if [ "${currentIP}" = "${storedIP}" ] ; then
  echo update not needed
else
  updateurl="http://update.dnsexit.com/RemoteUpdate.sv?login=${login}&password=${passtoken}&host=${host}"
  response=$( wget --quiet --output-document=- ${updateurl} | tail -1 )
  echo updated : ${response}
  echo ${currentIP} > ${storedipfile}
fi

Installation ddclient (dyndns and others)

This client updates the DynDNS record automatically.

sudo apt-get install ssh libio-socket-ssl-perl ddclient

Follow the instructions. Additionally configure a longer update interval 'daemon=3600' and use SSL:

sudo gedit /etc/ddclient.conf
protocol=dyndns2
use=web, web=checkip.dyndns.com, web-skip='IP Address'
server=members.dyndns.org
login=**dyndns-login**
password='**dyndns-pwd**'
wilbertvolkers.dyndns.org
daemon=3600
ssl=yes

View ddclient logging:

cat /var/log/syslog* | grep 'ddclient'

Test/force refresh:

sudo ddclient -force

Reconfigure and restart, you can edit the config file instead:

sudo dpkg-reconfigure ddclient
sudo /etc/init.d/ddclient restart

Remarks

Note that when your IP address has changed, the ddcient needs to announce this to your dyndns service. Then it is still possible that the DNS you are using does not give you the new IP address, it may take some time before updates are routed through the DNS network.