You are viewing an old version of this page. View the current version.

Compare with Current View Page History

Version 1 Next »

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_name = sys.argv[2] if len(sys.argv) > 2 else '@'

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.DescribeRecordRequest()
    domain_id = get_domain_id()
    record_id = get_record_id() 
    params = {
        "Domain": domain_name,
        "DomainId": domain_id,
        "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.DescribeRecord(req)
    response_dict = json.loads(resp.to_json_string())
    value = response_dict['RecordInfo']['Value']
    print(f"{value}")

except TencentCloudSDKException as err:
    print(err)

使用方法:

将脚本命名为listdns.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 listdns.py zhengyu1992.cn abc

返回如下结果表示成功

  • No labels