Added function to handle error checking namecheap

This commit is contained in:
Eric Renfro 2023-10-31 14:09:58 -04:00
parent 55ff658527
commit 7bba7ec9d8
Signed by: psi-jack
GPG Key ID: 14977F3A50D9A5BF
2 changed files with 32 additions and 25 deletions

View File

@ -12,33 +12,13 @@ if [[ -r "${DIP_BASE_DIR}/conf.d/${DIP_AGENT_EXEC}.conf" ]]; then
source "${DIP_BASE_DIR}/conf.d/${DIP_AGENT_EXEC}.conf"
fi
#if [[ -z "$agent_update_dns_r53_id" ]]; then
# logerr "ERROR: Need 'agent_update_dns_r53_id' to be defined to your Route53 Domain ID"
# exit 99
#fi
#if [[ -d "${DIP_BASE_DIR}/aws" ]]; then
# if [[ ! -r "${DIP_BASE_DIR}/aws/config" ]]; then
# logerr "ERROR: AWS config file not found: '${DIP_BASE_DIR}/aws/config'"
# exit 99
# elif [[ ! -r "${DIP_BASE_DIR}/aws/credentials" ]]; then
# logerr "ERROR: AWS credentials file not found: '${DIP_BASE_DIR}/aws/credentials'"
# exit 99
# else
# export AWS_CONFIG_FILE="${DIP_BASE_DIR}/aws/config"
# export AWS_SHARED_CREDENTIALS_FILE="${DIP_BASE_DIR}/aws/credentials"
# fi
#else
# logerr "ERROR: AWS config directory not found. '${DIP_BASE_DIR}/aws/' is expected to exist and contain 'config' and 'credentials' for AWS access."
# exit 99
#fi
if [[ -z "$DIP_CUR_IP" ]] || [[ -z "$DIP_OLD_IP" ]] || [[ -z "$DIP_RECORD" ]]; then
logerr "ERROR: Agent expects currentip, existingip, and record."
exit 98
fi
# Get Host and Domain Name
_get_host_and_domain() {
local record=$1
@ -54,16 +34,43 @@ _get_host_and_domain() {
fi
}
# Update and Check Response
_update_dns_namecheap() {
local dnsHostname=$1
local dnsDomain=$2
local dnsToken=$3
local dnsIP=$4
local result
local errors
result="$(curl "https://dynamicdns.park-your-domain.com/update?host=${dnsHostname}&domain=${dnsDomain}&password=${dnsToken}&ip=${dnsIP}" | grep ErrCount)"
if [[ $? -eq 0 ]]; then
errors="$(set -n -e 's/.*<ErrCount>\(.*\)<\/ErrCount>.*/\1/p' <<< $result)"
if [[ "$errors" -eq 0 ]]; then
echo "DNS record ${dnsHostname}.${dnsDomain} updated successfully."
return 0
else
echo "DNS record ${dnsHostname}.${dnsDomain} failed to update."
return 96
fi
else
echo "Failed to update DNS Record"
return 95
fi
}
# Main
_get_host_and_domain "$DIP_RECORD" || exit 99
if valid_ipv4 "$DIP_CUR_IP"; then
log "Updating Namecheap DNS IPv4 Record for ${hostname}.${domain}"
curl "https://dynamicdns.park-your-domain.com/update?host=${hostname}&domain=${domain}&password=${token}&ip=${DIP_CUR_IP}"
_update_dns_namecheap "$hostname" "$domain" "$token" "${DIP_CUR_IP}"
fi
if valid_ipv6 "$DIP_CUR_IP"; then
log "Updating Namecheap DNS IPv6 Record for ${hostname}.${domain}"
curl "https://dynamicdns.park-your-domain.com/update?host=${hostname}&domain=${domain}&password=${token}&ip=${DIP_CUR_IP}"
_update_dns_namecheap "$hostname" "$domain" "$token" "${DIP_CUR_IP}"
fi

View File

@ -1,5 +1,5 @@
# Namecheap DDNS Passwords
decalre -A agent_update_dns_namecheap_token
declare -A agent_update_dns_namecheap_token
#agent_update_dns_namecheap_token[domain.tld]=