Added core plugin approach. Changes not yet documented
This commit is contained in:
parent
abd1c8fc71
commit
52546805ad
5 changed files with 161 additions and 116 deletions
226
src/dynamic-ip
226
src/dynamic-ip
|
@ -14,17 +14,30 @@ trim() {
|
|||
echo -n "$var"
|
||||
}
|
||||
|
||||
valid_ip() {
|
||||
detect_ip_version() {
|
||||
local ip=$1
|
||||
|
||||
if [[ "${prog_name,,}" == "update-ipv4" ]]; then
|
||||
valid_ipv4 "$ip"
|
||||
return $?
|
||||
elif [[ "${prog_name,,}" == "update-ipv6" ]]; then
|
||||
valid_ipv6 "$ip"
|
||||
return $?
|
||||
if valid_ipv4 "$ip"; then
|
||||
return 4;
|
||||
elif valid_ipv6 "ip"; then
|
||||
return 6
|
||||
else
|
||||
return 0;
|
||||
fi
|
||||
return 89
|
||||
}
|
||||
|
||||
valid_ip() {
|
||||
local ip=$1
|
||||
local iptype
|
||||
|
||||
detect_ip_version "$ip"
|
||||
iptype=$?
|
||||
|
||||
case $iptype in
|
||||
4) return 0;;
|
||||
6) return 0;;
|
||||
*) return 1;;
|
||||
esac
|
||||
}
|
||||
|
||||
valid_ipv4() {
|
||||
|
@ -144,9 +157,9 @@ getDnsNS() {
|
|||
fi
|
||||
}
|
||||
|
||||
getDnsIP() {
|
||||
getDnsRecord() {
|
||||
local rec=$1
|
||||
local rectype
|
||||
local rectype=$2
|
||||
local result
|
||||
local err
|
||||
|
||||
|
@ -154,14 +167,6 @@ getDnsIP() {
|
|||
do
|
||||
[[ "$i" = "ERROR" ]] && return 1
|
||||
|
||||
if [[ "${prog_name,,}" == "update-ipv4" ]]; then
|
||||
rectype=A
|
||||
elif [[ "${prog_name,,}" == "update-ipv6" ]]; then
|
||||
rectype=AAAA
|
||||
else
|
||||
return 88
|
||||
fi
|
||||
|
||||
result=$(dig +short @${i} ${rectype} ${rec} | head -n1)
|
||||
err=$?
|
||||
|
||||
|
@ -344,21 +349,22 @@ run-update() {
|
|||
|
||||
# Caches current local IP so it doesn't have to every time.
|
||||
getCurrentLocalIP() {
|
||||
local iptype=$1
|
||||
local cip
|
||||
|
||||
if [[ -z "$currentip" ]]; then
|
||||
case "${prog_name,,}" in
|
||||
update-ipv4) cip=$(getIPv4) || return 1;;
|
||||
update-ipv6) cip=$(getIPv6) || return 1;;
|
||||
*) return 3;;
|
||||
case "$iptype" in
|
||||
4) cip=$(getIPv4) || return 1;;
|
||||
6) cip=$(getIPv6) || return 1;;
|
||||
*) return 3;;
|
||||
esac
|
||||
|
||||
echo "$cip"
|
||||
else
|
||||
case "${prog_name,,}" in
|
||||
update-ipv4) valid_ipv4 "$currentip" || return 1;;
|
||||
update-ipv6) valid_ipv6 "$currentip" || return 1;;
|
||||
*) return 3;;
|
||||
case "$iptype" in
|
||||
4) valid_ipv4 "$currentip" || return 1;;
|
||||
6) valid_ipv6 "$currentip" || return 1;;
|
||||
*) return 3;;
|
||||
esac
|
||||
|
||||
echo "$currentip"
|
||||
|
@ -366,99 +372,68 @@ getCurrentLocalIP() {
|
|||
}
|
||||
|
||||
getCurrentIP() {
|
||||
local i=$1
|
||||
local iptype=$1
|
||||
local record=$2
|
||||
|
||||
if [[ -z "$i" ]]; then
|
||||
return 1
|
||||
else
|
||||
case "${prog_name,,}" in
|
||||
update-ipv4)
|
||||
log "Checking if internet IP has changed for $i"
|
||||
currentip=$(getCurrentLocalIP) || return $?
|
||||
externalip=$(getDnsIP $i) || return 2
|
||||
return 0
|
||||
;;
|
||||
update-ipv6)
|
||||
log "Checking if internet IPv6 has changed for $i"
|
||||
currentip=$(getCurrentLocalIP) || return $?
|
||||
externalip=$(getDnsIP $i) || return 2
|
||||
return 0
|
||||
;;
|
||||
*) logerr "FATAL"; return 3 ;;
|
||||
esac
|
||||
fi
|
||||
[[ -z "$iptype" ]] && return 1
|
||||
[[ -z "$record" ]] && return 1
|
||||
|
||||
case "$iptype" in
|
||||
4) log "Checking if internet IP has changed for $record"
|
||||
currentip=$(getCurrentLocalIP 4) || return $?
|
||||
externalip=$(getDnsRecord "$record" "A") || return 2
|
||||
return 0
|
||||
;;
|
||||
6) log "Checking if internet IPv6 has changed for $record"
|
||||
currentip=$(getCurrentLocalIP 6) || return $?
|
||||
externalip=$(getDnsRecord "$record" "AAAA") || return 2
|
||||
return 0
|
||||
;;
|
||||
*) logerr "FATAL"; return 3 ;;
|
||||
esac
|
||||
}
|
||||
|
||||
check-update() {
|
||||
local arg=$1
|
||||
local d
|
||||
local iptype=$1
|
||||
local record=$2
|
||||
|
||||
if [[ -n "$arg" ]]; then
|
||||
getCurrentIP $arg
|
||||
err=$?
|
||||
[[ -z "$iptype" ]] && return 1
|
||||
[[ -z "$record" ]] && return 1
|
||||
|
||||
case $err in
|
||||
0) if [[ "$currentip" != "$externalip" ]]; then
|
||||
log "Updates found: $externalip is not $currentip"
|
||||
log "Running Agents for $arg"
|
||||
run-update "$currentip" "$externalip" "$arg"
|
||||
err=$?
|
||||
getCurrentIP $iptype $record
|
||||
err=$?
|
||||
|
||||
if [[ $err -ne 0 ]]; then
|
||||
logerr "WARNING: Agents had $err errors"
|
||||
else
|
||||
log "Agents ran successfully"
|
||||
fi
|
||||
case $err in
|
||||
0) if [[ "$currentip" != "$externalip" ]]; then
|
||||
log "Updates found: $externalip is not $currentip"
|
||||
log "Running Agents for $record"
|
||||
run-update "$currentip" "$externalip" "$record"
|
||||
err=$?
|
||||
|
||||
if [[ $err -ne 0 ]]; then
|
||||
logerr "WARNING: Agents had $err errors"
|
||||
else
|
||||
log "No change detected"
|
||||
log "Agents ran successfully"
|
||||
fi
|
||||
;;
|
||||
1) logerr "ERROR: Failed to determine current public IP"
|
||||
logerr "DEBUG: dig output '$currentip'"
|
||||
exit 4
|
||||
;;
|
||||
2) logerr "ERROR: Failed to determine external DNS IP"
|
||||
logerr "DEBUG: dig output '$externalip'"
|
||||
;;
|
||||
*) logerr "Unknown fatal error occurred"
|
||||
eclogerrho "(${BASH_SOURCE}:${LINENO}): ${FUNCNAME[0]:+${FUNCNAME[0]}(): }"
|
||||
exit $err
|
||||
;;
|
||||
esac
|
||||
else
|
||||
if [[ -n "$record" ]]; then
|
||||
check-update "$record"
|
||||
elif [[ -n "$record_file" ]]; then
|
||||
for d in $(getRecords "$record_file"); do
|
||||
check-update "$d"
|
||||
done
|
||||
else
|
||||
return 99
|
||||
fi
|
||||
fi
|
||||
else
|
||||
log "No change detected"
|
||||
fi
|
||||
;;
|
||||
1) logerr "ERROR: Failed to determine current public IP"
|
||||
logerr "DEBUG: dig output '$currentip'"
|
||||
exit 4
|
||||
;;
|
||||
2) logerr "ERROR: Failed to determine external DNS IP"
|
||||
logerr "DEBUG: dig output '$externalip'"
|
||||
;;
|
||||
*) logerr "Unknown fatal error occurred"
|
||||
eclogerrho "(${BASH_SOURCE}:${LINENO}): ${FUNCNAME[0]:+${FUNCNAME[0]}(): }"
|
||||
exit $err
|
||||
;;
|
||||
esac
|
||||
}
|
||||
|
||||
|
||||
# This section doesn't run if this script was sourced.
|
||||
|
||||
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
|
||||
# Internal Initialization
|
||||
|
||||
prog_name=$(basename "$0")
|
||||
script_dir=$(dirname "$(readlink -f "$0")")
|
||||
|
||||
if [[ -z "$1" ]]; then
|
||||
logerr "ERROR: Need to provide a DNS record or file to look-up"
|
||||
exit 1
|
||||
else
|
||||
if [[ -r "$1" ]]; then
|
||||
record_file=$1
|
||||
else
|
||||
record=$1
|
||||
fi
|
||||
fi
|
||||
|
||||
|
||||
prog_lock() {
|
||||
## Locking
|
||||
|
||||
if [[ "$USER" = "root" ]]; then
|
||||
|
@ -490,13 +465,36 @@ if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
|
|||
|
||||
# Remember! Lock file is removed when one of the scripts exits and it is
|
||||
# the only script holding the lock or lock is not acquired at all.
|
||||
}
|
||||
|
||||
|
||||
# This section doesn't run if this script was sourced.
|
||||
|
||||
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
|
||||
# Internal Initialization
|
||||
|
||||
prog_name=$(basename "$0")
|
||||
script_dir=$(dirname "$(readlink -f "$0")")
|
||||
|
||||
if [[ -z "$1" ]]; then
|
||||
logerr "ERROR: Need to provide a DNS record or file to look-up"
|
||||
exit 1
|
||||
else
|
||||
if [[ -r "$1" ]]; then
|
||||
record_file=$1
|
||||
else
|
||||
record=$1
|
||||
fi
|
||||
fi
|
||||
|
||||
prog_lock
|
||||
|
||||
|
||||
# Main
|
||||
|
||||
case "${prog_name,,}" in
|
||||
update-ipv4) check-update; exit $? ;;
|
||||
update-ipv6) check-update; exit $? ;;
|
||||
*) logerr "ERROR: Unknown update method"; exit 1 ;;
|
||||
esac
|
||||
if [[ -r "${script_dir}/plugins/${1}" ]]; then
|
||||
source "${script_dir}/plugins/${1}"
|
||||
plugin_name=${1,,}
|
||||
else
|
||||
logerr "ERROR: Unknown plugin '${1}'"
|
||||
fi
|
||||
fi
|
||||
|
|
25
src/plugins/update-ipv4
Normal file
25
src/plugins/update-ipv4
Normal file
|
@ -0,0 +1,25 @@
|
|||
plugin_name=$1
|
||||
check_record=$2
|
||||
|
||||
updateerrors=0
|
||||
|
||||
if [[ -z "$check_record" ]]; then
|
||||
logerr "ERROR: Need to provide a DNS record or file to look-up"
|
||||
exit 1
|
||||
else
|
||||
if [[ -r "$check_record" ]]; then
|
||||
for d in $(getRecords "$check_record"); do
|
||||
check-update 4 "$check_record"
|
||||
if [[ $? -ne 0 ]]; then
|
||||
let updateerrors++
|
||||
fi
|
||||
done
|
||||
else
|
||||
check-update 4 "$check_record"
|
||||
if [[ $? -ne 0 ]]; then
|
||||
let updateerrors++
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
|
||||
exit $updateerrors
|
24
src/plugins/update-ipv6
Normal file
24
src/plugins/update-ipv6
Normal file
|
@ -0,0 +1,24 @@
|
|||
plugin_name=$1
|
||||
check_record=$2
|
||||
updateerrors=0
|
||||
|
||||
if [[ -z "$check_record" ]]; then
|
||||
logerr "ERROR: Need to provide a DNS record or file to look-up"
|
||||
exit 1
|
||||
else
|
||||
if [[ -r "$check_record" ]]; then
|
||||
for d in $(getRecords "$check_record"); do
|
||||
check-update 6 "$check_record"
|
||||
if [[ $? -ne 0 ]]; then
|
||||
let updateerrors++
|
||||
fi
|
||||
done
|
||||
else
|
||||
check-update 6 "$check_record"
|
||||
if [[ $? -ne 0 ]]; then
|
||||
let updateerrors++
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
|
||||
exit $updateerrors
|
|
@ -1 +0,0 @@
|
|||
dynamic-ip
|
|
@ -1 +0,0 @@
|
|||
dynamic-ip
|
Loading…
Reference in a new issue