Skip to content

Instantly share code, notes, and snippets.

@bittorf
Forked from gbarre/gandi.sh
Last active June 23, 2024 11:45
Show Gist options
  • Select an option

  • Save bittorf/68c8947b48bf5754750a4c0ae73345a5 to your computer and use it in GitHub Desktop.

Select an option

Save bittorf/68c8947b48bf5754750a4c0ae73345a5 to your computer and use it in GitHub Desktop.
Update DNS A record with Gandi's API
#!/bin/sh
# Customize with your data - see: https://api.gandi.net/docs/authentication/
read 2>/dev/null -r APIKEY <APIKEY.txt || APIKEY='...'
read 2>/dev/null -r DOMAIN <DOMAIN.txt || DOMAIN='...'
read 2>/dev/null -r RECORD <RECORD.txt || RECORD='...'
### DO NOT EDIT AFTER THIS LINE
API="https://api.gandi.net/v5/livedns/domains/$DOMAIN"
command -v 'curl' >/dev/null || { echo "[ERROR] - missing 'curl'"; exit 2; }
command -v 'dig' >/dev/null || { echo "[ERROR] - missing 'dig'" ; exit 2; }
command -v 'jq' >/dev/null || { echo "[ERROR] - missing 'jq'" ; exit 2; }
CURRENT_IP="$( curl -4 --silent ifconfig.co/ip )" || exit 1
IPLENGTH="${#CURRENT_IP}"
# check that ifconfig.co give's an IP-address:
test "${IPLENGTH}" -gt 30 || exit 3
NAMESERVER="$( curl --silent --header "Authorization: Apikey ${APIKEY}" "$API/nameservers" | jq --raw-output '.[0] | sub("^.*?_"; "")' )"
LAST_REGISTERED_IP="$( dig +short "$RECORD.$DOMAIN" "@$NAMESERVER" )"
# only update if needed:
[ "$CURRENT_IP" != "$LAST_REGISTERED_IP" ] && \
curl --silent -X PUT --header "Authorization: Apikey $APIKEY" \
--header "Content-Type: application/json" \
--data "{\"rrset_values\": [\"$CURRENT_IP\"], \"rrset_ttl\": 300}" \
"$API/records/$RECORD/A"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment