2 # /usr/lib/ddns/dynamic_dns_lucihelper.sh
4 #.Distributed under the terms of the GNU General Public License (GPL) version 2.0
5 #.2014-2018 Christian Schoenebeck <christian dot schoenebeck at gmail dot com>
6 # This script is used by luci-app-ddns
8 # variables in small chars are read from /etc/config/ddns as parameter given here
9 # variables in big chars are defined inside these scripts as gloval vars
10 # variables in big chars beginning with "__" are local defined inside functions only
11 # set -vx #script debugger
13 .
/usr
/lib
/ddns
/dynamic_dns_functions.sh
# global vars are also defined here
19 $MYPROG [options] -- command
22 get_local_ip using given INTERFACE or NETWORK or SCRIPT or URL
23 get_registered_ip for given FQDN
24 verify_dns given DNS-SERVER
25 verify_proxy given PROXY
26 start start given SECTION
27 reload force running ddns processes to reload changed configuration
28 restart restart all ddns processes
31 -6 => use_ipv6=1 (default 0)
32 -d DNS-SERVER => dns_server=SERVER[:PORT]
33 -f => force_ipversion=1 (default 0)
34 -g => is_glue=1 (default 0)
35 -i INTERFACE => ip_interface=INTERFACE; ip_source="interface"
36 -l FQDN => lookup_host=FQDN
37 -n NETWORK => ip_network=NETWORK; ip_source="network"
38 -p PROXY => proxy=[USER:PASS@]PROXY:PORT
39 -s SCRIPT => ip_script=SCRIPT; ip_source="script"
40 -t => force_dnstcp=1 (default 0)
41 -u URL => ip_url=URL; ip_source="web"
42 -S SECTION SECTION to start
44 -h => show this help and exit
45 -L => use_logfile=1 (default 0)
46 -v LEVEL => VERBOSE=LEVEL (default 0)
47 -V => show version and exit
53 printf %s
\\n
"$MYPROG: $@" >&2
58 # preset some variables, wrong or not set in ddns-functions.sh
59 SECTION_ID
="lucihelper"
60 LOGFILE
="$ddns_logdir/$SECTION_ID.log"
61 DATFILE
="$ddns_rundir/$SECTION_ID.$$.dat" # save stdout data of WGet and other extern programs called
62 ERRFILE
="$ddns_rundir/$SECTION_ID.$$.err" # save stderr output of WGet and other extern programs called
63 DDNSPRG
="/usr/lib/ddns/dynamic_dns_updater.sh"
64 VERBOSE
=0 # no console logging
65 # global variables normally set by reading DDNS UCI configuration
66 use_syslog
=0 # no syslog
67 use_logfile
=0 # no logfile
69 use_ipv6
=0 # Use IPv6 - default IPv4
70 force_ipversion
=0 # Force IP Version - default 0 - No
71 force_dnstcp
=0 # Force TCP on DNS - default 0 - No
72 is_glue
=0 # Is glue record - default 0 - No
73 use_https
=0 # not needed but must be set
75 while getopts ":6d:fghi:l:n:p:s:S:tu:Lv:V" OPT
; do
78 d
) dns_server
="$OPTARG";;
79 f
) force_ipversion
=1;;
81 i
) ip_interface
="$OPTARG"; ip_source
="interface";;
82 l
) lookup_host
="$OPTARG";;
83 n
) ip_network
="$OPTARG"; ip_source
="network";;
85 s
) ip_script
="$OPTARG"; ip_source
="script";;
87 u
) ip_url
="$OPTARG"; ip_source
="web";;
92 V
) printf %s
\\n
"ddns-scripts $VERSION"; exit 255;;
93 :) usage_err
"option -$OPTARG missing argument";;
94 \?) usage_err
"invalid option -$OPTARG";;
95 *) usage_err
"unhandled option -$OPT $OPTARG";;
98 shift $
((OPTIND
- 1 )) # OPTIND is 1 based
100 [ $# -eq 0 ] && usage_err
"missing command"
105 [ -z "$lookup_host" ] && usage_err
"command 'get_registered_ip': 'lookup_host' not set"
106 write_log
7 "-----> get_registered_ip IP"
107 [ -z "$SECTION" ] || IPFILE
="$ddns_rundir/$SECTION.ip"
111 [ $__RET -ne 0 ] && IP
=""
115 [ -z "$dns_server" ] && usage_err
"command 'verify_dns': 'dns_server' not set"
116 write_log
7 "-----> verify_dns '$dns_server'"
117 verify_dns
"$dns_server"
121 [ -z "$proxy" ] && usage_err
"command 'verify_proxy': 'proxy' not set"
122 write_log
7 "-----> verify_proxy '$proxy'"
123 verify_proxy
"$proxy"
127 [ -z "$ip_source" ] && usage_err
"command 'get_local_ip': 'ip_source' not set"
128 [ -n "$proxy" -a "$ip_source" = "web" ] && {
129 # proxy defined, used for ip_source=web
130 export HTTP_PROXY
="http://$proxy"
131 export HTTPS_PROXY
="http://$proxy"
132 export http_proxy
="http://$proxy"
133 export https_proxy
="http://$proxy"
135 # don't need IP only the return code
137 if [ "$ip_source" = "web" -o "$ip_source" = "script" ]; then
138 # we wait only 3 seconds for an
139 # answer from "web" or "script"
140 write_log
7 "-----> timeout 3 -- get_local_ip IP"
141 timeout
3 -- get_local_ip IP
143 write_log
7 "-----> get_local_ip IP"
149 [ -z "$SECTION" ] && usage_err
"command 'start': 'SECTION' not set"
150 if [ $VERBOSE -eq 0 ]; then # start in background
151 $DDNSPRG -v 0 -S $SECTION -- start
&
153 $DDNSPRG -v $VERBOSE -S $SECTION -- start
169 # remove out and err file
170 [ -f $DATFILE ] && rm -f $DATFILE
171 [ -f $ERRFILE ] && rm -f $ERRFILE