-
Notifications
You must be signed in to change notification settings - Fork 8
/
CloudFlareDNSUpdate.sh
55 lines (41 loc) · 1.77 KB
/
CloudFlareDNSUpdate.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/bin/bash
#Clear any previous output - ONLY FOR DEV purpose
#clear
#Get the public IP address of the server
myIP=`dig -4 +short myip.opendns.com @resolver1.opendns.com`
#Store the cloudflare zone ID
#Get this information from your cloudflare dashboard
myZoneID=""
#Store the DNS Zone record ID
#Get this information from the cloudflare DNS API: https://api.cloudflare.com/#dns-records-for-a-zone-list-dns-records
myDNSRecord=""
#Store the DNS Zone name
#Get this information from the cloudflare DNS API: https://api.cloudflare.com/#dns-records-for-a-zone-list-dns-records
myDNSName=""
#Your cloudflare email used for login
myEmail=""
#Your cloudflare API key
#You can find a link on your overview page for the API key
myAPIKey=""
jsonResponse=$(curl -s -X PUT https://api.cloudflare.com/client/v4/zones/${myZoneID}/dns_records/${myDNSRecord} -H "Content-Type: application/json" -H "X-Auth-Email: ${myEmail}" -H "X-Auth-Key: ${myAPIKey}" -H "cache-control: no-cache" -d "{\"type\" : \"A\", \"name\" : \"${myDNSName}\", \"content\" : \"${myIP}\" }")
isDNSUpdated=$(echo ${jsonResponse} | jq --raw-output '.success' )
errorMessage=$(echo ${jsonResponse} | jq --raw-output '.errors[].message')
#Check if the error message is empty or null and if it is, try and look for a fallback error message
if [ -z "$errorMessage" ] || [ ! -n "$errorMessage" ]
then
errorMessage=$(echo ${jsonResponse} | jq --raw-output '.errors[].error_chain[].message')
fi
#if the DNS update failed for some reason, send an email
if ! $isDNSUpdated
then
emailTo="John Doe<[email protected]>"
emailFrom="John Doe<[email protected]>"
emailSubject="DNS update FAILED"
emailBody="Record for zone: ${myDNSName} is NOT UPDATED because: $errorMessage"
ssmtp -t << EOF
To: $emailTo
From: $emailFrom
Subject: $emailSubject
$emailBody
EOF
fi