Skip to content

Instantly share code, notes, and snippets.

@songpon
Last active February 10, 2018 14:56
Show Gist options
  • Save songpon/46fc04f0eee2d4dc92bbb88d63961001 to your computer and use it in GitHub Desktop.
Save songpon/46fc04f0eee2d4dc92bbb88d63961001 to your computer and use it in GitHub Desktop.
Digital Ocean update my dns record
#!/bin/bash
domain=example.com
token=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
#get record_id from this command
#curl -X GET -H "Content-Type: application/json" -H "Authorization: Bearer $api" "https://api.digitalocean.com/v2/domains/$domain/records"
record_id=88888888
current_ip="$(\
curl -X GET -H "Content-Type: application/json" -H "Authorization: Bearer $token" "https://api.digitalocean.com/v2/domains/$domain/records/$record_id" \
| sed -e 's/[{}]/''/g' | awk -v k="text" '{n=split($0,a,","); for (i=1; i<=n; i++) print a[i]}' | grep -Po '"'"data"'"\s*:\s*"\K([^"]*)' \
)"
external_ip="$(curl diagnostic.opendns.com/myip)"
# compare existing dns record vs current public ip
if [ "$external_ip" != "$current_ip" ] ; then
# update when diff
curl -X PUT -H "Content-Type: application/json" -H "Authorization: Bearer $token" -d '{"data": "'$external_ip'" }' \
"https://api.digitalocean.com/v2/domains/$domain/records/$record_id"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment