-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcloudflare-update-record.sh
executable file
·167 lines (143 loc) · 7.38 KB
/
cloudflare-update-record.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
#!/bin/bash
# CHANGE THESE
auth_email=""
auth_key=""
zone_name=""
record_name=""
zone_name2=""
record_name2=""
zone_name3=""
record_name3=""
zone_name4=""
record_name4=""
zone_name5=""
record_name5=""
# MAYBE CHANGE THESE
ip=$(curl -s http://ipv4.icanhazip.com)
ip_file="ip.txt"
id_file="cloudflare.ids"
id_file2="cloudflare.ids2"
id_file3="cloudflare.ids3"
id_file4="cloudflare.ids4"
id_file5="cloudflare.ids5"
log_file="cloudflare.log"
# LOGGER
log() {
if [ "$1" ]; then
echo -e "[$(date)] - $1" >> $log_file
fi
}
# SCRIPT START
log "Check Initiated"
if [ -f $ip_file ]; then
old_ip=$(cat $ip_file)
if [ $ip == $old_ip ]; then
echo "IP has not changed."
exit 0
fi
fi
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?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
if [ -f $id_file2 ] && [ $(wc -l $id_file2 | cut -d " " -f 1) == 2 ]; then
zone_identifier2=$(head -1 $id_file2)
record_identifier2=$(tail -1 $id_file2)
else
zone_identifier2=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones?name=$zone_name2" -H "X-Auth-Email: $auth_email" -H "X-Auth-Key: $auth_key" -H "Content-Type: application/json" | grep -Po '(?<="id":")[^"]*' | head -1 )
record_identifier2=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones/$zone_identifier2/dns_records?name=$record_name2" -H "X-Auth-Email: $auth_email" -H "X-Auth-Key: $auth_key" -H "Content-Type: application/json" | grep -Po '(?<="id":")[^"]*')
echo "$zone_identifier2" > $id_file2
echo "$record_identifier2" >> $id_file2
fi
if [ -f $id_file3 ] && [ $(wc -l $id_file3 | cut -d " " -f 1) == 2 ]; then
zone_identifier3=$(head -1 $id_file3)
record_identifier3=$(tail -1 $id_file3)
else
zone_identifier3=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones?name=$zone_name3" -H "X-Auth-Email: $auth_email" -H "X-Auth-Key: $auth_key" -H "Content-Type: application/json" | grep -Po '(?<="id":")[^"]*' | head -1 )
record_identifier3=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones/$zone_identifier3/dns_records?name=$record_name3" -H "X-Auth-Email: $auth_email" -H "X-Auth-Key: $auth_key" -H "Content-Type: application/json" | grep -Po '(?<="id":")[^"]*')
echo "$zone_identifier3" > $id_file3
echo "$record_identifier3" >> $id_file3
fi
if [ -f $id_file4 ] && [ $(wc -l $id_file4 | cut -d " " -f 1) == 2 ]; then
zone_identifier4=$(head -1 $id_file4)
record_identifier4=$(tail -1 $id_file4)
else
zone_identifier4=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones?name=$zone_name4" -H "X-Auth-Email: $auth_email" -H "X-Auth-Key: $auth_key" -H "Content-Type: application/json" | grep -Po '(?<="id":")[^"]*' | head -1 )
record_identifier4=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones/$zone_identifier4/dns_records?name=$record_name4" -H "X-Auth-Email: $auth_email" -H "X-Auth-Key: $auth_key" -H "Content-Type: application/json" | grep -Po '(?<="id":")[^"]*')
echo "$zone_identifier4" > $id_file4
echo "$record_identifier4" >> $id_file4
fi
if [ -f $id_file5 ] && [ $(wc -l $id_file5 | cut -d " " -f 1) == 2 ]; then
zone_identifier5=$(head -1 $id_file5)
record_identifier5=$(tail -1 $id_file5)
else
zone_identifier5=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones?name=$zone_name5" -H "X-Auth-Email: $auth_email" -H "X-Auth-Key: $auth_key" -H "Content-Type: application/json" | grep -Po '(?<="id":")[^"]*' | head -1 )
record_identifier5=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones/$zone_identifier5/dns_records?name=$record_name5" -H "X-Auth-Email: $auth_email" -H "X-Auth-Key: $auth_key" -H "Content-Type: application/json" | grep -Po '(?<="id":")[^"]*')
echo "$zone_identifier5" > $id_file5
echo "$record_identifier5" >> $id_file5
fi
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 "{\"id\":\"$zone_identifier\",\"type\":\"A\",\"name\":\"$record_name\",\"content\":\"$ip\",\"proxied\":true}")
update2=$(curl -s -X PUT "https://api.cloudflare.com/client/v4/zones/$zone_identifier2/dns_records/$record_identifier2" -H "X-Auth-Email: $auth_email" -H "X-Auth-Key: $auth_key" -H "Content-Type: application/json" --data "{\"id\":\"$zone_identifier2\",\"type\":\"A\",\"name\":\"$record_name2\",\"content\":\"$ip\",\"proxied\":true}")
update3=$(curl -s -X PUT "https://api.cloudflare.com/client/v4/zones/$zone_identifier3/dns_records/$record_identifier3" -H "X-Auth-Email: $auth_email" -H "X-Auth-Key: $auth_key" -H "Content-Type: application/json" --data "{\"id\":\"$zone_identifier3\",\"type\":\"A\",\"name\":\"$record_name3\",\"content\":\"$ip\",\"proxied\":true}")
update4=$(curl -s -X PUT "https://api.cloudflare.com/client/v4/zones/$zone_identifier4/dns_records/$record_identifier4" -H "X-Auth-Email: $auth_email" -H "X-Auth-Key: $auth_key" -H "Content-Type: application/json" --data "{\"id\":\"$zone_identifier4\",\"type\":\"A\",\"name\":\"$record_name4\",\"content\":\"$ip\",\"proxied\":true}")
update5=$(curl -s -X PUT "https://api.cloudflare.com/client/v4/zones/$zone_identifier5/dns_records/$record_identifier5" -H "X-Auth-Email: $auth_email" -H "X-Auth-Key: $auth_key" -H "Content-Type: application/json" --data "{\"id\":\"$zone_identifier5\",\"type\":\"A\",\"name\":\"$record_name5\",\"content\":\"$ip\",\"proxied\":true}")
if [[ $update == *"\"success\":false"* ]]; then
message="API UPDATE FAILED. DUMPING RESULTS:\n$update"
log "$message"
echo -e "$message"
exit 1
else
message="IP changed to: $ip on $zone_name"
echo "$ip" > $ip_file
log "$message"
echo "$message"
fi
if [[ $update2 == *"\"success\":false"* ]]; then
message="API UPDATE FAILED. DUMPING RESULTS:\n$update2"
log "$message"
echo -e "$message"
exit 1
else
message="IP changed to: $ip on $zone_name2"
echo "$ip" > $ip_file
log "$message"
echo "$message"
fi
if [[ $update3 == *"\"success\":false"* ]]; then
message="API UPDATE FAILED. DUMPING RESULTS:\n$update3"
log "$message"
echo -e "$message"
exit 1
else
message="IP changed to: $ip on $zone_name3"
echo "$ip" > $ip_file
log "$message"
echo "$message"
fi
if [[ $update4 == *"\"success\":false"* ]]; then
message="API UPDATE FAILED. DUMPING RESULTS:\n$update4"
log "$message"
echo -e "$message"
exit 1
else
message="IP changed to: $ip on $zone_name4"
echo "$ip" > $ip_file
log "$message"
echo "$message"
fi
if [[ $update5 == *"\"success\":false"* ]]; then
message="API UPDATE FAILED. DUMPING RESULTS:\n$update5"
log "$message"
echo -e "$message"
exit 1
else
message="IP changed to: $ip on $zone_name5"
echo "$ip" > $ip_file
log "$message"
echo "$message"
fi