Last active
April 20, 2021 06:02
-
-
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.
This file contains 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/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 |
This file contains 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
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 |
This file contains 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
{ | |
"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": "*" | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice mate!