Skip to content

Instantly share code, notes, and snippets.

@seakintruth
Last active July 21, 2021 17:31
Show Gist options
  • Save seakintruth/70ae0466ad29eb4d59debd4ae179c6cd to your computer and use it in GitHub Desktop.
Save seakintruth/70ae0466ad29eb4d59debd4ae179c6cd to your computer and use it in GitHub Desktop.
namecheap bash script to submit dynamic dns from several possible public ip address services
#!/usr/bin/env bash
# set log path
LogPath=/data/logs/crontab/dynamic_dns.txt
echo "---------------------------------" >> $LogPath
date -u >> $LogPath
echo "---------------------------------" >> $LogPath
# get this machine's public IP address from a service
# [TODO] currently only icanhazip is working, need to check the syntax of the others
remoteIp=$(curl ifconfig.me)
if test -z "$remoteIp"
then
echo "Public IP ($remoteIp) reported from ifconfig.me" >> $LogPath
else
remoteIp=$(dig +short myip.opendns.com @resolver1.opendns.com)
if test -z "$remoteIp"
then
echo "Public IP ($remoteIp) reported from myip.opendns.com" >> $LogPath
else
remoteIp=$(curl icanhazip.com)
echo "Public IP ($remoteIp) reported from icanhazip.com" >> $LogPath
fi
fi
update_ip () {
# Arguments are in this order
# LogPath, DnsHost, DnsDomain, DnsPassword, remoteIp
LogPath=$1
DnsHost=$2
DnsDomain=$3
DnsPassword=$4
remoteIp=$5
echo "--- Sending update for $DnsHost.$DnsDomain ---" >> $LogPath
# [TODO] It may be safer to send the parameters in the header instead of the URL if supported by namecheap,
# but as it's ssl it may not matter see https://stackoverflow.com/a/38727920
curl "https://dynamicdns.park-your-domain.com/update?host=$DnsHost&domain=$DnsDomain&password=$DnsPassword&ip=$remoteIp" >> $LogPath
echo "" >> $LogPath
}
#########
# domain your-domain.com
#########
DnsDomain=your-domain.com
DnsPassword=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
DnsHost=www
update_ip "$LogPath" "$DnsHost" "$DnsDomain" "$DnsPassword" "$remoteIp"
DnsHost=*
update_ip "$LogPath" "$DnsHost" "$DnsDomain" "$DnsPassword" "$remoteIp"
#########
# domain your-other-domain.com
#########
DnsDomain=your-other-domain.com
DnsPassword=YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY
DnsHost=www
update_ip "$LogPath" "$DnsHost" "$DnsDomain" "$DnsPassword" "$remoteIp"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment