dotfiles/devops/bin/renew-cf-dns-record.sh
Klesh Wong 902f75b847 wip
2021-01-15 11:57:38 +08:00

26 lines
693 B
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/sh
if [ "$#" -lt 5 ] ;then
echo "Renew Cloudflare DNS Record"
echo
echo " Usage: $0 <email> <app-key> <zone-id> <record-id> <domain>"
exit
fi
EMAIL=$1
API_KEY=$2
ZONE_ID=$3
RECORD_ID=$4
DOMAIN=$5
IP=$(curl -s https://myip.ipip.net | awk -F'' '{print $2}' | awk -F' ' '{print $1}')
grep -qF "$IP" /tmp/myip && echo "Unchanged since last renewal, do nothing" && exit
curl -X PUT "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/dns_records/$RECORD_ID" \
-H "X-Auth-Email: $EMAIL" \
-H "X-Auth-Key: $API_KEY" \
-H "Content-Type: application/json" \
--data '{"type": "A", "name": "'"$DOMAIN"'", "content": "'"$IP"'"}'
echo "$IP" > /tmp/myip