def update_domain(domain, new_ip, rrs): cli = client.AcsClient(KEY, SECRET, REGION) req = DescribeDomainRecordsRequest() req.set_DomainName(domain) req.set_accept_format('json') resp = cli.do_action(req) records = json.loads(resp)['DomainRecords']['Record'] for rec in records: if rec['Type'] != 'A': continue if rec['RR'] not in rrs: continue if rec['Value'] == new_ip: print_('Not need refresh, same as now.', new_ip, rec['RR']) continue req = UpdateDomainRecordRequest() req.set_RecordId(rec['RecordId']) req.set_Value(new_ip) req.set_RR(rec['RR']) req.set_Type(rec['Type']) req.set_accept_format('json') resp = cli.do_action(req) result = json.loads(resp) if 'Code' in result: raise Exception(result['Message'])
def update(): print("start time:" + time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())) new_ip = get_ip() with open("./config.json", 'r') as load_f: load_dict = json.load(load_f) accessKeyId = load_dict['AccessKeyId'] accessKeySecret = load_dict['AccessKeySecret'] domain = load_dict['domain'] secondDomain = load_dict['second-level-domain'] client = AcsClient(accessKeyId, accessKeySecret, 'cn-hangzhou') request = DescribeDomainRecordsRequest() request.set_accept_format('json') request.set_DomainName("haifeng.ink") response = client.do_action_with_exception(request) records = json.loads(response)['DomainRecords']['Record'] old_record_id = '' old_ip = '' for record in records: if record['RR'] == secondDomain: old_ip = record['Value'] old_record_id = record['RecordId'] break if old_record_id != '' and old_ip != new_ip: request = UpdateDomainRecordRequest() request.set_accept_format('json') request.set_RecordId(old_record_id) request.set_RR(secondDomain) request.set_Type('A') request.set_Value(new_ip) client.do_action_with_exception(request) print('更新ip:' + new_ip) elif old_record_id == '': request = AddDomainRecordRequest() request.set_accept_format('json') request.set_DomainName(domain) request.set_RR(secondDomain) request.set_Type('A') request.set_Value(new_ip) response = client.do_action_with_exception(request) # python2: print(response) print(str(response, encoding='utf-8')) print('添加新ip:' + new_ip) else: print('不需要更新') print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
except: print( "unexcepted error, exit." ) sys.exit(1) # parse response: data = json.loads(response) record = data["DomainRecords"]["Record"]; # recrucive all dns record and find the RecordId of target pre-domain. for single_record in record: if ( single_record["RR"] == domain_pre ): print( "==== get domain=[%s] RR=[%s] RecordId=[%s] Type=[%s] Value=[%s]." % (domain, single_record["RR"], single_record["RecordId"], single_record["Type"], single_record["Value"] ) ) # second step: call aliyun api to update the target record. request = UpdateDomainRecordRequest() request.set_accept_format('json') request.set_RR( domain_pre ) request.set_RecordId( single_record["RecordId"] ) if record_type == 1: request.set_Type( qiniu_cname[0] ) request.set_Value( qiniu_cname[1] ) else: request.set_Type( original_host[0] ) request.set_Value( original_host[1] ) try: response = client.do_action_with_exception(request) except ServerException as srv_ex: # if response.has_key('Code') or response.has_key('Message'): print("Error: Code=[%s] Message=[%s], exit." % (srv_ex.error_code, srv_ex.message) ) sys.exit(1)