Skip to content

Instantly share code, notes, and snippets.

@actuallymentor
Last active December 14, 2023 08:30
Show Gist options
  • Save actuallymentor/feec8f61b4685d358986bfae19f53345 to your computer and use it in GitHub Desktop.
Save actuallymentor/feec8f61b4685d358986bfae19f53345 to your computer and use it in GitHub Desktop.
Namecheap dyndns update script
#/bin/bash
# Declare the variables needed
host= # @ or subdomain
domain= # TLD
ddns_password= # Password as found in advanced dns settings
# get the current ip address, try with another service if this one is down
array_of_ip_services=( "https://icanhazip.com" "http://ipinfo.io/ip" "http://ifconfig.me/ip" "http://ipecho.net/plain" "http://checkip.amazonaws.com" "http://myexternalip.com/raw" "http://ipecho.net/plain" )
# loop through the array of ip services until we have a valid ip address
for ip_service in "${array_of_ip_services[@]}"
do
device_ip=$(curl -s $ip_service)
if [[ $device_ip =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
break
fi
done
# if we still don't have a valid ip address, exit with an error
if ! [[ $device_ip =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
echo "No valid ip address found"
exit 1
fi
# Check the current Ip of the domain, add the host to the dig if the host is not @
if [ "$host" != "@" ]; then
domain_ip=$(dig +short $host.$domain)
else
domain_ip=$(dig +short $domain)
fi
# If there was an issue getting the current ip, or the current ip is equal to the new ip, exit
if [ -z "$domain_ip" ] || [ "$domain_ip" == "$device_ip" ]; then
echo "No change in IP (domain $domain_ip == device $device_ip). No update required"
exit 0
fi
# echo out the action we are taking
echo "Updating $host.$domain to $device_ip from $domain_ip"
# update the dns record
output=$(curl -s "https://dynamicdns.park-your-domain.com/update?host=$host&domain=$domain&password=$ddns_password&ip=$device_ip")
# check the output for success or failure, <ErrCount>0</ErrCount> and <Done>true</Done> both mean success
if [[ $output =~ \<ErrCount\>0\</ErrCount\> ]] && [[ $output =~ \<Done\>true\</Done\> ]]; then
echo "Update successful"
exit 0
else
echo "Update failed"
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment