#!/bin/bash ######################################################################################### ### bash <(curl -fsSL https://raw.lhy.life/update-cfdns.sh) --zone_id="" --cf_token="" --domain="" --ip="" ######################################################################################### ## GetArgValue [defvalue] ## ./script --key=value g_args=("$@") function GetArgValueEx() { local key="$1" for arg in "${g_args[@]}" do if [[ "$arg" == "--$key="* ]] then echo "${arg#*=}" return 0 fi done if (( "$#" > 1 )); then echo "$2" return 0 fi echo "Error: Unable to find arg: $key" >&2 return 1 } function CheckCmd() { local cmd="$1" if ! command -v "$cmd" &>/dev/null; then echo "Error: command \"$cmd\" not found." exit 1 fi } CheckCmd "curl" CheckCmd "jq" ZONE_ID=$(GetArgValueEx "zone_id") || exit 1 CF_TOKEN=$(GetArgValueEx "cf_token") || exit 1 DOMAIN=$(GetArgValueEx "domain") || exit 1 IP=$(GetArgValueEx "ip") || exit 1 RECORD_ID=$(curl "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/dns_records?type=A&name=$DOMAIN" -H "Authorization: Bearer $CF_TOKEN" | jq -r '.result[0].id' ) curl -X PATCH "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/dns_records/$RECORD_ID" -H 'Content-Type: application/json' -H "Authorization: Bearer $CF_TOKEN" -d "{ \"ttl\": 300, \"type\": \"A\", \"content\": \"$IP\", \"proxied\": false }" echo ""