-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate-dns.sh
executable file
·114 lines (96 loc) · 3.19 KB
/
update-dns.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
#!/bin/bash
# Create a file to store log
LOG_FILE="$(pwd)/update-dns.log"
if ! [ -f $LOG_FILE ]; then
touch $LOG_FILE
fi
# Write the current time to the log file
echo "" >> $LOG_FILE
echo "$(date "+%z %Y-%m-%d %H:%M:%S")" >> $LOG_FILE
# Validate if config file exists
CONFIG_FILE="$(pwd)/config.txt"
if ! [ -f $CONFIG_FILE ]; then
echo "Config file not found" >> $LOG_FILE
echo "Config file not found"
exit 1
fi
# Validate if every parameter is set
source $CONFIG_FILE
if [ -z "${record}" ]; then
echo "Record not set in config file" >> $LOG_FILE
echo "Record not set in config file"
exit 1
fi
if [ -z "${zone_id}" ]; then
echo "Zone ID (zone_id) not set in config file" >> $LOG_FILE
echo "Zone ID (zone_id) not set in config file"
exit 1
fi
if [ -z "${token}" ]; then
echo "Token not set in config file" >> $LOG_FILE
echo "Token not set in config file"
exit 1
fi
if [ -z "${email}" ]; then
echo "Email not set in config file" >> $LOG_FILE
echo "Email not set in config file"
exit 1
fi
if [ "${proxy}" != "true" ] && [ "${proxy}" != "false" ]; then
echo "Record not set in config file" >> $LOG_FILE
echo "Record not set in config file"
exit 1
fi
if [ -z "${ttl}" ]; then
echo "Ttl not set in config file" >> $LOG_FILE
echo "Ttl not set in config file"
exit 1
fi
: '
List of alternative services to get the external IP address
If failed or incorrect, you can try to replace the service with another one
"https://api.ipify.org"
"https://ipinfo.io/ip"
"https://ifconfig.me"
"https://icanhazip.com"
'
# Get the external IPv4 address
ip=$(curl -4 -s -X GET https://api.ipify.org --max-time 10)
if [ -z "$ip" ]; then
echo "Error! Can't get external ip from https://api.ipify.org. Either there's no network connection or the site is down" >>$LOG_FILE
echo "Error! Can't get external ip from https://api.ipify.org. Either there's no network connection or the site is down"
exit 1
fi
# Get record_info
record_info=$(
curl -s -X GET "https://api.cloudflare.com/client/v4/zones/$zone_id/dns_records?type=A&name=$record" \
-H "X-Auth-Email: $email" \
-H "Authorization: Bearer $token" \
-H "Content-Type: application/json"
)
if [ $(echo $record_info | jq -r '.success') = "false" ]; then
echo ${record_info} >> $LOG_FILE
echo ${record_info}
exit 1
fi
# Get record_id
record_id=$(echo $record_info | jq -r '.result[0].id')
# Push dns record to Cloudflare
data="\"name\": \"$record\", \"proxied\": $proxy, \"ttl\": $ttl, \"content\": \"$ip\", \"type\": \"A\""
update_dns_record=$(
curl -s --request PATCH \
--url "https://api.cloudflare.com/client/v4/zones/$zone_id/dns_records/$record_id" \
-H "X-Auth-Email: $email" \
-H "Authorization: Bearer $token" \
-H "Content-Type: application/json" \
--data "{$data}"
)
# Do some logs
if [[ $(echo $update_dns_record | jq -r '.success') = "false" ]]; then
echo ${update_dns_record} >> $LOG_FILE
echo ${update_dns_record}
exit 1
fi
echo "DNS record updated successfully!"
echo "$record DNS Record updated to $ip, ttl: $ttl, proxied: $proxy"
echo "$record DNS Record updated to $ip, ttl: $ttl, proxied: $proxy" >> $LOG_FILE