forked from wherelse/cloudflare-ddns-script
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cloudflare-ddns.sh
104 lines (92 loc) · 3.76 KB
/
cloudflare-ddns.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
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
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/bin/bash
# CHANGE THESE
auth_email="xxxxxxx@xxxx.com" #你的CloudFlare注册账户邮箱,your cloudflare account email address
auth_key="*****************" #你的cloudflare账户Globel ID ,your cloudflare Globel ID
zone_name="Your main Domain" #你的域名,your root domain address
record_name="Your Full Domain" #完整域名,your full domain address
record_type="AAAA" #A or AAAA,ipv4 或 ipv6解析
ip_index="local" #use "internet" or "local",使用本地方式还是网络方式获取地址
eth_card="eth0" #使用本地方式获取ip绑定的网卡,默认为eth0,仅本地方式有效,the default ethernet card is eth0
ip_file="ip.txt" #保存地址信息,save ip information in the ip.txt
id_file="cloudflare.ids"
log_file="cloudflare.log"
if [ $record_type = "AAAA" ];then
if [ $ip_index = "internet" ];then
ip=$(curl -6 ip.sb)
elif [ $ip_index = "local" ];then
if [ "$user" = "root" ];then
ip=$(ifconfig $eth_card | grep 'inet6'| grep -v '::1'|grep -v 'fe80' | cut -f2 | awk '{ print $2}' | head -1)
else
ip=$(/sbin/ifconfig $eth_card | grep 'inet6'| grep -v '::1'|grep -v 'fe80' | cut -f2 | awk '{ print $2}' | head -1)
fi
else
echo "Error IP index, please input the right type"
exit 0
fi
elif [ $record_type = "A" ];then
if [ $ip_index = "internet" ];then
ip=$(curl -4 ip.sb)
elif [ $ip_index = "local" ];then
if [ "$user" = "root" ];then
ip=$(ifconfig $eth_card | grep 'inet'| grep -v '127.0.0.1' | grep -v 'inet6'|cut -f2 | awk '{ print $2}')
else
ip=$(/sbin/ifconfig $eth_card | grep 'inet'| grep -v '127.0.0.1' | grep -v 'inet6'|cut -f2 | awk '{ print $2}')
fi
else
echo "Error IP index, please input the right type"
exit 0
fi
else
echo "Error DNS type"
exit 0
fi
# 日志 log file
log() {
if [ "$1" ]; then
echo -e "[$(date)] - $1" >> $log_file
fi
}
# SCRIPT START
log "Check Initiated"
#判断ip是否发生变化,check the ip had been changed?
if [ -f $ip_file ]; then
old_ip=$(cat $ip_file)
if [ $ip == $old_ip ]; then
echo "IP has not changed."
exit 0
fi
fi
#获取域名和授权 get the domain and authentic
if [ -f $id_file ] && [ $(wc -l $id_file | cut -d " " -f 1) == 2 ]; then
zone_identifier=$(head -1 $id_file)
record_identifier=$(tail -1 $id_file)
else
zone_identifier=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones?name=$zone_name" \
-H "X-Auth-Email: $auth_email" \
-H "X-Auth-Key: $auth_key" \
-H "Content-Type: application/json" | grep -Po '(?<="id":")[^"]*' | head -1 )
record_identifier=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones/$zone_identifier/dns_records?type=${record_type}&name=$record_name" \
-H "X-Auth-Email: $auth_email" \
-H "X-Auth-Key: $auth_key" \
-H "Content-Type: application/json" | grep -Po '(?<="id":")[^"]*')
echo "$zone_identifier" > $id_file
echo "$record_identifier" >> $id_file
fi
#更新DNS记录 update the dns
update=$(curl -s -X PUT "https://api.cloudflare.com/client/v4/zones/$zone_identifier/dns_records/$record_identifier" \
-H "X-Auth-Email: $auth_email" \
-H "X-Auth-Key: $auth_key" \
-H "Content-Type: application/json" \
--data "{\"type\":\"$record_type\",\"name\":\"$record_name\",\"content\":\"$ip\",\"ttl\":1,\"proxied\":false}")
#反馈更新情况 gave the feedback about the update statues
if [[ $update == *"\"success\":true"* ]]; then
message="IP changed to: $ip"
echo "$ip" > $ip_file
log "$message"
echo "$message"
else
message="API UPDATE FAILED. DUMPING RESULTS:\n$update"
log "$message"
echo -e "$message"
exit 1
fi