{"record":{"id":"651f9021d5b05f5d","repo":"XIU2/CloudflareSpeedTest","slug":"failed-to-get-the-preferred-ipv4-address","errorCode":null,"errorMessage":"Failed to get the preferred IPv4 address.","messagePattern":"Failed to get the preferred IPv4 address\\.","errorType":"console","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"script/cfst_dnspod.sh","lineNumber":56,"sourceCode":"}\n\n# 更新 DNS 记录\nupdate_dns_record() {\n    local record_id=$1\n    local record_type=$2\n    local ip_address=$3\n    curl -s -X POST -d \"login_token=$dnspod_token&format=json&domain=$dnspod_domain&record_id=$record_id&sub_domain=$dnspod_record&record_type=$record_type&record_line=默认&value=$ip_address\" \"$dnspod_api_url/Record.Modify\"\n}\n\n# 运行 CFST v4\n./cfst -f ip.txt -n 500 -o result4.csv\n\n# 读取 CSV 文件并提取优选 IPv4 地址\npreferred_ipv4=$(awk -F, 'NR==2 {print $1}' result4.csv)\n\n# 检查是否获取到了 IPv4 地址\nif [ -z \"$preferred_ipv4\" ]; then\n  echo \"Failed to get the preferred IPv4 address.\"\nelse\n  echo \"BETTER IPv4: $preferred_ipv4\"\n\n  # 获取 IPv4 记录 ID\n  ipv4_record_id=$(get_record_id \"A\")\n\n  if [ -n \"$ipv4_record_id\" ]; then\n    # 更新 IPv4 记录\n    update_dns_record \"$ipv4_record_id\" \"A\" \"$preferred_ipv4\"\n    echo \"Updated DNSPod record with IPv4: $preferred_ipv4\"\n  else\n    # 创建 IPv4 记录\n    new_ipv4_record_id=$(create_dns_record \"A\" \"$preferred_ipv4\")\n    if [ -n \"$new_ipv4_record_id\" ]; then\n      echo \"Created DNSPod record with IPv4: $preferred_ipv4\"\n    else\n      echo \"Failed to create DNSPod record with IPv4.\"\n    fi","sourceCodeStart":38,"sourceCodeEnd":74,"githubUrl":"https://github.com/XIU2/CloudflareSpeedTest/blob/1da0c025d7b6f9bf0b2b0cf47a547f65430deab2/script/cfst_dnspod.sh#L38-L74","documentation":"cfst_dnspod.sh runs ./cfst -f ip.txt -n 500 -o result4.csv (line 49, after rm -f result4.csv result6.csv at line 10), then reads preferred_ipv4=$(awk -F, 'NR==2 {print $1}' result4.csv) at line 52. When that is empty the script prints this message at line 56 and skips the whole IPv4 branch (no DNSPod lookup/create/update); execution continues to the IPv6 section.","triggerScenarios":"result4.csv missing because cfst never ran (binary not executable, ip.txt absent in CWD) or cfst exited without qualifying IPs; or result4.csv exists with only a header row so awk NR==2 finds nothing.","commonSituations":"Running from a directory without ./cfst or ip.txt (credentials are env vars — API_TOKEN/DOMAIN/SUB_DOMAIN — so the file requirements are easy to miss); -n 500 exceeding the usable entries in ip.txt; all IPs failed latency filtering; unattended cron with the wrong cwd.","solutions":["Run ./cfst -f ip.txt -n 500 -o result4.csv manually and confirm it executes and writes data rows","Verify ./cfst is executable and ip.txt exists in the working directory (adjust -n to at most the IP list size)","Relax -t/-sl filters if the CSV is header-only, then re-run","Invoke with credentials exported and from the right directory: API_TOKEN='id,token' DOMAIN=example.com SUB_DOMAIN=www bash cfst_dnspod.sh","Add a file guard before line 52: [ -s result4.csv ] || { echo 'no result4.csv'; exit 1; }"],"exampleFix":"# script/cfst_dnspod.sh (before)\npreferred_ipv4=$(awk -F, 'NR==2 {print $1}' result4.csv)\n# (after)\nif [ -s result4.csv ] && [ \"$(wc -l < result4.csv)\" -ge 2 ]; then\n  preferred_ipv4=$(awk -F, 'NR==2 {print $1}' result4.csv)\nelse\n  preferred_ipv4=''\nfi","handlingStrategy":"validation","validationCode":"[ -x ./cfst ] && [ -f ip.txt ] && [ -n \"$API_TOKEN\" ] && [ -n \"$DOMAIN\" ] && [ -n \"$SUB_DOMAIN\" ] || { echo 'prereqs missing (cfst/ip.txt/API_TOKEN/DOMAIN/SUB_DOMAIN)' >&2; exit 1; }","typeGuard":"is_ipv4(){ [[ $1 =~ ^([0-9]{1,3}\\.){3}[0-9]{1,3}$ ]]; }\nis_ipv4 \"$preferred_ipv4\" || echo 'preferred_ipv4 not a valid IPv4' >&2","tryCatchPattern":null,"preventionTips":["Export API_TOKEN ('id,token' comma form), DOMAIN, SUB_DOMAIN in the environment/wrapper before running","Check result4.csv exists and has at least 2 lines after each cfst run; treat absence as a skip signal, not success","Size -n to your ip.txt (-n 500 is pointless for shorter lists) and relax -t/-sl when the CSV is header-only"],"tags":["shell","bash","dnspod","dns","speedtest","empty-result"],"backgroundTag":null,"analyzedSha":"1da0c025d7b6f9bf0b2b0cf47a547f65430deab2","analyzedAt":"2026-08-15T22:22:10.737Z","schemaVersion":2},"datasetVersion":"2026-08-16T03:17:38.424Z"}