这个脚本只需要设定SecretId和SecretKey,domain_name、record_name、record_ip通过脚本参数指定
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', "你的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
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
返回如下结果表示成功
----------------------------------------------
#!/bin/bash webhookurl=https://oapi.dingtalk.com/robot/send?access_token=0ccbf717fb61bef56d0cc29d25b5d7e79da719997c5d0dca55f709bd54e5ef32 time=$(date) #currentdnsip=$(dig +short fnsdisk.zhengyu1992.cn A) currentdnsip=$(python3 listfndsdisk.py) currentserverip=$(timeout 5 curl -s ifconfig.me) message="{ \"msgtype\": \"markdown\", \"markdown\": { \"title\": \"DNS记录修改\", \"text\": \"#### DNS记录修改\n\n- 时间: **$time**\n- 域名: **fnsdisk.zhengyu1992.cn**\n- IP:**$currentserverip**\n\" } }" if [ "$currentdnsip" = "$currentserverip" ]; then echo "The current DNS IP and server IP are the same: $currentdnsip" exit 0 else echo "The current DNS IP ($currentdnsip) does not match the server IP ($currentserverip). Updating DNS..." python3 changefndsdisk.py "$currentserverip" curl -s "${webhookurl}" -H 'Content-Type: application/json' -d "${message}" fi