Skip to content

Instantly share code, notes, and snippets.

@remimikalsen
Last active May 24, 2024 15:52
Show Gist options
  • Save remimikalsen/f458db9bed5adcf5664b1ed6b46ff989 to your computer and use it in GitHub Desktop.
Save remimikalsen/f458db9bed5adcf5664b1ed6b46ff989 to your computer and use it in GitHub Desktop.
Dynamic DNS with Domeneshop
#/bin/bash
# https://theawesomegarage.com/blog/godaddy-terminated-my-free-dns-management-api-now-what
# This script will retrieve your Public IP, and if the service IP is different from last time
# the script will attempt to call Domeneshop's specific API endpoint for DNS updates.
# I suggest calling the script from crontab like this, for example every minute:
# */1 * * * * /path/to/updatedomeneshop.sh A @ example.com >> /var/log/dns-updates.log
# Set A record and domain to values specified by user
TYPE=$1 # name of A record to update
NAME=$2 # name of A record to update
DOMAIN=$3 # name of domain to update
record_cache=/tmp/.domeneshopDNS.$TYPE.$NAME.$DOMAIN.addr
[ -e $record_cache ] && old_record_ip=`cat $record_cache`
# Get token and secret here: https://domene.shop/admin?view=api
token="*******"
secret="*********"
# A=IPv4 or AAAA=IPv6
if [ $TYPE = "AAAA" ]; then
public_ip=$(ip -6 addr list scope global $device | grep -v " fd" | sed -n 's/.*inet6 \([0-9a-f:]\+\).*/\1/p' | head -n 1)
else
public_cache=/tmp/.myPublicIP
[ -e $public_cache ] && public_ip=`cat $public_cache`
# Check if the IPv4 cached public ip address matches the IPv4 pattern
if ! [[ $public_ip =~ ^([0-9]{1,3}\.){3}[0-9]{1,3}$ ]]; then
echo "$TYPE.$ZONE_ID.$NAME: $(date): Invalid Public IP Cache"
exit 1
fi
fi
# Re-validate that we got an IPv4 or IPv6 address
if [ -z "$public_ip" ]; then
echo "$TYPE.$ZONE_ID.$NAME: $(date): Empty IP. Exiting."
exit 1
fi
# Check cache ip, if matched, program exit
if [ "$old_record_ip" = "$public_ip" ]; then
#echo "$TYPE.$ZONE_ID.$NAME: $(date): Still $public_ip. Exiting."
exit
else
echo "$TYPE.$ZONE_ID.$NAME: $(date): New IP $public_ip. Updating..."
nresult=$(curl -o /dev/null -s -w "%{http_code}" "https://$token:$secret@api.domeneshop.no/v0/dyndns/update?hostname=$DOMAIN&myip=$public_ip")
if [ "$nresult" = "204" ]; then
echo $NAME"."$DOMAIN": $(date): $TYPE: OK $nresult"
echo $public_ip > $record_cache
else
echo $NAME"."$DOMAIN": $(date): $TYPE: NOT OK $nresult"
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment