Last active
February 10, 2018 14:56
-
-
Save songpon/46fc04f0eee2d4dc92bbb88d63961001 to your computer and use it in GitHub Desktop.
Digital Ocean update my dns record
This file contains hidden or 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/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