-
-
Save Asenar/c0ebc8c2067a0f6742e352820ed8ca43 to your computer and use it in GitHub Desktop.
Update DNS A record with Gandi's API
This file contains 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/sh | |
# Customize with your datas | |
APIKEY="secret_token" | |
DOMAIN="my_domain" | |
RECORD="my_record" | |
# DO NOT EDIT AFTER THIS LINE | |
CURRENTIP=$(curl -s ifconfig.co/ip) | |
IPLENGTH=$(echo -n ${CURRENTIP} | wc -m) | |
# Check that ifconfig.io give me an IP | |
if [ ${IPLENGTH} -gt 30 ] | |
then | |
exit 1 | |
fi | |
# Get gandi's NS for my domain | |
NS=$(curl -s --header "Authorization: Apikey ${APIKEY}" https://api.gandi.net/v5/livedns/domains/${DOMAIN}/nameservers | jq --raw-output '.[0] | sub("^.*?_"; "")') | |
# Get the last IP recorded | |
LASTREGISTEREDIP=$(dig +short ${RECORD}.${DOMAIN} @${NS}) | |
# Update if needed | |
if [ "${CURRENTIP}" != "${LASTREGISTEREDIP}" ] | |
then | |
curl -s -X PUT --header "Authorization: Apikey ${APIKEY}" \ | |
--header "Content-Type: application/json" \ | |
-d "{\"rrset_values\": [\"${CURRENTIP}\"], \"rrset_ttl\": "300"}" \ | |
https://api.gandi.net/v5/livedns/domains/${DOMAIN}/records/${RECORD}/A | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment