这个脚本只需要设定SecretId和SecretKey,domain_name、record_name、record_ip通过脚本参数指定
Code Block | ||
---|---|---|
| ||
import os
import sys
import json
import types
from tencentcloud.common import credential
from tencentcloud.common.profile.client_profile import ClientProfile
from tencentcloud.common.profile.http_profile import HttpProfile
from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException
from tencentcloud.dnspod.v20210323 import dnspod_client, models
# 设置环境变量
os.environ.setdefault('SecretId', "你的SecretId")
os.environ.setdefault('SecretKey', "你的SecretKey")
# 构造需要的参数
cred = credential.Credential(os.getenv('SecretId'), os.getenv('SecretKey'))
httpProfile = HttpProfile()
httpProfile.endpoint = "dnspod.tencentcloudapi.com"
clientProfile = ClientProfile()
clientProfile.httpProfile = httpProfile
client = dnspod_client.DnspodClient(cred, "", clientProfile)
domain_name = sys.argv[1]
record_name = sys.argv[2]
record_ip = sys.argv[3]
def get_domain_id():
params = {
"Domain": domain_name
}
try:
req = models.DescribeDomainListRequest()
req.from_json_string(json.dumps(params))
resp = client.DescribeDomainList(req)
response_dict = json.loads(resp.to_json_string())
return response_dict['DomainList'][0]['DomainId']
except IndexError:
print("No domain found with name: {}".format(domain_name))
sys.exit(1)
except TencentCloudSDKException as err:
print("An error occurred while fetching domain ID: {}".format(err))
sys.exit(1)
def get_record_id():
params = {
"Domain": domain_name,
"Subdomain": record_name
}
try:
req = models.DescribeRecordListRequest()
req.from_json_string(json.dumps(params))
resp = client.DescribeRecordList(req)
response_dict = json.loads(resp.to_json_string())
if len(response_dict['RecordList']) != 1:
print("Expected exactly one record, but found: {}".format(len(response_dict['RecordList'])))
sys.exit(1)
return response_dict['RecordList'][0]['RecordId']
except TencentCloudSDKException as err:
print("An error occurred while fetching record ID: {}".format(err))
sys.exit(1)
try:
req = models.ModifyRecordRequest()
domain_id = get_domain_id()
record_id = get_record_id()
params = {
"Domain": domain_name,
"DomainId": domain_id,
"SubDomain": record_name,
"RecordType": "A",
"RecordLine": "默认",
"RecordLineId": "0",
"Value": record_ip,
"MX": 0,
"TTL": 600,
"Weight": 10,
"Status": "ENABLE",
"RecordId": record_id
}
req.from_json_string(json.dumps(params))
if len(sys.argv) != 3:
print("Usage: {} <domain_name> <record_name> <new_ip>".format(sys.argv[0]))
sys.exit(1)
resp = client.ModifyRecord(req)
print("Record successfully updated.")
except TencentCloudSDKException as err:
print(err) |
使用方法:
将脚本命名为changedns.py
Code Block |
---|
yum install python3
tee /etc/pip.conf << EOF
[global]
index-url = http://mirrors.aliyun.com/pypi/simple/
[install]
trusted-host=mirrors.aliyun.com
EOF
pip3 install tencentcloud-sdk-python-dnspod
export SecretId=AKIDgJkH******
export SecretKey=CgTZxO6******
python3 changedns.py zhengyu1992.cn abc 1.1.1.1 |
返回如下结果表示成功
----------------------------------------------
...
腾讯云默认免费API次数100万次,默认限制200次/秒
接口请求域名: dnspod.tencentcloudapi.com
访问腾讯云官网获取域名相关ID信息
1.获取域名ID在右边的输出结果中,名为DomainId的字段
https://console.cloud.tencent.com.cn/api/explorer?Product=dnspod&Version=2021-03-23&Action=DescribeDomainList
2.获取域名解析记录ID在右边的输出结果中,找到你想要修改的记录值名为RecordId的字段
3.修改域名解析记录
4.获取APIKEY