A bash script that retrieves your public IP address and updates a domain record (like a subdomain) using DigitalOcean API v2.
This script is an alternative for dyndns and noip tools, with no dependencies, just a bash script that is less than 20 lines.
How it works:
Use it with a cron job for example and you are done.
I have written a detailed step by step tutorial on how to configure and use the script, with a section on how to open ports on your router too, but if you know what you are doing here is the script:
#!/bin/bash
#################### CHANGE THE FOLLOWING VARIABLES ####################
TOKEN="abcdef1234567890"
DOMAIN="yourdomain.info"
RECORD_ID="111222333"
LOG_FILE="/home/youruser/ips.txt"
########################################################################
CURRENT_IPV4="$(dig +short myip.opendns.com @resolver1.opendns.com)"
LAST_IPV4="$(tail -1 $LOG_FILE | awk -F, '{print $2}')"
if [ "$CURRENT_IPV4" = "$LAST_IPV4" ]; then
echo "IP has not changed ($CURRENT_IPV4)"
else
echo "IP has changed: $CURRENT_IPV4"
echo "$(date),$CURRENT_IPV4" >> "$LOG_FILE"
curl -X PUT -H "Content-Type: application/json" -H "Authorization: Bearer $TOKEN" -d '{"data":"'"$CURRENT_IPV4"'"}' "https://api.digitalocean.com/v2/domains/$DOMAIN/records/$RECORD_ID"
fi
by: Salva LabajosOctober 9, 2020Visit site
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
Have you created an Integration, API Wrapper, Service, or other Tool that helps developers build on DigitalOcean? Help users find it by listing it in Community Tools.
This would be perfect for servers if it took the variables as inputs and had some error handling…
Sweet! You just saved me some time! Thanks!