-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnamesilo_ddns.sh
95 lines (76 loc) · 2.51 KB
/
namesilo_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
#!/bin/bash
# change the domain name and api key with yours.
# get api key from https://www.namesilo.com/account_api.php
domain_name="yours.com"
sub_domain="test"
api_key="37871giolknf47170aws1"
# define domain_name and temp xml file
domain_xml="/tmp/namesilo_domain_list.xml"
domain_txt="/tmp/namesilo_last_ip.txt"
log_path="/tmp/namesilo_ip_update.log"
# combine domain name
if [[ $sub_domain != "" ]];then
domain_name="$sub_domain.$domain_name"
fi
#echo $domain_name
# check ip status
# get current ip address
check_ip=http://api.ipify.org
current_ip=$(curl -s $check_ip)
if [[ "$current_ip" = "$(cat $domain_txt)" ]]; then
echo "same public ip and wait for next check"
exit $?
else
echo "public ip changed and start the update"
fi
## define variables
host_type=""
host_name=""
host_ip=""
host_rrid=""
# get dnsListRecords
# https://www.namesilo.com/api_reference.php#dnsListRecords
dns_list_url="https://www.namesilo.com/api/dnsListRecords?version=1&type=xml&key=$api_key&domain=$domain_name"
#echo $dnslist_url
# get xml from namesilo
curl -s "$dns_list_url" > $domain_xml
#cat $domain_name.xml
# split xml file into bash readable format
read_dom ()
{
local IFS=\>
read -d \< ENTITY CONTENT
}
# get domain ip, domain record id from xml
while read_dom; do
if [[ $ENTITY = "record_id" ]]; then
host_rrid="$CONTENT"
elif [[ $ENTITY = "type" ]]; then
host_type=$CONTENT
elif [[ $ENTITY = "host" ]]; then
host_name=$CONTENT
elif [[ $host_type = "A" ]] && [[ $host_name = "$domain_name" ]] && [[ $ENTITY = "value" ]]; then
host_ip="$CONTENT"
echo "$host_rrid $host_type $host_name $host_ip "
break
fi
done < $domain_xml
# get history ip address
history_ip=$host_ip
# echo "$current_ip $history_ip"
# update records
# https://www.namesilo.com/api_reference.php#dnsUpdateRecord
dns_update_url="https://www.namesilo.com/api/dnsUpdateRecord?version=1&type=xml&key=$api_key&domain=$domain_name&rrid=$host_rrid&rrhost=$sub_domain&rrvalue=$current_ip&rrttl=3600"
#echo $dns_update_url
if [[ $current_ip = $history_ip ]]; then
echo $current_ip > $domain_txt
echo "$(date): same domain ip record, no need to update" >> $log_path
elif [[ $(curl -s "$dns_update_url" | grep -c "success") > 0 ]]; then
# echo "domain ip changed and update to $current_ip"
echo $current_ip > $domain_txt
echo "$(date): My public IP changed to $current_ip!" >> $log_path
else
echo "$(date): update failed" >> $log_path
exit 1
fi
exit $?