Skip to content

Instantly share code, notes, and snippets.

@ajorpheus
Forked from FullStackIndie/ip-check.sh
Created December 22, 2022 17:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ajorpheus/0d02d80c6019419c145da88fdbd70da3 to your computer and use it in GitHub Desktop.
Save ajorpheus/0d02d80c6019419c145da88fdbd70da3 to your computer and use it in GitHub Desktop.
Updated Ip-Check
#!/bin/bash
#Variable Declaration - Change These
HOSTED_ZONE_ID="Z0244******"
#test/dummy subdomain to see if my IP has changed
NAME="dynamic-dns.*********.net."
#My websites that need there IP address updated
CRITTER="development.*********.net."
IDENTITY="development.*********.net."
GATEWAY="development.*********.net."
TYPE="A"
#Using 60 for the health check to prevent long DNS caching time
HEALTH_CHECK_TTL=60
#Using 300 for my websites - at most my websites won't work for 5 minutes (browser may cache DNS entries) but AWS ROUTE 53 updates #DNS pretty fast
TTL=300
#Get current IP address
IP=$(curl http://checkip.amazonaws.com/)
#validate IP address (makes sure Route 53 doesn't get updated with a malformed payload)
if [[ ! $IP =~ ^[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}$ ]]; then
exit 1
fi
#get current
aws route53 list-resource-record-sets --hosted-zone-id $HOSTED_ZONE_ID |
jq -r ".ResourceRecordSets[] | select (.Name == \"$NAME\") | select (.Type == \"$TYPE\") | .ResourceRecords[0].Value" > /home/murph/workspace/cron-jobs/current_route53_value
cat /home/murph/workspace/cron-jobs/current_route53_value
#check if IP is different from Route 53
if grep -Fxq "$IP" /home/murph/workspace/cron-jobs/current_route53_value; then
echo "IP Has Not Changed, Exiting"
exit 1
fi
echo "IP Changed, Updating Records"
#prepare route 53 payload
cat > /home/murph/workspace/cron-jobs/route53_changes.json << EOF
{
"Comment":"Updated From DDNS Shell Script",
"Changes":[
{
"Action":"UPSERT",
"ResourceRecordSet":{
"ResourceRecords":[
{
"Value":"$IP"
}
],
"Name":"$NAME",
"Type":"$TYPE",
"TTL":$HEALTH_CHECK_TTL
}
},
{
"Action":"UPSERT",
"ResourceRecordSet":{
"ResourceRecords":[
{
"Value":"$IP"
}
],
"Name":"$CRITTER",
"Type":"$TYPE",
"TTL":$TTL
}
},
{
"Action":"UPSERT",
"ResourceRecordSet":{
"ResourceRecords":[
{
"Value":"$IP"
}
],
"Name":"$IDENTITY",
"Type":"$TYPE",
"TTL":$TTL
}
},
{
"Action":"UPSERT",
"ResourceRecordSet":{
"ResourceRecords":[
{
"Value":"$IP"
}
],
"Name":"$GATEWAY",
"Type":"$TYPE",
"TTL":$TTL
}
}
]
}
EOF
#update records
sudo aws route53 change-resource-record-sets --hosted-zone-id $HOSTED_ZONE_ID --change-batch file:///home/murph/workspace/cron-jobs/route53_changes.json >> /var/log/ip-check.log
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment