feature: auto-detect ip, update exit func

- auto-detect IP address if not supplied
- rework exit functions: one for error, one for success
This commit is contained in:
Asif Bacchus 2021-05-07 15:32:46 -06:00
parent cf820d777d
commit 53f7245001
2 changed files with 65 additions and 26 deletions

View File

@ -0,0 +1,6 @@
<component name="InspectionProjectProfileManager">
<profile version="1.0">
<option name="myName" value="Project Default" />
<inspection_tool class="possibleLocalVar" enabled="false" level="WEAK WARNING" enabled_by_default="false" />
</profile>
</component>

View File

@ -45,6 +45,23 @@ badParam() {
fi fi
} }
exitError() {
if [ -z "$1" ]; then
printf "%s[%s] ERROR: An unspecified error occurred. Exiting.%s\n" "$err" "$(stamp)" "$norm" >>"$logFile"
exit 99
elif [ "$1" -eq 10 ]; then
errMsg="Unable to auto-detect IP address. Try again later or supply the IP address to be used."
fi
printf "%s[%s] ERROR: %s (code: %s)%s\n" "$err" "$(stamp)" "$errMsg" "$1" "$norm" >>"$logFile"
printf "%s[%s] -- CloudFlare DDNS update-script: execution completed with error --%s\n" "$err" "$(stamp)" "$norm" >>"$logFile"
exit "$1"
}
exitOK() {
printf "%s[%s] -- CloudFlare DDNS update-script: execution complete --%s\n" "$ok" "$(stamp)" "$norm" >>"$logFile"
exit 0
}
stamp() { stamp() {
(date +%F" "%T) (date +%F" "%T)
} }
@ -54,14 +71,6 @@ scriptHelp() {
exit 0 exit 0
} }
quit() {
if [ -z "$1" ]; then
# exit gracefully
printf "%s[%s] -- CloudFlare DDNS update-script: execution complete --%s\n" "$ok" "$(stamp)" "$norm" >>"$logFile"
exit 0
fi
}
### default variable values ### default variable values
scriptPath="$(CDPATH='' \cd -- "$(dirname -- "$0")" && pwd -P)" scriptPath="$(CDPATH='' \cd -- "$(dirname -- "$0")" && pwd -P)"
scriptName="$(basename "$0")" scriptName="$(basename "$0")"
@ -70,11 +79,11 @@ accountFile="$scriptPath/cloudflare.credentials"
colourizeLogFile=1 colourizeLogFile=1
dnsRecords="" dnsRecords=""
dnsSeparator="," dnsSeparator=","
ipAddress="$(hostname -i)" ipAddress=""
ip4=1 ip4=1
ip6=0 ip6=0
errCount=0 ip4DetectionSvc="http://ipv4.icanhazip.com"
warnCount=0 ip6DetectionSvc="http://ipv6.icanhazip.com"
### process startup parameters ### process startup parameters
if [ -z "$1" ]; then if [ -z "$1" ]; then
@ -173,25 +182,49 @@ fi
printf "%sParameters:\n" "$magenta" printf "%sParameters:\n" "$magenta"
printf "script path: %s\n" "$scriptPath/$scriptName" printf "script path: %s\n" "$scriptPath/$scriptName"
printf "credentials file: %s\n" "$accountFile" printf "credentials file: %s\n" "$accountFile"
if [ "$ip4" = 1 ]; then
printf "mode: IP4\n"
elif [ "$ip6" = 1 ]; then
printf "mode: IP6\n"
fi
printf "ddns ip address: %s\n" "$ipAddress"
# iterate DNS records to update
dnsRecordsToUpdate="$dnsRecords$dnsSeparator"
while [ "$dnsRecordsToUpdate" != "${dnsRecordsToUpdate#*${dnsSeparator}}" ] && { [ -n "${dnsRecordsToUpdate%%${dnsSeparator}*}" ] || [ -n "${dnsRecordsToUpdate#*${dnsSeparator}}" ]; }; do
record="${dnsRecordsToUpdate%%${dnsSeparator}*}"
dnsRecordsToUpdate="${dnsRecordsToUpdate#*${dnsSeparator}}"
printf "updating record: %s\n" "$record"
done
printf "(end of parameter list)%s\n" "$norm"
} >>"$logFile" } >>"$logFile"
exit 0 if [ "$ip4" -eq 1 ]; then
printf "mode: IP4\n" >>"$logFile"
elif [ "$ip6" -eq 1 ]; then
printf "mode: IP6\n" >>"$logFile"
fi
# detect and report IP address
if [ -z "$ipAddress" ]; then
# detect public ip address
if [ "$ip4" -eq 1 ]; then
if ! ipAddress="$(curl -s $ip4DetectionSvc)"; then
printf "ddns ip address: %serror%s\n" "$err" "$norm" >>"$logFile"
exitError 10
fi
fi
if [ "$ip6" -eq 1 ]; then
if ! ipAddress="$(curl -s $ip6DetectionSvc)"; then
printf "ddns ip address: %serror%s\n" "$err" "$norm" >>"$logFile"
exitError 10
fi
fi
printf "ddns ip address (detected): %s\n" "$ipAddress" >>"$logFile"
else
printf "ddns ip address (supplied): %s\n" "$ipAddress" >>"$logFile"
fi
# iterate DNS records to update
dnsRecordsToUpdate="$dnsRecords$dnsSeparator"
while [ "$dnsRecordsToUpdate" != "${dnsRecordsToUpdate#*${dnsSeparator}}" ] && { [ -n "${dnsRecordsToUpdate%%${dnsSeparator}*}" ] || [ -n "${dnsRecordsToUpdate#*${dnsSeparator}}" ]; }; do
record="${dnsRecordsToUpdate%%${dnsSeparator}*}"
dnsRecordsToUpdate="${dnsRecordsToUpdate#*${dnsSeparator}}"
printf "updating record: %s\n" "$record" >>"$logFile"
done
printf "(end of parameter list)%s\n" "$norm" >>"$logFile"
# exit gracefully
exitOK
### exit return codes ### exit return codes
# 0: normal exit, no errors # 0: normal exit, no errors
# 1: invalid or unknown parameter # 1: invalid or unknown parameter
# 2: cannot find or access curl # 2: cannot find or access curl
# 99: unspecified error occurred