Last active
June 1, 2025 14:30
-
-
Save iilei/53383aba5b520d5da1e268b1194eea68 to your computer and use it in GitHub Desktop.
cron job to update dyndns with namecheap
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 | |
# cron job to update dyndns with namecheap | |
set -e | |
# Variables | |
PASSWORD="abc" | |
UPDATE_URL="dynamicdns.park-your-domain.com/update?host=<username>&domain=<domain>&password=<pass>&ip=<ipaddr>" | |
# Array of domain configurations | |
DOMAINS=( | |
"subdomain example.com" | |
) | |
# Get current IP | |
CURRENT_IP=$(curl --silent https://checkip.amazonaws.com) | |
echodate() { | |
echo `date +"%Y-%m-%dT%H:%M:%S%z"`"$(echo -e '\t')"$* | |
} | |
# Function to update DNS record | |
update_dns() { | |
local host=$1 | |
local domain=$2 | |
local current_ip=$3 | |
# Get DNS IP | |
local dns_ip=$(dig +short $host.$domain) | |
# Check if IP has changed | |
if [ "$current_ip" != "$dns_ip" ]; then | |
local update_uri=$(echo $UPDATE_URL | sed -e "s/<pass>/$PASSWORD/g" | sed -e "s/<username>/$host/g" | sed -e "s/<domain>/$domain/g" | sed -e "s/<ipaddr>/$current_ip/g") | |
# Update DNS record | |
curl --silent --output /dev/null $update_uri | |
echodate "Updated DNS record for $host.$domain to $current_ip" | |
else | |
echodate "IP has not changed for $host.$domain" | |
fi | |
} | |
# Loop through the list of domains | |
for domain_config in "${DOMAINS[@]}"; do | |
# Split the domain configuration into host and domain | |
read -r host domain <<< "$domain_config" | |
# Update DNS record for the domain | |
update_dns "$host" "$domain" "$CURRENT_IP" | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment