利用cloudflare 批量更新ddns

150次阅读
没有评论

共计 1699 个字符,预计需要花费 5 分钟才能阅读完成。

#!/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
版权声明:本站原创文章,由 admin 2019-10-07发表,共计1699字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
评论(没有评论)
验证码