Skip to content

Instantly share code, notes, and snippets.

@scruffydan
Last active April 20, 2021 06:02
Show Gist options
  • Save scruffydan/8469628 to your computer and use it in GitHub Desktop.
Save scruffydan/8469628 to your computer and use it in GitHub Desktop.
Shell script to fetch public ip address and update Amazon Rout53 DNS. Requires cli53 (https://github.com/barnybug/cli53). If run from crontab you might need to include the full path to cli53 in line 31. Type `which cli53` to get full path.
#!/bin/sh
TTL="300"
ZONE="example.com"
HOSTS="@ subdomain0 subdomain1" #subdomains seperated by spaces
LASTIP_CHECK="example.com" #domain to check to se if current IP matches public DNS records
# Use -f to force ip change
if echo "$1" | grep -q "\-f" ; then
FORCE=true
else
FORCE=false
fi
# Get Public DNS A record IP
LASTIP=`dig +short $LASTIP_CHECK`
# Get WAN IP address from OpenDNS
WAN=`dig +short myip.opendns.com @resolver1.opendns.com`
# Check if we need to update the DNS records
echo "The current IP is: $WAN"
if [ "$LASTIP" != "$WAN" ] || [ $FORCE = true ]; then
echo "IP changed from $LASTIP to $WAN"
else
echo "WAN IP has not changed"
echo "Cowardly refusing to proceed!"
exit
fi
for h in $HOSTS
do
echo $h
cli53 rrcreate --replace $ZONE "$h $TTL A $WAN"
done
SHELL=/usr/local/bin/bash
PATH=/etc:/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin
# Order of crontab fields
# minute hour mday month wday command
*/5 * * * * /home/ddns/bin/ddns.sh
{
"Statement": [
{
"Effect": "Allow",
"Action": [
"route53:GetHostedZone",
"route53:ListResourceRecordSets",
"route53:ChangeResourceRecordSets"
],
"Resource": "arn:aws:route53:::hostedzone/<zone id>"
},
{
"Effect": "Allow",
"Action": [
"route53:ListHostedZones",
"route53:ListHostedZonesByName"
],
"Resource": "*"
}
]
}
@johnpili
Copy link

Nice mate!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment