Linux脚本-更新IP到CloudFlare

602 次浏览次阅读
#!/bin/sh
#echo "Datetime: "$(date)
#到 CloudFlare 个人资料获取 API 令牌 *
key=xxxxx
#域名 *
zone=xxx.xx
#可以自动获取
zone_id=
#记录类型
type=A
#可以自动获取
rec_id=
#记录名称 *
name=xxx
#IP 可自动获取
content=
ttl=120
#是否启用代理
proxied=false

if ["$content" = ""]
then
    if ["$type" = "AAAA"]
    then
        content=`curl -s http://ipv6.icanhazip.com`
    else
        content=`curl -s http://ipv4.icanhazip.com`
    fi
    if ["$content" = ""]
    then
        date  
        echo " 没有可用于设置记录值的 IP 地址。"
        exit 1
    else
        oldIP=`cat ip.txt`
        if ["$content" = "$oldIP"]
        then
            echo "IP 地址没有变化。"
            exit 0
        fi
    fi

fi
echo "IP Addr: "$content
echo "=========="
if ["$name" = ""]
then
    echo " 您必须提供要更改的记录的名称。"
    exit 1
fi

if ["$zone" = ""]
then
    echo " 您必须提供要更改的域名。"
    exit 1
fi

if ["$name" = "$zone"]
then
    hostname="$name"
else
    hostname="$name.$zone"
fi

if ["$key" = ""]
then
    echo " 您必须提供您的用户 API 令牌。"
    exit 1
fi

# Get the zone id for the entry we're trying to change if it's not provided
if ["$zone_id" = ""]
then
    echo "GET: https://api.cloudflare.com/client/v4/zones?name=$zone"
    zone_response_json=`curl -X GET "https://api.cloudflare.com/client/v4/zones?name=$zone" -H "Authorization: Bearer $key" -H "Content-Type: application/json"`
    # echo "zone_response_json: "$zone_response_json
    echo "=========="
    zone_id=`echo $zone_response_json | sed -E "s/.+\"result\":\[\{\"id\":\"([a-f0-9]+)\"[^\}]+$zone.+/\1/g"`
    if ["$zone_id" = ""]
    then
        echo " 在 Cloudflare 中找不到 DNS Zone ID,请确保它存在。"
        exit 1
    fi
fi

# Get the record id for the entry we're trying to change if it's not provided
if ["$rec_id" = ""]
then
    echo "GET: https://api.cloudflare.com/client/v4/zones/$zone_id/dns_records?name=$hostname"
    rec_response_json=`curl -X GET "https://api.cloudflare.com/client/v4/zones/$zone_id/dns_records?name=$hostname" -H "Authorization: Bearer $key" -H "Content-Type: application/json"`
    # echo "rec_response_json: "$rec_response_json
    echo "=========="
    rec_id=`echo $rec_response_json | sed -E "s/.+\"result\":\[\{\"id\":\"([a-f0-9]+)\"[^\}]+\$hostname\",\"type\":\"$type\"[^\}]+.+/\1/g"`
    if ["$rec_id" = ""]
    then
        echo " 在 Cloudflare 中找不到 DNS 记录,请确保它存在。"
        exit 1
    fi
fi

# Update the DNS record
echo "PUT: https://api.cloudflare.com/client/v4/zones/$zone_id/dns_records/$rec_id"
update_response=`curl -X PUT "https://api.cloudflare.com/client/v4/zones/$zone_id/dns_records/$rec_id" -H "Authorization: Bearer $key" -H "Content-Type: application/json" --data "{\"id\":\"$rec_id\",\"type\":\"$type\",\"name\":\"$hostname\",\"content\":\"$content\",\"ttl\":$ttl,\"proxied\":$proxied}"`
# echo "update_response: "$update_response
echo "=========="
success_val=`echo $update_response | sed -E "s/.+\"success\":(true|false).+/\1/g"`
if ["$success_val" = "true"]
then
    echo " 记录新成功。"
    echo "$content" > ip.txt
else
    echo " 记录更新失败。"
    exit 1
fi

正文完
 0
yxsoft
版权声明:本站原创文章,由 yxsoft 于2024-06-01发表,共计4930字。
转载说明:除特殊说明外本站文章皆由YXSoft发布,转载请注明出处。