Skip to content

Instantly share code, notes, and snippets.

@2sh
Created August 19, 2024 08:48
Show Gist options
  • Save 2sh/f23d8c4da307ce812d8466239fe4d599 to your computer and use it in GitHub Desktop.
Save 2sh/f23d8c4da307ce812d8466239fe4d599 to your computer and use it in GitHub Desktop.
#!/bin/bash
set -e
hosts=()
hosts[0]="<host> <domain> <password>"
cache_dir=/var/lib/misc/namecheap-dns-update
mkdir -p "$cache_dir"
current_ip=$(dig -4 +short myip.opendns.com @resolver1.opendns.com)
for host in "${hosts[@]}"
do
host=($host)
cache_file="$cache_dir/${host[0]}.${host[1]}"
if [ -f "$cache_file" ]
then
last_ip=$(<"$cache_file")
else
last_ip="?"
fi
if [ "$last_ip" != "$current_ip" ]
then
response=$(wget -qO- "https://dynamicdns.park-your-domain.com/update?host=${host[0]}&domain=${host[1]}&password=${host[2]}&ip=${current_ip}")
if echo "$response" | grep -q "<ErrCount>0</ErrCount>"
then
echo "Server DNS record updated for ${host[0]}.${host[1]} : ${last_ip} -> ${current_ip}"
echo "$current_ip" > "$cache_file"
else
reason=$(echo "$response" | sed -En "s/.*<Err1>(.+)<\/Err1>.*/\1/p")
echo "Server DNS record update FAILED for ${host[0]}.${host[1]} : ${reason}"
fi
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment