Thursday, 8 October 2009

Problem with DynamicDNS and SSH or Sever PART2 - Practice in BASH

Ok, if I am clear with theory, I can start with practice.


  • Domain - that was the easiest part, you can choose between DYNDNS or NO-IP, both are providing the same service domain assigning to dynamic DNS, I`ve chosen , they are providing better service.

  • Second I thought, I need something telling my server to see for domain, with every day changing IP. One way every day assigning the new born address or do some automation script


Of course the script would be the best solution, and for LINUX we have an option BASH scripting, easy and work`s on any Linux distro. I will explain how I`ve come to final script but here is the solution.

First we need to write write it in the /etc/cron.hourly/dyndns (the name of file)



:$ sudo gedit /etc/cron.hourly/dyndns

You can choose nano if you like it.

There are two different versions BASH (I mentioned) or Pyton. Use whichever you want from below, but make sure the file name is dyndns without extension (no .py) because the scripts in cron.hourly won't run if they have an extension;


#!/bin/bash
# Set the user-name and password for the dyndns account
USERNAME=
PASSWORD=
# Set the system being used to either static or dynamic DNS
SYSTEM=dyndns
# Set the hostname for the record to change
DYNHOST=
# Set whether to wildcard the DNS entry, i.e. *.$DYNHOST
WILDCARD=OFF
############################################
## DO NOT EDIT ANYTHING BEYOND THIS POINT ##
############################################
if [ -z "$DYNDNS" ]; then
DYNDNS="$DYNHOST"
fi
if [ -z "$DNSWILD"]; then
DNSWILD="$WILDCARD"
fi
LOOKUP=`host $DYNHOST | awk '{print $4}'`
MYIP=`curl -s http://www.ipchicken.com | awk '/[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*/ {print $1}'`
# Do the work
if [ "$LOOKUP" = "$MYIP" ]; then
echo "No change in DNS entry."
else
echo `lynx -auth=${USERNAME}:${PASSWORD} -source "http://members.dyndns.org:8245/nic/update?system=${SYSTEM}&hostname=${DYNDNS}&myip=${MYIP}&wildcard=${DNSWILD}"`
fi



This version works, but not in all cases, be careful and test when you upgrade or reinstall you Linux distro, it could not work properly.
An remember to set configuration for dyndns.com, so your dynamic IP address is updated properly in time period 60 mins.
Article inspired by http://webupd8.blogspot.com.

No comments:

Post a Comment