腾讯云DDNS脚本
2025年10月31日
114字

说明

脚本主体来自腾讯云官方的api工具

前置步骤

获取域名的recordID

打开腾讯云的域名页面,并选择你要操作的域名

一步直达

点击右侧设置按钮

点击右侧设置按钮

勾选记录ID

勾选记录ID

脚本

请确定主机上安装了curlping,否则会导致脚本无法运行

#!/bin/sh
secret_id="" # 填写你的SecretID
secret_key="" # 填写你的SecretKey
domain="" # 填写你的域名
record_id=0 # 填写你的记录ID
sub_domain="" # 填写你的主机名
updateIP(){
service="dnspod"
host="dnspod.tencentcloudapi.com"
region=""
token=""
action="ModifyRecord"
version="2021-03-23"
algorithm="TC3-HMAC-SHA256"
timestamp=$(date +%s)
date=$(date -u -d @$timestamp +"%Y-%m-%d")
payload="{\"Domain\":\"$domain\",\"RecordType\":\"A\",\"RecordLine\":\"默认\",\"Value\":\"$new_ip\",\"RecordId\":$record_id,\"SubDomain\":\"$sub_domain\"}"
http_request_method="POST"
canonical_uri="/"
canonical_querystring=""
canonical_headers="content-type:application/json; charset=utf-8\nhost:$host\nx-tc-action:$(echo $action | awk '{print tolower($0)}')\n"
signed_headers="content-type;host;x-tc-action"
hashed_request_payload=$(echo -n "$payload" | openssl sha256 -hex | awk '{print $2}')
canonical_request="$http_request_method\n$canonical_uri\n$canonical_querystring\n$canonical_headers\n$signed_headers\n$hashed_request_payload"
credential_scope="$date/$service/tc3_request"
hashed_canonical_request=$(printf "$canonical_request" | openssl sha256 -hex | awk '{print $2}')
string_to_sign="$algorithm\n$timestamp\n$credential_scope\n$hashed_canonical_request"
secret_date=$(printf "$date" | openssl sha256 -hmac "TC3$secret_key" | awk '{print $2}')
secret_service=$(printf $service | openssl dgst -sha256 -mac hmac -macopt hexkey:"$secret_date" | awk '{print $2}')
secret_signing=$(printf "tc3_request" | openssl dgst -sha256 -mac hmac -macopt hexkey:"$secret_service" | awk '{print $2}')
signature=$(printf "$string_to_sign" | openssl dgst -sha256 -mac hmac -macopt hexkey:"$secret_signing" | awk '{print $2}')
authorization="$algorithm Credential=$secret_id/$credential_scope, SignedHeaders=$signed_headers, Signature=$signature"
curl -XPOST "https://$host" -d "$payload" -H "Authorization: $authorization" -H "Content-Type: application/json; charset=utf-8" -H "Host: $host" -H "X-TC-Action: $action" -H "X-TC-Timestamp: $timestamp" -H "X-TC-Version: $version" -H "X-TC-Region: $region" -H "X-TC-Token: $token"
}
new_ip=`curl -s http://ip.glh.red/`
old_ip=`ping home.bitnet.fun -c 1 | sed '1{s/[^(]*(//;s/).*//;q}'`
#如果老的IP与获取的新的IP不一致则请求 腾讯云 更新IP
if [[ "${new_ip}" != "" && "${new_ip}" != "${old_ip}" ]]
then
echo -e "`date`\t\c"
echo -e "${old_ip} > ${new_ip}\c"
updateIP
echo "IP更新成功"
else
echo "IP未发生变化,无需更新"
fi
——END——
# Linux
# 运维
# 服务器
# 腾讯云
作者信息:Gaze.
发表于:2025年10月31日
本文标题: 腾讯云DDNS脚本