dynamic-ip/src/agents.d/update-dns-namecheap

77 lines
2.0 KiB
Bash
Executable File

#!/bin/bash
# Internal Initialization
source "${DIP_FUNCTIONS}"
if [[ -r "${DIP_BASE_DIR}/conf.d/${DIP_AGENT_NAME}.conf" ]]; then
source "${DIP_BASE_DIR}/conf.d/${DIP_AGENT_NAME}.conf"
fi
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 "$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
hostname=${record%%.*}
domain=${record#*.}
if [[ -n "${agent_update_dns_namecheap_token[$domain]}" ]]; then
token="${agent_update_dns_namecheap_token[$domain]}"
return 0
else
logerr "ERROR: Password for $domain not found in agent configuration."
return 1
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}"
_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}"
_update_dns_namecheap "$hostname" "$domain" "$token" "${DIP_CUR_IP}"
fi