2 # /usr/lib/dynamic_dns/dynamic_dns_updater.sh
4 # Written by Eric Paul Bishop, Janary 2008
5 # Distributed under the terms of the GNU General Public License (GPL) version 2.0
7 # This script is (loosely) based on the one posted by exobyte in the forums here:
8 # http://forum.openwrt.org/viewtopic.php?id=14040
11 . /usr/lib/ddns/dynamic_dns_functions.sh
15 if [ -z "$service_id" ]
17 echo "ERRROR: You must specify a service id (the section name in the /etc/config/ddns file) to initialize dynamic DNS."
21 #default mode is verbose_mode, but easily turned off with second parameter
28 ###############################################################
29 # Leave this comment here, to clearly document variable names
30 # that are expected/possible
32 # Now use load_all_config_options to load config
33 # options, which is a much more flexible solution.
38 #config_get enabled $service_id enabled
39 #config_get service_name $service_id service_name
40 #config_get update_url $service_id update_url
43 #config_get username $service_id username
44 #config_get password $service_id password
45 #config_get domain $service_id domain
48 #config_get ip_source $service_id ip_source
49 #config_get ip_interface $service_id ip_interface
50 #config_get ip_network $service_id ip_network
51 #config_get ip_url $service_id ip_url
53 #config_get force_interval $service_id force_interval
54 #config_get force_unit $service_id force_unit
56 #config_get check_interval $service_id check_interval
57 #config_get check_unit $service_id check_unit
58 #########################################################
59 load_all_config_options "ddns" "$service_id"
63 if [ -z "$check_interval" ]
68 if [ -z "$check_unit" ]
74 if [ -z "$force_interval" ]
79 if [ -z "$force_unit" ]
88 retrieve_prog="/usr/bin/wget -O - ";
89 service_file="/usr/lib/ddns/services"
91 ip_regex="[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}"
97 #determine what update url we're using if the service_name is supplied
98 if [ -n "$service_name" ]
100 #remove any lines not containing data, and then make sure fields are enclosed in double quotes
101 quoted_services=$(cat $service_file | grep "^[\t ]*[^#]" | awk ' gsub("\x27", "\"") { if ($1~/^[^\"]*$/) $1="\""$1"\"" }; { if ( $NF~/^[^\"]*$/) $NF="\""$NF"\"" }; { print $0 }' )
104 #echo "quoted_services = $quoted_services"
107 for service_line in $quoted_services
109 #grep out proper parts of data and use echo to remove quotes
110 next_name=$(echo $service_line | grep -o "^[\t ]*\"[^\"]*\"" | xargs -r -n1 echo)
111 next_url=$(echo $service_line | grep -o "\"[^\"]*\"[\t ]*$" | xargs -r -n1 echo)
113 if [ "$next_name" = "$service_name" ]
123 verbose_echo "update_url=$update_url"
127 #if this service isn't enabled then quit
128 if [ "$enabled" != "1" ]
137 #compute update interval in seconds
138 case "$force_unit" in
140 force_interval_seconds=$(($force_interval*60*60*24))
143 force_interval_seconds=$(($force_interval*60*60))
146 force_interval_seconds=$(($force_interval*60))
149 force_interval_seconds=$force_interval
153 force_interval_seconds=$(($force_interval*60*60))
159 #compute check interval in seconds
160 case "$check_unit" in
162 check_interval_seconds=$(($check_interval*60*60*24))
165 check_interval_seconds=$(($check_interval*60*60))
168 check_interval_seconds=$(($check_interval*60))
171 check_interval_seconds=$check_interval
175 check_interval_seconds=$check_interval
181 verbose_echo "force seconds = $force_interval_seconds"
182 verbose_echo "check seconds = $check_interval_seconds"
184 #kill old process if it exists & set new pid file
185 if [ -d /var/run/dynamic_dns ]
187 #if process is already running, stop it
188 if [ -e "/var/run/dynamic_dns/$service_id.pid" ]
190 old_pid=$(cat /var/run/dynamic_dns/$service_id.pid)
191 test_match=$(ps | grep "^[\t ]*$old_pid")
192 verbose_echo "old process id (if it exists) = \"$test_match\""
193 if [ -n "$test_match" ]
200 #make dir since it doesn't exist
201 mkdir /var/run/dynamic_dns
203 echo $$ > /var/run/dynamic_dns/$service_id.pid
208 #determine when the last update was
209 current_time=$(monotonic_time)
210 last_update=$(( $current_time - (2*$force_interval_seconds) ))
211 if [ -e "/var/run/dynamic_dns/$service_id.update" ]
213 last_update=$(cat /var/run/dynamic_dns/$service_id.update)
215 time_since_update=$(($current_time - $last_update))
218 human_time_since_update=$(( $time_since_update / ( 60 * 60 ) ))
219 verbose_echo "time_since_update = $human_time_since_update hours"
224 registered_ip=$(echo $(nslookup "$domain" 2>/dev/null) | grep -o "Name:.*" | grep -o "$ip_regex")
227 #do update and then loop endlessly, checking ip every check_interval and forcing an updating once every force_interval
231 current_ip=$(get_current_ip)
234 current_time=$(monotonic_time)
235 time_since_update=$(($current_time - $last_update))
238 verbose_echo "Running IP check..."
239 verbose_echo "current system ip = $current_ip"
240 verbose_echo "registered domain ip = $registered_ip"
243 if [ "$current_ip" != "$registered_ip" ] || [ $force_interval_seconds -lt $time_since_update ]
245 verbose_echo "update necessary, performing update ..."
248 final_url=$update_url
249 for option_var in $ALL_OPTION_VARIABLES
251 if [ "$option_var" != "update_url" ]
253 replace_name=$(echo "\[$option_var\]" | tr 'a-z' 'A-Z')
254 replace_value=$(eval echo "\$$option_var")
255 replace_value=$(echo $replace_value | sed -f /usr/lib/ddns/url_escape.sed)
256 final_url=$(echo $final_url | sed s^"$replace_name"^"$replace_value"^g )
259 final_url=$(echo $final_url | sed s/"\[HTTPAUTH\]"/"$username${password:+:$password}"/g )
260 final_url=$(echo $final_url | sed s/"\[IP\]"/"$current_ip"/g )
263 verbose_echo "updating with url=\"$final_url\""
265 #here we actually connect, and perform the update
266 update_output=$( $retrieve_prog "$final_url" )
268 verbose_echo "Update Output:"
269 verbose_echo "$update_output"
272 #save the time of the update
273 current_time=$(monotonic_time)
274 last_update=$current_time
275 time_since_update='0'
276 registered_ip=$current_ip
279 verbose_echo "update complete, time is: $human_time"
281 echo "$last_update" > "/var/run/dynamic_dns/$service_id.update"
284 human_time_since_update=$(( $time_since_update / ( 60 * 60 ) ))
285 verbose_echo "update unnecessary"
286 verbose_echo "time since last update = $human_time_since_update hours"
287 verbose_echo "the time is now $human_time"
290 #sleep for 10 minutes, then re-check ip && time since last update
291 sleep $check_interval_seconds
294 #should never get here since we're a daemon, but I'll throw it in anyway