changedns.py
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', "AKIDgJkHnMyfPEygt6LgdUEmDjM6xljdiagmaa") os.environ.setdefault('SecretKey', "CgTZxO6unAldfwHe7yAT0PpIkakvFZ6uaa") # 构造需要的参数 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) != 4: 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,配置环境
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.2
返回如下结果表示成功