#!/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" ]] || [[ -z "$DIP_DOMAIN" ]]; then logerr "ERROR: Agent expects currentip, existingip, record and domain." exit 98 fi # Get Host and Domain Name _get_host_and_domain() { local record=$1 local domain=$2 local hostname if [[ -z "$record" ]] || [[ -z "$domain" ]]; then return 1 fi hostname="${record%%."$domain"}" if [[ -n "$hostname" ]]; then DIP_RECORD="$hostname" else return 1 fi #hostname=${record%%.*} #hostname="${record%%."$domain"}" #domain=${record#*.} if [[ -n "${agent_update_dns_namecheap_token[$domain]}" ]]; then DIP_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 rec=$1 local dom=$2 local pas=$3 local ip=$4 local result local errors if result=$(curl -qs "https://dynamicdns.park-your-domain.com/update?host=${rec}&domain=${dom}&password=${pas}&ip=${ip}" | grep ErrCount); then errors=$(grep -oPm1 "(?<=)[^<]+" <<< "$result") if [[ "$errors" -eq 0 ]]; then echo "DNS record ${rec}.${dom} updated successfully." return 0 else echo "DNS record ${rec}.${dom} failed to update." return 96 fi else echo "Failed to update DNS Record" return 95 fi } # Main _get_host_and_domain "$DIP_RECORD" "$DIP_DOMAIN" || exit 99 if valid_ipv4 "$DIP_CUR_IP"; then log "Updating Namecheap DNS IPv4 Record for ${DIP_RECORD}.${DIP_DOMAIN}" _update_dns_namecheap "$DIP_RECORD" "$DIP_DOMAIN" "$DIP_TOKEN" "${DIP_CUR_IP}" unset DIP_TOKEN exit $? fi if valid_ipv6 "$DIP_CUR_IP"; then log "Updating Namecheap DNS IPv6 Record for ${DIP_RECORD}.${DIP_DOMAIN}" _update_dns_namecheap "$DIP_RECORD" "$DIP_DOMAIN" "$DIP_TOKEN" "${DIP_CUR_IP}" unset DIP_TOKEN exit $? fi