Skip to content

Instantly share code, notes, and snippets.

@icetee
Last active February 23, 2017 01:13
Show Gist options
  • Save icetee/85739d374380d15ebfec271f41e8bfbe to your computer and use it in GitHub Desktop.
Save icetee/85739d374380d15ebfec271f41e8bfbe to your computer and use it in GitHub Desktop.
DigitalOcean refresh domain data
#!/bin/bash
# Simple IP address update with DigitalOcean API
# https://gist.github.com/icetee/85739d374380d15ebfec271f41e8bfbe
#
# Run every 15 minutes? Use `crontab -e` and paste this
# */15 * * * * /home/pi/ip.sh >/dev/null 2>&1
# -----------------------------------------------------------------------------
# Variables
API="https://api.digitalocean.com/v2"
ID="__ID__"
TOKEN="__TOKEN__"
DOMAIN="__DOMAIN__"
MDNAME=".iphash"
IP1="https://l2.io/ip"
IP2="https://api.ipify.org"
# Logic
IP=$(curl -s $IP1)
if ! [[ $IP =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
IP=$(curl -s $IP2)
fi
CURR_MD="$(echo $IP | md5sum | cut -d' ' -f1)"
FILE_MD="$(echo | cat $MDNAME | head -1 | cut -d' ' -f1)"
if [ "$CURR_MD" != "$FILE_MD" ]; then
echo $IP | md5sum > "$MDNAME"
DATA='{"id": "'$ID'", "data": "'$IP'"}'
RESPONSE=$(curl -X PUT \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $TOKEN" \
-d "$DATA" \
"$API/domains/$DOMAIN/records/$ID")
else
exit;
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment