#!/bin/sh # /usr/lib/ddns/dynamic_dns_updater.sh # #.Distributed under the terms of the GNU General Public License (GPL) version 2.0 # Original written by Eric Paul Bishop, January 2008 # (Loosely) based on the script on the one posted by exobyte in the forums here: # http://forum.openwrt.org/viewtopic.php?id=14040 # extended and partial rewritten #.2014-2018 Christian Schoenebeck # # variables in small chars are read from /etc/config/ddns # variables in big chars are defined inside these scripts as global vars # variables in big chars beginning with "__" are local defined inside functions only # set -vx #script debugger . $(dirname $0)/dynamic_dns_functions.sh # global vars are also defined here usage() { cat << EOF Usage: $MYPROG [options] -- command Commands: start Start SECTION or NETWORK or all stop Stop NETWORK or all Parameters: -n NETWORK Start/Stop sections in background monitoring NETWORK, force VERBOSE=0 -S SECTION SECTION to start use either -N NETWORK or -S SECTION -h show this help and exit -V show version and exit -v LEVEL VERBOSE=LEVEL (default 1) '0' NO output to console '1' output to console '2' output to console AND logfile + run once WITHOUT retry on error -d dry run (don't send any changes) EOF } usage_err() { printf %s\\n "$MYPROG: $@" >&2 usage >&2 exit 1 } while getopts ":hv:dn:S:V" OPT; do case "$OPT" in h) usage; exit 0;; v) VERBOSE=$OPTARG;; d) DRY_RUN=1;; n) NETWORK=$OPTARG;; S) SECTION_ID=$OPTARG;; V) printf %s\\n "ddns-scripts $VERSION"; exit 0;; :) usage_err "option -$OPTARG missing argument";; \?) usage_err "invalid option -$OPTARG";; *) usage_err "unhandled option -$OPT $OPTARG";; esac done shift $((OPTIND - 1 )) # OPTIND is 1 based [ -n "$NETWORK" -a -n "$SECTION_ID" ] && usage_err "use either option '-N' or '-S' not both" [ $# -eq 0 ] && usage_err "missing command" [ $# -gt 1 ] && usage_err "to much commands" case "$1" in start) if [ -n "$NETWORK" ]; then start_daemon_for_all_ddns_sections "$NETWORK" exit 0 fi if [ -z "$SECTION_ID" ]; then start_daemon_for_all_ddns_sections exit 0 fi ;; stop) if [ -n "$INTERFACE" ]; then stop_daemon_for_all_ddns_sections "$NETWORK" exit 0 else stop_daemon_for_all_ddns_sections exit 0 fi exit 1 ;; reload) killall dynamic_dns_updater.sh 2>/dev/null exit $? ;; *) usage_err "unknown command - $1";; esac # set file names PIDFILE="$ddns_rundir/$SECTION_ID.pid" # Process ID file UPDFILE="$ddns_rundir/$SECTION_ID.update" # last update successful send (system uptime) DATFILE="$ddns_rundir/$SECTION_ID.dat" # save stdout data of WGet and other extern programs called ERRFILE="$ddns_rundir/$SECTION_ID.err" # save stderr output of WGet and other extern programs called IPFILE="$ddns_rundir/$SECTION_ID.ip" # LOGFILE="$ddns_logdir/$SECTION_ID.log" # log file # VERBOSE > 1 delete logfile if exist to create an empty one # only with this data of this run for easier diagnostic # new one created by write_log function [ $VERBOSE -gt 1 -a -f $LOGFILE ] && rm -f $LOGFILE # Previously -v 3 could we used for dry run [ $VERBOSE -ge 3 ] && DRY_RUN=1 # TRAP handler trap "trap_handler 0 \$?" 0 # handle script exit with exit status trap "trap_handler 1" 1 # SIGHUP Hangup / reload config trap "trap_handler 2" 2 # SIGINT Terminal interrupt trap "trap_handler 3" 3 # SIGQUIT Terminal quit # trap "trap_handler 9" 9 # SIGKILL no chance to trap trap "trap_handler 15" 15 # SIGTERM Termination ################################################################################ # Leave this comment here, to clearly document variable names that are expected/possible # Use load_all_config_options to load config options, which is a much more flexible solution. # # config_load "ddns" # config_get $SECTION_ID