时间:2023-02-07 05:16:01 | 来源:建站知识
时间:2023-02-07 05:16:01 来源:建站知识
情景说明yum -y install epel-release yum -y install python-pip
pip install aliyun-python-sdk-alidns pip install requests
# coding:utf-8from aliyunsdkcore import clientfrom aliyunsdkalidns.request.v20150109 import DescribeDomainsRequest, DescribeDomainRecordsRequest, / UpdateDomainRecordRequestimport json, urllib, re####################################################################################### 个人配置区域###################################################################################### 创建的 AccessKeyID = "xxxxx"Secret = "xxxxx"# 默认RegionId = "cn-hangzhou"# 你的域名DomainName = "abc.com"# 你想解析的二级域名,是一个列表,可以写多个HostNameList = ['test', 'hello', 'world']# 默认Types = "A"clt = client.AcsClient(ID, Secret, RegionId)def GetLocalIP(): """ 动态获取当前公司对外的公网 IP :return: """ IPInfo = urllib.urlopen("http://2018.ip138.com/ic.asp").read() IP = re.findall(r"ip:'(.*?)',", IPInfo)[0] return IPdef EditDomainRecord(HostName, RecordId, Types, IP): """ 更新域名 IP :param HostName: :param RecordId: :param Types: :param IP: :return: """ UpdateDomainRecord = UpdateDomainRecordRequest.UpdateDomainRecordRequest() UpdateDomainRecord.set_accept_format('json') UpdateDomainRecord.set_RecordId(RecordId) UpdateDomainRecord.set_RR(HostName) UpdateDomainRecord.set_Type(Types) UpdateDomainRecord.set_TTL('600') UpdateDomainRecord.set_Value(IP) UpdateDomainRecordJson = json.loads(clt.do_action_with_exception(UpdateDomainRecord)) print(UpdateDomainRecordJson)# 获取域名信息def GetAllDomainRecords(DomainName, Types, IP): DomainRecords = DescribeDomainRecordsRequest.DescribeDomainRecordsRequest() DomainRecords.set_accept_format('json') DomainRecords.set_DomainName(DomainName) DomainRecordsJson = json.loads(clt.do_action_with_exception(DomainRecords)) print(DomainRecordsJson['DomainRecords']['Record']) for HostName in HostNameList: for x in DomainRecordsJson['DomainRecords']['Record']: RR = x['RR'] Type = x['Type'] if RR == HostName and Type == Types: RecordId = x['RecordId'] print(RecordId)IP = GetLocalIP()EditDomainRecord(DomainName, RegionId, Types, IP)GetAllDomainRecords(DomainName, Types, IP)
【4】添加定时任务: 每两小时更新一次# 添加定时任务echo "* */2 * * * /usr/bin/python /scripts/ddns_update.py" >> /var/spool/cron/root
【5】配置完成,此时你可以手动执行检验该脚本是否能够正常的修改域名解析!至于后面的 Nginx 反向代理,网上的方法很多,这里就不一一缀诉!关键词:实现,固定,动态