forked from fire1ce/DDNS-Cloudflare-Bash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
updateDNS6.sh
61 lines (51 loc) · 2.71 KB
/
updateDNS6.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
56
57
58
59
60
61
#!/usr/bin/env bash
export PATH=/sbin:/opt/bin:/usr/local/bin:/usr/contrib/bin:/bin:/usr/bin:/usr/sbin:/usr/bin/X11
## A bash script to update a Cloudflare DNS AAAA record with the Internal IP of the source machine ##
## DNS record MUST pre-creating on Cloudflare
##### Config Params
what_ip="internal" ##### Which IP should be used for the record: usually internal for ipv6
what_interface="eth0" ##### For internal IP, provide interface name
dns_record="ddns.example.com" ##### DNS A record which will be updated
zoneid="ChangeMe" ##### Cloudflare's Zone ID
proxied="false" ##### Use Cloudflare proxy on dns record true/false
ttl=1 ##### 120-7200 in seconds or 1 for Auto
cloudflare_api_token="ChangeMe" ##### Cloudflare API Token keep it private!!!!
##### Get the current IP addresss
if [ "${what_ip}" == "external" ]; then
ip=$(curl -s -6 -X GET https://icanhazip.com)
else
if [ "${what_ip}" == "internal" ]; then
if which ip >/dev/null; then
ip=$(ip -o -6 addr show ${what_interface} mngtmpaddr | awk '{print $4;}' | grep -P '^(?!fd)[[:alnum:]]{4}:.*/64'|cut -d/ -f 1)
else
ip=$(ifconfig ${what_interface} | grep 'inet6 ' | awk '{print $2}')
fi
else
echo "missing or incorrect what_ip/what_interface parameter"
fi
fi
logger -s -t $(basename $0) -p user.info "==> Current Local IP is $ip"
##### get the dns record id and current ip from cloudflare's api
dns_record_info=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones/$zoneid/dns_records?type=AAAA&name=$dns_record" \
-H "Authorization: Bearer $cloudflare_api_token" \
-H "Content-Type: application/json")
dns_record_id=$(echo ${dns_record_info} | grep -o '"id":"[^"]*' | cut -d'"' -f4)
dns_record_ip=$(echo ${dns_record_info} | grep -o '"content":"[^"]*' | cut -d'"' -f4)
logger -s -t $(basename $0) -p user.info "==> DNS Record on Cloudflare is currently set to $dns_record_ip"
if [ ${dns_record_ip} == ${ip} ]; then
logger -s -t $(basename $0) -p user.info "==> No changes needed!"
exit
else
logger -s -t $(basename $0) -p user.info "==> Updating!!!"
fi
##### updates the dns record
update=$(curl -s -X PUT "https://api.cloudflare.com/client/v4/zones/$zoneid/dns_records/$dns_record_id" \
-H "Authorization: Bearer $cloudflare_api_token" \
-H "Content-Type: application/json" \
--data "{\"type\":\"AAAA\",\"name\":\"$dns_record\",\"content\":\"$ip\",\"ttl\":$ttl,\"proxied\":$proxied}")
if [[ $update == *"\"success\":false"* ]]; then
logger -s -t $(basename $0) -p user.warn "==> FAILED:\n$update"
exit 1
else
logger -s -t $(basename $0) -p user.info "==> $dns_record DNS Record Updated To: $ip"
fi