lede-packages-rs

git clone git://archive.git.mtrnord.blog/MTRNord/lede-packages-rs.git
Log | Files | Refs | README | LICENSE

dynamic_dns_lucihelper.sh (5333B)


      1 #!/bin/sh
      2 # /usr/lib/ddns/dynamic_dns_lucihelper.sh
      3 #
      4 #.Distributed under the terms of the GNU General Public License (GPL) version 2.0
      5 #.2014-2017 Christian Schoenebeck <christian dot schoenebeck at gmail dot com>
      6 # This script is used by luci-app-ddns
      7 #
      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
     12 
     13 . /usr/lib/ddns/dynamic_dns_functions.sh	# global vars are also defined here
     14 
     15 usage() {
     16 	cat << EOF
     17 
     18 Usage:
     19  $MYPROG [options] -- command
     20 
     21 Commands:
     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
     29 
     30 Parameters:
     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
     43 
     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
     48 
     49 EOF
     50 }
     51 
     52 usage_err() {
     53 	printf %s\\n "$MYPROG: $@" >&2
     54 	usage >&2
     55 	exit 255
     56 }
     57 
     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
     68 
     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
     74 
     75 while getopts ":6d:fghi:l:n:p:s:S:tu:Lv:V" OPT; do
     76 	case "$OPT" in
     77 		6)	use_ipv6=1;;
     78 		d)	dns_server="$OPTARG";;
     79 		f)	force_ipversion=1;;
     80 		g)	is_glue=1;;
     81 		i)	ip_interface="$OPTARG"; ip_source="interface";;
     82 		l)	lookup_host="$OPTARG";;
     83 		n)	ip_network="$OPTARG"; ip_source="network";;
     84 		p)	proxy="$OPTARG";;
     85 		s)	ip_script="$OPTARG"; ip_source="script";;
     86 		t)	force_dnstcp=1;;
     87 		u)	ip_url="$OPTARG"; ip_source="web";;
     88 		h)	usage; exit 255;;
     89 		L)	use_logfile=1;;
     90 		v)	VERBOSE=$OPTARG;;
     91 		S)	SECTION=$OPTARG;;
     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";;
     96 	esac
     97 done
     98 shift $((OPTIND - 1 ))	# OPTIND is 1 based
     99 
    100 [ $# -eq 0 ] && usage_err "missing command"
    101 
    102 __RET=0
    103 case "$1" in
    104 	get_registered_ip)
    105 		[ -z "$lookup_host" ] && usage_err "command 'get_registered_ip': 'lookup_host' not set" 
    106 		write_log 7 "-----> get_registered_ip IP"
    107 		IP=""
    108 		get_registered_ip IP
    109 		__RET=$?
    110 		[ $__RET -ne 0 ] && IP=""
    111 		printf "%s" "$IP"
    112 		;;
    113 	verify_dns)
    114 		[ -z "$dns_server" ] && usage_err "command 'verify_dns': 'dns_server' not set" 
    115 		write_log 7 "-----> verify_dns '$dns_server'"
    116 		verify_dns "$dns_server"
    117 		__RET=$?
    118 		;;
    119 	verify_proxy)
    120 		[ -z "$proxy" ] && usage_err "command 'verify_proxy': 'proxy' not set" 
    121 		write_log 7 "-----> verify_proxy '$proxy'"
    122 		verify_proxy "$proxy"
    123 		__RET=$?
    124 		;;
    125 	get_local_ip)
    126 		[ -z "$ip_source" ] && usage_err "command 'get_local_ip': 'ip_source' not set" 
    127 		[ -n "$proxy" -a "$ip_source" = "web" ] && {
    128 			# proxy defined, used for ip_source=web
    129 			export HTTP_PROXY="http://$proxy"
    130 			export HTTPS_PROXY="http://$proxy"
    131 			export http_proxy="http://$proxy"
    132 			export https_proxy="http://$proxy"
    133 		}
    134 		# don't need IP only the return code
    135 		IP=""
    136 		if [ "$ip_source" = "web" -o  "$ip_source" = "script" ]; then
    137 			# we wait only 3 seconds for an
    138 			# answer from "web" or "script"
    139 			write_log 7 "-----> timeout 3 -- get_local_ip IP"
    140 			timeout 3 -- get_local_ip IP
    141 		else
    142 			write_log 7 "-----> get_local_ip IP"
    143 			get_local_ip IP
    144 		fi
    145 		__RET=$?
    146 		;;
    147 	start)
    148 		[ -z "$SECTION" ] &&  usage_err "command 'start': 'SECTION' not set"
    149 		if [ $VERBOSE -eq 0 ]; then	# start in background
    150 			$DDNSPRG -v 0 -S $SECTION -- start &
    151 		else
    152 			$DDNSPRG -v $VERBOSE -S $SECTION -- start
    153 		fi
    154 		;;
    155 	reload)
    156 		$DDNSPRG -- reload
    157 		;;
    158 	restart)
    159 		$DDNSPRG -- stop
    160 		sleep 1
    161 		$DDNSPRG -- start
    162 		;;
    163 	*)
    164 		__RET=255
    165 		;;
    166 esac
    167 
    168 # remove out and err file
    169 [ -f $DATFILE ] && rm -f $DATFILE
    170 [ -f $ERRFILE ] && rm -f $ERRFILE
    171 return $__RET