Created
August 19, 2024 08:48
-
-
Save 2sh/f23d8c4da307ce812d8466239fe4d599 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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