-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdater.sh
148 lines (116 loc) · 3.07 KB
/
updater.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
#!/bin/sh
# READ 4
# WRITE 2
# EXECUTE 1
path=$(dirname $0)
date=$(date '+%Y-%m-%d')
datetime=$(date '+%Y-%m-%d %H:%M:%S')
base_url="https://api.godaddy.com/v1/domains"
writelog ()
{
logpath=$path/logs
logfile=$logpath/$date.log
# If logdir not found, create one
if [ ! -d $logpath ];
then
mkdir $logpath
chmod 740 $logpath
fi
# Check for log file
if [ ! -s $logfile ];
then
touch $logfile
chmod 640 $logfile
fi
# Write message to log
echo $datetime": $1" >> $logfile
}
exitapp ()
{
writelog "---------------------------------------------------------"
exit $1
}
ip_url="https://api.ipify.org"
writelog "IP Updater started!"
cred_path="$path/apicredentials"
domain_path="$path/domainname"
ip_path="$path/lastip"
changepath="$path/record_changes.log"
apikeys=$(cat $cred_path)
res1=$?
domain=$(cat $domain_path)
res2=$?
if [ ! $res1 -eq 0 ] || [ ! $res2 -eq 0 ]
then
touch $cred_path
touch $domain_path
chmod 600 $cred_path
chmod 600 $domain_path
writelog "Configuration files are missing! Please configure apikey and domain files."
writelog "Put API Key and Secret into credentials file and domain name into domain file."
writelog "Keys format: API_KEY:API_SECRET | Domain format DOMAIN_NAME.SUFFIX."
exitapp 1
elif [ ! -s $cred_path ] || [ ! -s $domain_path ]
then
writelog "Files are missing data! Please configure options properly!"
writelog "Put API Key and Secret into credentials file and domain name into domain file."
writelog "Keys format: API_KEY:API_SECRET | Domain format DOMAIN_NAME.SUFFIX"
exitapp 1
fi
writelog "Getting last ip from file..."
lastip=$(cat $ip_path)
ipres=$?
A_url="$base_url/$domain/records/A/@"
WILD_url="$base_url/$domain/records/A/*"
# Get external ip
writelog "Getting external ip..."
ip=$(curl -X GET $ip_url)
# Trim whitespace
trimmed_ip=$(echo $ip | xargs)
# Check if IP is empty
if [ -z "$trimmed_ip" ];
then
writelog "Error while getting IP, value was empty. Exiting..."
exitapp 1
fi
# Get last ip and check if need to update
if [ ! $ipres -eq 0 ];
then
writelog "Last IP file not found, creating file.."
touch $ip_path
chmod 600 $ip_path
lastip="NULL"
elif [ "$ip" = "$lastip" ];
then
writelog "IP not changed from last, exiting..."
exitapp 0
fi
# Save new IP to file
writelog "IP is changed! Updating record..."
echo $ip > $ip_path
# Log new ip
writelog "Last IP was: $lastip"
writelog "New IP is: $ip"
if [ ! -s $changepath ];
then
writelog "IP change logfile not found. Creating one.."
touch $changepath
chmod 640 $changepath
fi
echo "$datetime: IP changed! FROM $lastip TO $ip" >> $changepath
# Create domain data request
result=[{\"data\":\"$ip\"}]
# Put modified domain IP to API
writelog "Sending PUT data to API..."
curl -X PUT -H "Content-type: application/json" -H "Authorization: sso-key $apikeys" -d $result $A_url
putres1=$?
curl -X PUT -H "Content-type: application/json" -H "Authorization: sso-key $apikeys" -d $result $WILD_url
putres2=$?
if [ ! $putres1 -eq 0 ] || [ ! $putres2 -eq 0 ];
then
writelog "Error in PUT(s)! Send failed!"
exitapp 1
fi
writelog "PUT sent!"
writelog "Operation done"
exitapp 0