1. 首页
  2. 未分类

利用cloudflare 批量更新ddns

#!/bin/bash

# CHANGE THESE
auth_email="cloudflare用户名"
auth_key="cloudflarekey" # found in cloudflare account settings
zone_name="域名"

record_name=(替换需要更新的域名,空格隔开)
copy_record=(替换需要更新的域名,空格隔开,和上面一对一)

# MAYBE CHANGE THESE


id_file="cloudflare2.ids"
record_file="record_file/"
recordids=".ids"
for(( i=0;i<${#record_name[@]};i++)) do
    old_ip=`ping ${record_name[$i]}   -c 1 -w 1 | sed '1{s/[^(]*(//;s/).*//;q}'`
    ip=`ping ${copy_record[$i]}   -c 1 -w 1 | sed '1{s/[^(]*(//;s/).*//;q}'`
    if [ $ip == $old_ip ]; then
        echo "IP has not changed."
    fi

    if [ -f $id_file ]; then
        zone_identifier=$(head -1 $id_file)
    else
        zone_identifier=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones?name=$zone_name" -H "X-Auth-Email: $auth_email" -H "X-Auth-Key: $auth_key" -H "Content-Type: application/json" | grep -Po '(?<="id":")[^"]*' | head -1 )
        echo "$zone_identifier" > $id_file
    fi
    
    if [ ! -d "$record_file" ];then
        mkdir $record_file
    fi
    
    if [ -f $record_file${record_name[$i]}$recordids ]; then
        record_identifier=$(head -1 $record_file${record_name[$i]}$recordids)
    else
        record_identifier=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones/$zone_identifier/dns_records?name=${record_name[$i]}" -H "X-Auth-Email: $auth_email" -H "X-Auth-Key: $auth_key" -H "Content-Type: application/json"  | grep -Po '(?<="id":")[^"]*')
        echo "$record_identifier" >> $record_file${record_name[$i]}$recordids
    fi



    update=$(curl -s -X PUT "https://api.cloudflare.com/client/v4/zones/$zone_identifier/dns_records/$record_identifier" -H "X-Auth-Email: $auth_email" -H "X-Auth-Key: $auth_key" -H "Content-Type: application/json" --data "{\"id\":\"$zone_identifier\",\"type\":\"A\",\"name\":\"${record_name[$i]}\",\"content\":\"$ip\"}")

    if [[ $update == *"\"success\":false"* ]]; then
        message="API UPDATE FAILED. DUMPING RESULTS:\n$update"
        echo -e "$message"
    else
        message="IP changed to: $ip"
    fi
done;
exit 0

原创文章,作者:admin,如若转载,请注明出处:https://www.huiyingwu.com/1242/

发表评论

电子邮件地址不会被公开。 必填项已用*标注