Just wanted to share a Bash script for dynamic update of DNS addresses at DynDNS registrar.
This scripts require CURL ---> apt-get install curl.
I have also added 2 scripts for Systemd cron style to call the update script on regular intervals. you can ofcourse call the script with a regular CRON if you want.
This script work behind a NAT/Router with internal IP
ddns_update.sh
Bash
#!/bin/bash
################################################################
# Title:.......ddns_update.sh #
# Author:......Prilly #
# Support:.....prilly@speedbox.me #
# Date:........08.10.2015 #
# Description:.Dynamic DNS updater script - DynDns.org #
################################################################
# DNS resolver used for query
# (opendns=208.67.222.222|resolver1.opendns.com, google=8.8.8.8)
dns="resolver1.opendns.com"
log_file="/var/log/ddclient.log"
check_update(){
for host in ${hostlist[@]}; do
if [ ${host} == "@" ]; then
ip_address_current=$(nslookup ${domain} ${dns} | grep Address | grep -v "#" | cut -f2 -d" ")
else
ip_address_current=$(nslookup ${host}.${domain} ${dns} | grep Address | grep -v "#" | cut -f2 -d" ")
fi
ip_diff_check=$(echo ${ip_address_current} | grep -c ${ip_address_new})
if [ ${ip_diff_check} -eq 0 ]; then
echo "$(date "+%d/%m/%y %R - Update old IP address: ${ip_address_current} With new IP address: ${ip_address_new} For domain: ${host}.${domain}")" &>> ${log_file}
curl https://${username}:${password}@members.dyndns.org/nic/update?hostname=${host}.${domain}\&myip=${ip_address_new}\&wildcard=NOCHG\&mx=NOCHG\&backmx=NOCHG &>> ${log_file}
else
break
fi
done
}
ip_address_new=$(dig +short myip.opendns.com @resolver1.opendns.com)
####################################
# Copy and paste this code as many times as you need.
# Change the values in the hostlist for the A records you wish to update,
# the domain to your domain, and the password as given out by Namecheap
#
### domain.tld ###
# hostlist=("@" "www" "beta")
# domain="domain.tld"
# username="########################"
# password="########################"
# check_update
####################################
### domain.tld ###
hostlist=("test")
domain="domain.tld"
username="test"
password="test"
check_update
####################################
exit 0
Display More
SystemD - ddns.service
Code
[Unit]
Description=Dynamic DNS update
[Service]
User=root
ExecStart=/usr/local/etc/ddns_update.sh >/dev/null
[Install]
WantedBy=basic.target
Systemd - ddns.timer
Code
[Unit]
Description=Run Dynamic DNS update every 5 min
[Timer]
OnBootSec=2min
OnUnitActiveSec=5min
Unit=ddns.service
[Install]
WantedBy=timers.target
Please enjoy the best possible solution to update your dynamic domains from behind a router/NAT..
Prilly