-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathddns-start
More file actions
91 lines (75 loc) · 2.74 KB
/
Copy pathddns-start
File metadata and controls
91 lines (75 loc) · 2.74 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/bin/sh
API_TOKEN="" # Your API Token
ZONE_ID="" # Your zone id, hex16 string
RECORD_NAME="" # Your DNS record name, e.g. sub.example.com
RECORD_TTL="1" # TTL in seconds (1=auto)
UPDATE_IPv6=false # Set to true if you want to also update IPv6
IP_QUERY_SITE="https://myip.dnsomatic.com/" # Site used to get external DNS if router isn't able to pull it due to double NAT
get_dns_record_ids() {
local record_name=$1
local type=$2
local api_token=$3
local zone_id=$4
RESPONSE="$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones/${zone_id}/dns_records?type=${type}&name=${record_name}" \
-H "Authorization: Bearer ${api_token}" \
-H "Content-Type:application/json")"
echo $RESPONSE | python -c "
import sys, json
data = json.load(sys.stdin)
for record in data['result']:
print (record['id'])"
}
update_dns_record() {
local record_name=$1
local record_id=$2
local type=$3
local ip=$4
local record_ttl=$5
local api_token=$6
local zone_id=$7
curl -s -X PUT "https://api.cloudflare.com/client/v4/zones/${zone_id}/dns_records/${record_id}" \
-H "Authorization: Bearer ${api_token}" \
-H "Content-Type: application/json" \
--data "{\"type\":\"${type}\",\"name\":\"${record_name}\",\"content\":\"${ip}\",\"ttl\":${record_ttl},\"proxied\":false}"
}
RESULT=true
# Update IPv4
#Get IPv4 from Router otherwise from external source
IPv4=${1}
if [ -z "$IPv4" ]; then
IPv4="$(curl -fs4 $IP_QUERY_SITE)"
logger "IP ${IPv4} obtained external source $IP_QUERY_SITE"
else
logger "IP ${IPv4} obtained by router"
fi
A_RECORD_IDS=$(get_dns_record_ids $RECORD_NAME A $API_TOKEN $ZONE_ID)
for A_RECORD_ID in $A_RECORD_IDS; do
RESPONSE="$(update_dns_record $RECORD_NAME $A_RECORD_ID A $IPv4 $RECORD_TTL $API_TOKEN $ZONE_ID)"
echo $RESPONSE | grep '"success":\ *true' >/dev/null
if [ $? -eq 0 ]; then
logger "Updated A record for ${RECORD_NAME} to ${IPv4}"
else
logger "Unable to update A record for ${RECORD_NAME} with ${IPv4}"
RESULT=false
fi
done
if [ "$UPDATE_IPv6" == true ]; then
# Update IPv6
IPv6=$(ip -6 addr list scope global $device | grep -v " fd" | sed -n 's/.*inet6 \([0-9a-f:]\+\).*/\1/p' | head -n 1)
AAAA_RECORD_IDS=$(get_dns_record_ids $RECORD_NAME AAAA $API_TOKEN $ZONE_ID)
for AAAA_RECORD_ID in $AAAA_RECORD_IDS; do
RESPONSE="$(update_dns_record $RECORD_NAME $AAAA_RECORD_ID AAAA $IPv6 $RECORD_TTL $API_TOKEN $ZONE_ID)"
echo $RESPONSE | grep '"success":\ *true' >/dev/null
if [ $? -eq 0 ]; then
logger "Updated AAAA record for ${RECORD_NAME} to ${IPv6}"
else
logger "Unable to update AAAA record for ${RECORD_NAME}"
RESULT=false
fi
done
fi
if [ "$RESULT" == true ]; then
/sbin/ddns_custom_updated 1
else
/sbin/ddns_custom_updated 0
fi