dyndns update via bash

Sometimes it comes handy to update your dynamic IP onĀ  DynDNS or namecheap via bash file.

This is a simple solution on how to do that:

A improved script:

#!/bin/bash
#
# DynDNS for Namecheap
# including check between externel IP and existing DNS entries in the internet
#

# Domain parameters
DOMAIN="DOAINNAME"
SUBDOMAIN="HOSTNAME"
DOMPW="DYNDNSPASSWORD"

# get external IP
GETEXTIP=$(curl -0 --silent http://checkip.dyndns.org | awk '{print $6}' | cut -f 1 -d "<")

# get external IP from DNS
GETDNSIP=$(dig +short $SUBDOMAIN.$DOMAIN @8.8.8.8)

# DEBUG COMMANDS
#echo "EXT: "$GETEXTIP
#echo "DNS: "$GETDNSIP

# Update DNY entry

if [ "$GETDNSIP" != "$GETEXTIP" ]; then
 curl -0 --silent  "http://dynamicdns.park-your-domain.com/update?domain=$DOMAIN&password=$DOMPW&host=$SUBDOMAIN&ip=$GETEXTIP" > /dev/null
fi

The improvements are:

  • no need for a logfile
  • check DNS entries of google DNS Server against the external IP provided by check.dyndns.org

 

 

Original script provided in this blog post:

#!/bin/bash
# Define parameters

DATE=$(date "+%Y%m%d-%H%M") # Define date for logfile entries
GETIP=$(wget -q -O - checkip.dyndns.org | sed -e 's/[^[:digit:]|.]//g') # get external IP address

# namecheap update
USER="home" #subdomain
DOMAIN="external.com" #domainname
PASSWORDNC="test123" #password namcheap

# DynDNS update
USERDD="dduser" # username dyndns
PASSWORDDD="test321" # password dyndns
DOMAINDD="home.external2.com" #FQDN to update

# analyse logfile
LOGFILE="/tmp/extip.log"
GETLOGIP=$(tail -n 1 $LOGFILE | awk -F"-" '{ print $3 }')
test -f $LOGFILE  || echo "no LOGFILE"
touch $LOGFILE

# check current IP vs stired one
if [ "$GETIP" != "" ];
 then
# check IP vs last logged IP
  if [ "$GETIP" != "$GETLOGIP" ];
   then
# Update dyndns servies when IP changes
	echo $DATE-$GETIP >> $LOGFILE
	curl -O http://dynamicdns.park-your-domain.com/update'?'host=$USER'&'domain=$DOMAIN'&'password=$PASSWORD'&'ip=$GETIP 
	curl -O http://$USERDD:$PASSWORDDD@members.dyndns.org/nic/update'?'system=DynDNS'&'hostname=DOMAINDD'&'myip=$GETIP
  fi
fi

This script is based on an other script within this blog <here>.

Leave a Reply

Your email address will not be published. Required fields are marked *