DDNS Updater Script


2 posts in this topic Last Reply

Recommended Posts

Execute this script every 5 minutes (custom cron */5 * * * *) through the user scripts Plugin. It opens the DDNS URL only if the IPv4 or IPv6 has changed. By default the IPv4 is obtained through a web service like icanhazip.com and the IPv6 through the servers ethernet interface.

 

Optionally you can obtain the IPv6 from a specific container:

ipv6=$(docker inspect "Nginx-Proxy-Manager-Official" --format='{{range .NetworkSettings.Networks}}{{.GlobalIPv6Address}}{{end}}')

 

Donate? 🤗

 

#!/bin/bash
# #####################################
# Script:      DDNS Updater v0.5
# Description: Opens a DDNS URL to update the IPv4 and/or IPv6.
# Author:      Marc Gutt
# 
# Notes:
# Execute this script every 5 minutes through the user scripts plugin
# custom cron schedule: */5 * * * *
# 
# ######### Settings ##################

# set your ddns domain(s)
domains=(
    "unraid.example.com"
)

# set your DDNS API login
user="username"
pass="password_or_token"

# obtain IPv4 from public service or set a fixed IP like "1.2.3.4"
ipv4="icanhazip.com"

# obtain IPv6 from specific container or set a fixed IP like "1234:1234:1234::1234"
#ipv6=$(docker inspect "Nginx-Proxy-Manager-Official" --format='{{range .NetworkSettings.Networks}}{{.GlobalIPv6Address}}{{end}}')
#ipv6="icanhazip.com"
ipv6=$(hostname -I | egrep -o '[0-9a-z:]+:[0-9a-z:]+' | head -n 1)

# choose your DDNS provider
ddns="duckdns"

# ######### Script ####################
# make script race condition safe
if [[ -d "/tmp/${0///}" ]] || ! mkdir "/tmp/${0///}"; then exit 1; fi; trap 'rmdir "/tmp/${0///}"' EXIT;
# files to store IP addresses
ipv4filename="/tmp/ipv4.${ddns}.ddns"
ipv6filename="/tmp/ipv6.${ddns}.ddns"
ipv4lastcheck="/tmp/ipv4.lastcheck.${ddns}.ddns"
ipv6lastcheck="/tmp/ipv6.lastcheck.${ddns}.ddns"

# obtain public IPv4
if [[ -n "$ipv4" ]] && [[ "$ipv4" != [0-9]* ]]; then
    lastcheck=$(stat -c %Y "$ipv4lastcheck")
    # obtain public IPv4 only every 5 minutes to avoid DDoS'ing the external service
    if [[ $lastcheck -lt $(date -d "-5 minutes" +"%s") ]]; then
        ipv4=$(curl -4 ${ipv4})
        touch "$ipv4lastcheck"
    else
        ipv4=$(cat "$ipv4filename")
    fi
fi
# obtain public IPv6
if [[ -n "$ipv6" ]] && [[ "$ipv6" != [0-9]* ]]; then
    lastcheck=$(stat -c %Y "$ipv6lastcheck")
    # obtain public IPv6 only every 5 minutes to avoid DDoS'ing the external service
    if [[ $lastcheck -lt $(date -d "-5 minutes" +"%s") ]]; then
        ipv6=$(curl -6 ${ipv6})
        touch "$ipv6lastcheck"
    else
        ipv6=$(cat "$ipv6filename")
    fi
fi
# check if DDNS IP update is necessary
if [[ $(cat "$ipv4filename") != "$ipv4" ]] || [[ $(cat "$ipv6filename") != "$ipv6" ]]; then
    # write new IPs to files
    echo "${ipv4}" > "$ipv4filename"
    echo "${ipv6}" > "$ipv6filename"
    # loop through domains
    for domain in "${domains[@]}"; do
        # obtain "foo" from "foo.example.com"
        subdomain=$(echo "$domain" | cut -f1 -d'.' )
        # obtain "example.com" from "foo.example.com"
        tld=${domain#$subdomain.}
        # update IP through DDNS API
        case $ddns in
        "allinkl")
            url="https://${user}:${pass}@dyndns.kasserver.com/?myip=${ipv4}&myip6=${ipv6}"
            ;;
        "duckdns")
            url="https://www.duckdns.org/update?domains=${domain}&token=${pass}&ip=${ipv4}&ipv6=${ipv6}"
            ;;
        esac
        curl $url
        echo $url
    done
fi

 

Link to post
  • mgutt changed the title to DDNS Updater Script

Manual

 

DuckDNS

 

200335325_2021-06-1718_11_43.png.63c2ee7ffc4115b7ca13c3f15108fa79.png

 

Result:

821625457_2021-06-1718_19_20.png.3a73ee85ee738f713be6ec552bcc92e9.png

 

If you want to delete all status files generated through this script, execute this:

rm /tmp/*.ddns

 

Link to post

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.