-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcloudflare_ddns.sh
executable file
·182 lines (173 loc) · 5.8 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#!/bin/sh
#
# CloudFlare Dynamic DNS
# https://github.com/renfei/cloudflare-ddns-shell
#
# Updates CloudFlare records with the current public IP address
#
# Takes the same basic arguments as A/CNAME updates in the CloudFlare v4 API
# https://api.cloudflare.com/#dns-records-for-a-zone-update-dns-record
#
# Use with cron jobs etc.
#
# e.g.
#
# manually run:
# cloudflare_ddns.sh -key 404613183ab3971a2118ae5bf03d63e032f9e -zone renfei.net -name extra
#
# cronjob entry to run every 5 minutes:
# */5 * * * * /path/to/cloudflare_ddns.sh -key 404613183ab3971a2118ae5bf03d63e032f9e -zone renfei.net -name extra >> /path/to/cloudflare_ddns.log
#
# will both set the type A DNS record for extra.renfei.net to the current public IP address for user test@renfei.net with the provided API key
#
# #############################################################
#
# CloudFlare 动态 DNS
# https://github.com/renfei/cloudflare-ddns-shell
#
# 使用当前公共 IP 地址更新 CloudFlare 记录
#
# 采用与 CloudFlare v4 API 中的 A/CNAME 更新相同的基本参数
# https://api.cloudflare.com/#dns-records-for-a-zone-update-dns-record
#
# 与 cron 作业等一起使用。
#
# 例如
#
# 手动运行:
# cloudflare_ddns.sh -key 404613183ab3971a2118ae5bf03d63e032f9e -zone renfei.net -name extra
#
# cronjob 定时任务每 5 分钟运行一次:
# */5 * * * * /path/to/cloudflare_ddns.sh -key 404613183ab3971a2118ae5bf03d63e032f9e -zone renfei.net -name extra >> /path/to/cloudflare_ddns.log
#
# 使用提供的 API 密钥将 extra.renfei.net 的 DNS A记录设置为当前公网 IP 地址
echo "CloudFlare Dynamic DNS Start"
echo "Datetime: "$(date)
echo "=========="
key=
zone=
zone_id=
type=A
rec_id=
name=
content=
ttl=60
proxied=false
while [ "$1" != "" ]; do
case $1 in
-key ) shift
key=$1
;;
-zone ) shift
zone=$1
;;
-zone_id ) shift
zone_id=$1
;;
-type ) shift
type=$1
;;
-rec_id ) shift
rec_id=$1
;;
-name ) shift
name=$1
;;
-content ) shift
content=$1
;;
-ttl ) shift
ttl=$1
;;
-proxied ) shift
proxied=$1
;;
* ) echo "unknown parameter $1"
exit 1
esac
shift
done
if [ "$content" = "" ]
then
content=`curl -s http://ip.renfei.net -H "Accept: text"`
if [ "$content" = "" ]
then
date
echo "No IP address to set record value with. 没有可用于设置记录值的 IP 地址。"
exit 1
fi
if [[ $content =~ ":" ]]
then
content=`curl -s http://ipv4.renfei.net -H "Accept: text"`
if [ "$content" = "" ]
then
date
echo "No IP address to set record value with. 没有可用于设置记录值的 IP 地址。"
exit 1
fi
fi
fi
echo "IP Addr: "$content
echo "=========="
if [ "$name" = "" ]
then
echo "You must provide the name of the record you wish to change. 您必须提供要更改的记录的名称。"
exit 1
fi
if [ "$zone" = "" ]
then
echo "You must provide the domain you wish to change. 您必须提供要更改的域名。"
exit 1
fi
if [ "$name" = "$zone" ]
then
hostname="$name"
else
hostname="$name.$zone"
fi
if [ "$key" = "" ]
then
echo "You must provide your user API token. 您必须提供您的用户 API 令牌。"
exit 1
fi
# Get the zone id for the entry we're trying to change if it's not provided
if [ "$zone_id" = "" ]
then
echo "GET: https://api.cloudflare.com/client/v4/zones?name=$zone"
zone_response_json=`curl -X GET "https://api.cloudflare.com/client/v4/zones?name=$zone" -H "Authorization: Bearer $key" -H "Content-Type: application/json"`
# echo "zone_response_json: "$zone_response_json
echo "=========="
zone_id=`echo $zone_response_json | sed -E "s/.+\"result\":\[\{\"id\":\"([a-f0-9]+)\"[^\}]+$zone.+/\1/g"`
if [ "$zone_id" = "" ]
then
echo "Cloudflare DNS Zone id could not be found, please make sure it exists. 在 Cloudflare 中找不到 DNS Zone ID,请确保它存在。"
exit 1
fi
fi
# Get the record id for the entry we're trying to change if it's not provided
if [ "$rec_id" = "" ]
then
echo "GET: https://api.cloudflare.com/client/v4/zones/$zone_id/dns_records?name=$hostname"
rec_response_json=`curl -X GET "https://api.cloudflare.com/client/v4/zones/$zone_id/dns_records?name=$hostname" -H "Authorization: Bearer $key" -H "Content-Type: application/json"`
# echo "rec_response_json: "$rec_response_json
echo "=========="
rec_id=`echo $rec_response_json | sed -E "s/.+\"result\":\[\{\"id\":\"([a-f0-9]+)\"[^\}]+\$hostname\",\"type\":\"$type\"[^\}]+.+/\1/g"`
if [ "$rec_id" = "" ]
then
echo "Cloudflare DNS Record id could not be found, please make sure it exists. 在 Cloudflare 中找不到 DNS 记录,请确保它存在。"
exit 1
fi
fi
# Update the DNS record
echo "PUT: https://api.cloudflare.com/client/v4/zones/$zone_id/dns_records/$rec_id"
update_response=`curl -X PUT "https://api.cloudflare.com/client/v4/zones/$zone_id/dns_records/$rec_id" -H "Authorization: Bearer $key" -H "Content-Type: application/json" --data "{\"id\":\"$rec_id\",\"type\":\"$type\",\"name\":\"$hostname\",\"content\":\"$content\",\"ttl\":$ttl,\"proxied\":$proxied}"`
# echo "update_response: "$update_response
echo "=========="
success_val=`echo $update_response | sed -E "s/.+\"success\":(true|false).+/\1/g"`
if [ "$success_val" = "true" ]
then
echo "Record Updated. 记录更新成功。"
else
echo "Record update failed. 记录更新失败。"
exit 1
fi