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
12 .
/usr
/lib
/ddns
/shell_get.sh
20 if [ -z "$service_id" ]
22 echo "ERRROR: You must specify a service id (the section name in the /etc/config/ddns file) to initialize dynamic DNS."
26 #default mode is verbose_mode, but easily turned off with second parameter
33 ###############################################################
34 # Leave this comment here, to clearly document variable names
35 # that are expected/possible
37 # Now use load_all_config_options to load config
38 # options, which is a much more flexible solution.
43 #config_get enabled $service_id enabled
44 #config_get service_name $service_id service_name
45 #config_get update_url $service_id update_url
48 #config_get username $service_id username
49 #config_get password $service_id password
50 #config_get domain $service_id domain
53 #config_get ip_source $service_id ip_source
54 #config_get ip_interface $service_id ip_interface
55 #config_get ip_network $service_id ip_network
56 #config_get ip_url $service_id ip_url
58 #config_get force_interval $service_id force_interval
59 #config_get force_unit $service_id force_unit
61 #config_get check_interval $service_id check_interval
62 #config_get check_unit $service_id check_unit
63 #########################################################
64 load_all_config_options
"ddns" "$service_id"
68 if [ -z "$check_interval" ]
73 if [ -z "$check_unit" ]
79 if [ -z "$force_interval" ]
84 if [ -z "$force_unit" ]
93 retrieve_prog
="/usr/bin/wget --no-check-certificate -O - ";
94 if [ -h "/usr/bin/wget" ]
96 busybox_wget
=$
(ls -l /usr
/bin
/wget |
awk ' { if ($0~/busybox/) { print "BUSYBOX"}} ')
97 if [ -n "$busybox_wget" ]; then
98 retrieve_prog
="shell_get"
102 verbose_echo
"retrieve_prog=\"$retrieve_prog\""
104 service_file
="/usr/lib/ddns/services"
106 ip_regex
="[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}"
112 #determine what update url we're using if the service_name is supplied
113 if [ -n "$service_name" ]
115 #remove any lines not containing data, and then make sure fields are enclosed in double quotes
116 quoted_services
=$
(cat $service_file |
grep "^[\t ]*[^#]" |
awk ' gsub("\x27", "\"") { if ($1~/^[^\"]*$/) $1="\""$1"\"" }; { if ( $NF~/^[^\"]*$/) $NF="\""$NF"\"" }; { print $0 }' )
119 #echo "quoted_services = $quoted_services"
122 for service_line
in $quoted_services
124 #grep out proper parts of data and use echo to remove quotes
125 next_name
=$
(echo $service_line |
grep -o "^[\t ]*\"[^\"]*\"" |
xargs -r -n1 echo)
126 next_url
=$
(echo $service_line |
grep -o "\"[^\"]*\"[\t ]*$" |
xargs -r -n1 echo)
128 if [ "$next_name" = "$service_name" ]
138 verbose_echo
"update_url=$update_url"
142 #if this service isn't enabled then quit
143 if [ "$enabled" != "1" ]
152 #compute update interval in seconds
153 case "$force_unit" in
155 force_interval_seconds
=$
(($force_interval*60*60*24))
158 force_interval_seconds
=$
(($force_interval*60*60))
161 force_interval_seconds
=$
(($force_interval*60))
164 force_interval_seconds
=$force_interval
168 force_interval_seconds
=$
(($force_interval*60*60))
174 #compute check interval in seconds
175 case "$check_unit" in
177 check_interval_seconds
=$
(($check_interval*60*60*24))
180 check_interval_seconds
=$
(($check_interval*60*60))
183 check_interval_seconds
=$
(($check_interval*60))
186 check_interval_seconds
=$check_interval
190 check_interval_seconds
=$check_interval
196 verbose_echo
"force seconds = $force_interval_seconds"
197 verbose_echo
"check seconds = $check_interval_seconds"
199 #kill old process if it exists & set new pid file
200 if [ -d /var
/run
/dynamic_dns
]
202 #if process is already running, stop it
203 if [ -e "/var/run/dynamic_dns/$service_id.pid" ]
205 old_pid
=$
(cat /var
/run
/dynamic_dns
/$service_id.pid
)
206 test_match
=$
(ps |
grep "^[\t ]*$old_pid")
207 verbose_echo
"old process id (if it exists) = \"$test_match\""
208 if [ -n "$test_match" ]
215 #make dir since it doesn't exist
216 mkdir
/var
/run
/dynamic_dns
218 echo $$
> /var
/run
/dynamic_dns
/$service_id.pid
223 #determine when the last update was
224 current_time
=$
(date +%s
)
225 last_update
=$
(( $current_time - (2*$force_interval_seconds) ))
226 if [ -e "/var/run/dynamic_dns/$service_id.update" ]
228 last_update
=$
(cat /var
/run
/dynamic_dns
/$service_id.update
)
230 time_since_update
=$
(($current_time - $last_update))
233 human_time_since_update
=$
(( $time_since_update / ( 60 * 60 ) ))
234 verbose_echo
"time_since_update = $human_time_since_update hours"
239 registered_ip
=$
(echo $
(nslookup "$domain" 2>/dev
/null
) |
grep -o "Name:.*" |
grep -o "$ip_regex")
242 #do update and then loop endlessly, checking ip every check_interval and forcing an updating once every force_interval
246 current_ip
=$
(get_current_ip
)
249 current_time
=$
(date +%s
)
250 time_since_update
=$
(($current_time - $last_update))
253 verbose_echo
"Running IP check..."
254 verbose_echo
"current system ip = $current_ip"
255 verbose_echo
"registered domain ip = $registered_ip"
258 if [ "$current_ip" != "$registered_ip" ] ||
[ $force_interval_seconds -lt $time_since_update ]
260 verbose_echo
"update necessary, performing update ..."
263 final_url
=$update_url
264 for option_var
in $ALL_OPTION_VARIABLES
266 replace_name
=$
(echo "\[$option_var\]" |
tr 'a-z' 'A-Z')
267 replace_value
=$
(eval echo "\$$option_var")
268 final_url
=$
(echo $final_url |
sed s
/"$replace_name"/"$replace_value"/g
)
270 final_url
=$
(echo $final_url |
sed s
/"\[IP\]"/"$current_ip"/g
)
273 verbose_echo
"updating with url=\"$final_url\""
275 #here we actually connect, and perform the update
276 update_output
=$
( $retrieve_prog "$final_url" )
278 verbose_echo
"Update Output:"
279 verbose_echo
"$update_output"
282 #save the time of the update
283 current_time
=$
(date +%s
)
284 last_update
=$current_time
285 time_since_update
='0'
286 registered_ip
=$current_ip
289 verbose_echo
"update complete, time is: $human_time"
291 echo "$last_update" > "/var/run/dynamic_dns/$service_id.update"
294 human_time_since_update
=$
(( $time_since_update / ( 60 * 60 ) ))
295 verbose_echo
"update unnecessary"
296 verbose_echo
"time since last update = $human_time_since_update hours"
297 verbose_echo
"the time is now $human_time"
300 #sleep for 10 minutes, then re-check ip && time since last update
301 sleep $check_interval_seconds
304 #should never get here since we're a daemon, but I'll throw it in anyway