Automatic DDNS update on CentOS8 (for MyDNS.JP only)

Since many articles with the same purpose have already been posted, I have the regret of building a rooftop, but I think I have added some new knowledge and ingenuity, so I will post it as a memo for myself.

It is a CentOS 8 environment.

/root/ddnscheck.sh

ddnscheck.sh


#!/usr/bin/bash
# This script checks and updates ddns connection for this server.

# TARGET
TARGET="myserver.xxx.xx"

# Operating limit in seconds (default 16 hours : 4am to 8pm)
LIMIT=57600
if [ $# -ge 1 ]; then
  let LIMIT=$1
fi

# Sleep seconds between pings
SLEEP=15

# Forced update limit in seconds (1 hour)
UPDATE_LIMIT=3600

# Overall loop counter
I=0
# Updating loop counter
J=0

echo "Going to check ddns for $LIMIT seconds ..." 1>&2

while [ $I -lt $LIMIT ];
do
  echo -n "`date +"%m-%d %H:%M:%S"` " 1>&2
  ping -4 -c1 -w5 $TARGET >> /dev/null
  if [ $? == 0 ]; then
    echo " OK." 1>&2
  else
    echo " Not Connected."
    let J=$UPDATE_LIMIT
  fi
  if [ $J -ge $UPDATE_LIMIT ]; then
    echo " Going to update DDNS ..."
    wget -4 -O - https://mydns******:[email protected]/login.html 1>&2
    wget -6 -O - https://mydns******:[email protected]/login.html 1>&2
    let J=0
  fi
  sleep $SLEEP
  let I=$I+$SLEEP
  let J=$J+$SLEEP
done
echo "Operation finished." 1>&2

/etc/cron.d/ddnscheck

55 3 * * * root /root/ddnscheck.sh &> /var/log/nginx/ddnscheck.log

Explanation

ddnscheck.sh runs for 57600 seconds (16 hours) by default. Every 15 seconds I type ping on my home server (or myself) with his DDNS server name, and if I don't get a response, I think my home server's global address has changed and MyDNS. Hit the JP address notification URL. Even if the address has not changed, notify MyDNS.JP of the address every hour. Start this script with cron daily at 3:55 AM.

ipv4 and ipv6

The -4 and -6 used in ping and wget are switches that force operation on ipv4 or ipv6, respectively. When I accessed the address notification URL of MyDNS.JP with wget without these switches, only the ipv6 address was updated in my environment. That's not the case, so I'm doing the above.

Instead of changing the wget switch, you may change the URL of MyDNS.JP to a dedicated address as follows.

    wget -O - https://mydns******:[email protected]/login.html 1>&2
    wget -O - https://mydns******:[email protected]/login.html 1>&2

reference

It is good to refer to the document of MyDNS.JP.

Recommended Posts

Automatic DDNS update on CentOS8 (for MyDNS.JP only)
Countermeasures for FDclone build failure on CentOS 8
CentOS rbenv update
OpenVPN on CentOS 8
Installing Visual Studio Code on Ubuntu 20.04 (automatic update support)