def modify_record_name(domain_name, name, record_id, new_ip_addr): request = UpdateDomainRecordRequest() request.set_accept_format('json') request.set_RecordId(record_id) request.set_RR(name) request.set_Type("A") request.set_Value(new_ip_addr) with open(log_file, "a") as logfile: try: response = client.do_action_with_exception(request) sms.dns_record_update_sms( '.'.join([name, domain_name]).replace(".", "-"), new_ip_addr.replace('.', '-')) serverchan_ip_change_msg(name, "-".join(new_ip_addr.split("."))) logfile.write( "%s \t %s \n" % (str(time.asctime(time.localtime(time.time()))), response)) r_j = json.loads(response.decode()) request_id = r_j["RequestId"] record_id = r_j["RecordId"] insert_change(request_id=request_id, record_id=record_id, domain=domain_name, name=name, ip_addr=new_ip_addr) exist_and_count(domain_name, name, record_id) OK_response("200.3 OK") except Exception as e: logfile.write("%s \t %s \n" % (str(time.asctime(time.localtime( time.time()))), traceback.print_exc())) code = "503.4" serverchan_error_msg(domain_name, code, e) error_response(code)
def aliyun_update_subdomain(client, subdomain, ipaddress, addtype, recordid): request = None response = None request = UpdateDomainRecordRequest() request.set_accept_format('json') tmp = subdomain.partition('.') request.set_RecordId(recordid) request.set_RR(tmp[0]) if addtype == 'ipv4': request.set_Type('A') else: request.set_Type('AAAA') request.set_Value(ipaddress) try: response = client.do_action_with_exception(request) except ServerException as e: print('Server Error! Domain: {}, Code: {}.'.format( subdomain, e.get_error_code())) print(e.get_error_msg()) return None except ClientException as e: print('Client Error! Domain: {}, Code: {}.'.format( subdomain, e.get_error_code())) print(e.get_error_msg()) return None return json.loads(str(response, encoding='utf-8'))
def update_domain(domain, current_ip): request = DescribeSubDomainRecordsRequest() sub_domain = domain['RR'] + '.' + domain['DomainName'] print(sub_domain) request.set_SubDomain(sub_domain) response = client.do_action_with_exception(request) r = json.loads(response) print(datetime.datetime.now(), r) records = r['DomainRecords']['Record'] if records: # 存在记录 record = records[0] if record['Value'] != current_ip: update_request = UpdateDomainRecordRequest() # 主机记录 update_request.set_RR(domain['RR']) # 记录ID update_request.set_RecordId(record['RecordId']) # 主机记录值设为当前主机IP update_request.set_Value(current_ip) # 解析记录类型 update_request.set_Type(domain['Type']) update_response = client.do_action_with_exception(update_request) print(json.loads(update_response))
def __setRecordValue(self, record_value): self.__requireParmSet() # creat request,set parm if self.has_remote_record: if record_value == self.remote_record_value: print "current local internet ip is " + record_value + ", which same as the remote record [SKIP]" return True else: print "current local internet ip is " + record_value + ", which different from the remote record " + self.remote_record_value + " [UPDATE]" request = UpdateDomainRecordRequest() request.set_RecordId(self.record_id) request.set_RR(self.sub_domain_name) request.set_Type(self.record_type) request.set_Value(record_value) else: print "current local internet ip is " + record_value + ", the remote sub domain record do not exist ! [ADD]" request = AddDomainRecordRequest() request.set_DomainName(self.domain_name) request.set_RR(self.sub_domain_name) request.set_Type(self.record_type) request.set_Value(record_value) # execute res = self.__tryExecute(request) if res[0]: if self.has_remote_record: print "update [OK]" else: print "add [OK]" else: if self.has_remote_record: print "update [ERROR] " + res[1] else: print "add [ERROR] " + res[1]
def update_domain_record(client, host, domain, _type, ip_address, record_id): r""" Update domain information :param client: client object :param host: e.g. 'www' :param domain: e.g. 'dongs.xyz' :param _type: e.g. 'A', 'CNAME' :param ip_address: e.g. '127.0.0.1' :param record_id: record id from Aliyun """ try: request = UpdateDomainRecordRequest() request.set_accept_format('json') request.set_Value(ip_address) request.set_Type(_type) request.set_RR(host) request.set_RecordId(record_id) response = client.do_action_with_exception(request) logging.info( "Successfully updated domain record: {_host}.{_domain} ({__type} record) to {_ip_address}." .format(_host=host, _domain=domain, __type=_type, _ip_address=ip_address)) logging.debug(response) except Exception as e: logging.error( "Failed to update domain record: {_host}.{_domain} ({__type} record) to {_ip_address}." .format(_host=host, _domain=domain, __type=_type, _ip_address=ip_address)) logging.error(e)
def updateDomainRecord(client, rr, domain, ip, type): records = getRecords(client, rr, domain) record_id = None for record in records: if record['RR'] == rr and record['DomainName'] == domain and record[ 'Type'] == type: record_id = record['RecordId'] avail_ip = record['Value'] avail_record = record break if record_id is None: print("不存在记录,正在添加记录") addDomainRecord(client, rr, domain, ip, type) else: print("存在记录:" + str(avail_record)) if avail_ip == ip: print("ip未改变,不更改记录,原ip=" + avail_ip + ",现ip=" + ip) else: print("ip已改变,准备更改记录,原ip=" + avail_ip + ",现ip=" + ip) request = UpdateDomainRecordRequest() request.set_action_name("UpdateDomainRecord") request.set_RR(rr) request.set_RecordId(record_id) request.set_Type(type) request.set_Value(ip) request.set_TTL(600) request.set_accept_format('json') response = client.do_action_with_exception(request) print(str(response, encoding='utf-8')) return json.loads(response)['RecordId']
def update(self, record_id, domain_type, rr, value): """ :param record_id: *************** :param domain_type: A :param rr: hk :param value: *************** :return: """ request = UpdateDomainRecordRequest() request.set_accept_format('json') request.set_Value(value) request.set_Type(domain_type) request.set_RR(rr) request.set_RecordId(record_id) try: response = self.client.do_action_with_exception(request) if ResponseUtil.success(response): record_id = response_record_id(response) if record_id: logging.info( "Successfully update domain parse information for %s to %s" % (record_id, value)) return True, None else: return False, str(response, encoding='utf-8') except Exception as e: logging.error( "Failed to add sub domain parse information for %s to %s" % (record_id, value), e) return False, e
def refresh_domain_records(): """ 更新域名 DNS """ try: ip = get_ip() client = AcsClient(ALI_AK, ALI_SECRET, ALI_REGION_ID) req_get_records = DescribeDomainRecordsRequest() req_get_records.set_accept_format('json') req_get_records.set_DomainName(ALI_DOMAIN) response = json.loads(client.do_action_with_exception(req_get_records)) msg = "" for record in response["DomainRecords"]["Record"]: recode_ip = record["Value"] if recode_ip != ip: req_set_record = UpdateDomainRecordRequest() req_set_record.set_RecordId(record["RecordId"]) req_set_record.set_RR(record["RR"]) req_set_record.set_Type(record["Type"]) req_set_record.set_TTL(record["TTL"]) req_set_record.set_Value(ip) req_set_record.set_accept_format('json') client.do_action_with_exception(req_set_record) info = str.format("{0}.{1} {2} --> {3}", record["RR"], ALI_DOMAIN, recode_ip, ip) logging.info(info) msg += info + "\n" if msg != "": send_mail(msg) except: logging.exception("Exception occurred")
def editByDomainRecords(_client: AcsClient, _Record_data: dict, _value: str): """ 修改解析记录 :param _client: :param _Record_data: 通过获取列表得来的解析信息 :param _value: 要修改的记录值 :return: """ request = UpdateDomainRecordRequest() request.set_accept_format('json') request.set_RecordId(_Record_data.get('RecordId')) request.set_RR(_Record_data.get("RR")) request.set_Type(_Record_data.get('Type')) request.set_Value(_value) try: response = _client.do_action_with_exception(request) except ServerException as exce_entity: if exce_entity.message == 'The DNS record already exists.': err_msg = "{RR} 解析已被修改为 {value} ,无需修改.".format( RR=_Record_data.get("RR"), value=_value) else: err_msg = "未知异常; 阿里云异常消息原文: " + exce_entity.message raise Exception(err_msg) return json.loads(str(response, encoding='utf-8'))
def update(self, rr, value): """ Update the record for specify subdomain. :param rr: subdomain :param value: ip address """ recordid = str() records = self.list() for record in records: if record['RR'] == rr: recordid = record['RecordId'] if recordid == '': logging.debug( f'AliyunDNS::update: record for {rr + "." + self.domain} does not exist, ignore' ) return request = UpdateDomainRecordRequest() request.set_accept_format('json') request.set_RecordId(f'{recordid}') request.set_RR(f'{rr}') request.set_Type(f'A') request.set_Value(f'{value}') response = str(self.client.do_action_with_exception(request), encoding='utf-8') logging.debug(json.loads(response))
def Dns(_rr): request = UpdateDomainRecordRequest() request.set_accept_format('json') logging.info('Update %s.%s DNS' % (_rr, domain)) logging.info("Getting RecordId....") record = getRecord(_rr) recordId = record['RecordId'] logging.info("RecordId:%s" % recordId) logging.info("Getting IP....") ip = getIP() logging.info("IP:%s" % ip) if ip == record['Value']: return "IP consistency does not need to be updated." logging.info("Updating DNS....") request.set_Value(ip) request.set_RecordId(recordId) request.set_RR(_rr) request.set_Type("A") response = client.do_action_with_exception(request) return json.loads(response.decode("UTF-8"))
def update_dns_record(ali_api_id, ali_api_secret, ip, record_id, request_sub_domain): client = AcsClient(ali_api_id, ali_api_secret, 'cn-hangzhou') request = UpdateDomainRecordRequest() request.set_accept_format('json') request.set_RecordId(record_id) request.set_RR(request_sub_domain) request.set_Type("A") request.set_Value(ip) try: response = client.do_action_with_exception(request) except (ClientException, ServerException) as reason: log.error("update dns failed. do_action_with_exception") return False result = str(response, encoding='utf-8') log.info("update dns record result: \r\n" + result) json_obj = json.loads(result) if len(json_obj['RecordId']) == 0: log.error("update dns failed. error code " + json_obj['code']) return False return True
def update_ddns_ip(ip, a_type, rr, record_id): request = UpdateDomainRecordRequest() request.set_accept_format('json') request.set_Value(ip) request.set_Type(a_type) request.set_RR(rr) request.set_RecordId(record_id) client.do_action_with_exception(request)
def modify_domain_record(self, record_id, rr, record_type, value): request = UpdateDomainRecordRequest() request.set_Value(value) request.set_RecordId(record_id) request.set_accept_format('JSON') request.set_RR(rr) request.set_Type(record_type) return json.loads(self.clt.do_action_with_exception(request))
def updateDNS(recordID, IP, RR): request = UpdateDomainRecordRequest() request.set_accept_format('json') request.set_RecordId(recordID) request.set_Value(IP) request.set_Type("A") request.set_RR(RR) response = client.do_action_with_exception(request) return response
def update_record(x, y): request = UpdateDomainRecordRequest() request.set_accept_format('json') request.set_RecordId(x) request.set_RR(RR) request.set_Type(Type) request.set_Value(y) response = client.do_action_with_exception(request) return str(response, encoding='utf-8')
def updateRecord(RecordId: str, RR: str, Type: str, Value: str) -> str: request = UpdateDomainRecordRequest() request.set_accept_format('json') request.set_Value(Value) request.set_Type(Type) request.set_RR(RR) request.set_RecordId(RecordId) response = client.do_action_with_exception(request) return str(response, encoding='utf-8')
def update_dns_resolve(self, ip, recordId, type): request = UpdateDomainRecordRequest() request.set_accept_format('json') request.set_Value(ip) request.set_Type(type) request.set_RR(self.get_data()['tertiaryDomain']) request.set_RecordId(recordId) response = self.client.do_action_with_exception(request) logger.info("域名解析修改成功:%s",ip)
def editParsingRecords(ip): request = UpdateDomainRecordRequest() request.set_accept_format('json') request.set_RecordId(recordId) request.set_RR("k") request.set_Type("A") request.set_Value(ip) response = client.do_action_with_exception(request) ylog(str(response, encoding='utf-8'))
def update(RecordId, RR, Type, Value): # 修改域名解析记录 from aliyunsdkalidns.request.v20150109.UpdateDomainRecordRequest import UpdateDomainRecordRequest request = UpdateDomainRecordRequest() request.set_accept_format('json') request.set_RecordId(RecordId) request.set_RR(RR) request.set_Type(Type) request.set_Value(Value) response = client.do_action_with_exception(request)
def update(RecordId, RR, Type, Value): from aliyunsdkalidns.request.v20150109.UpdateDomainRecordRequest import UpdateDomainRecordRequest request = UpdateDomainRecordRequest() request.set_accept_format("json") request.set_RecordId(RecordId) request.set_RR(RR) request.set_Type(Type) request.set_Value(Value) DDNSUnit.response(request)
def modify_record(self, old_rr=None, new_rr=None, type=None, line=None, value=None, ttl=None, mx=None): """ 修改记录 :return: True|False """ try: record_id = self.get_record_id(old_rr) if not record_id: return False if type == "显示URL": type = "REDIRECT_URL" if type == "隐性URL": type = "FORWARD_URL" client = AcsClient(self.key, self.secret, 'cn-hangzhou') request = UpdateDomainRecordRequest() request.set_accept_format('json') request.set_RecordId(record_id) request.set_RR(new_rr + "." + self.prefix_domain) request.set_Type(type) request.set_Value(value) request.set_Line(self.record_line(value=line)) request.set_TTL(600 if int(ttl) < 600 else int(ttl)) if type == "MX": request.set_Priority(mx) try: response = client.do_action_with_exception(request) response = json.loads(response) if "Code" in response.keys(): print(F"modify record failed: {response}") print(response) return False print(F"modify record success: [{old_rr} -> {new_rr}]") return True except ServerException as e: print(F"No changes for record: {e}") return True except Exception: print(F"modify record unknown exception") print(traceback.format_exc()) return False
def update_record(client, record, ip): request = UpdateDomainRecordRequest() request.set_accept_format('json') request.set_RecordId(record['RecordId']) request.set_RR(record['RR']) request.set_Type("A") request.set_Value(ip) response = client.do_action_with_exception(request) r = response is not None and 'RequestId' in to_dict(response) print(' - [{}] {}'.format(r, record['RR']))
def UpdateRecords(RecordID, ResolutionRecord, RecordType, RecordValue): request = UpdateDomainRecordRequest() request.set_accept_format('json') request.set_RecordId(RecordID) request.set_RR(ResolutionRecord) request.set_Type(RecordType) request.set_Value(RecordValue) response = client.do_action_with_exception(request) records = json.loads(response) print(records)
def update_record(self, index: int, record_id: str, ip: str): """ update domain item to target ip address """ req = UpdateDomainRecordRequest() req.set_RR(self._config.rr_list[index]) req.set_RecordId(record_id) req.set_Type('A') req.set_Value(ip) req.set_Line('default') resp = self._client.do_action_with_exception(req).decode() logger.debug(f'update domain response: {resp}')
def update_domain_record(client, ip, record): """update specific domain record""" request = UpdateDomainRecordRequest() request.set_accept_format("json") request.set_RecordId(record["RecordId"]) request.set_RR(record["RR"]) request.set_Type(record["Type"]) request.set_Value(ip) return client.do_action_with_exception(request)
def update_domain_record(client, rid, value): """update specific domain record""" request = UpdateDomainRecordRequest() request.set_accept_format("json") request.set_RecordId(rid) request.set_RR("_acme-challenge") request.set_Type("TXT") request.set_Value(value) return client.do_action_with_exception(request)
def set_dns_record(client: AcsClient, record: dict, value: str): request = UpdateDomainRecordRequest() request.set_accept_format("json") request.set_RecordId(record["RecordId"]) request.set_RR(record["RR"]) request.set_Type(record["Type"]) request.set_Value(value) client.do_action_with_exception(request) print("Updated [%s]:\n%s %s %s\n" % ((record["RR"] + "." + record["DomainName"]), record["RecordId"], record["Type"], value))
def aliupdate(gy, sy, _recordid, _rr, _type, _value): credentials = AccessKeyCredential(gy, sy) client = AcsClient(region_id='cn-hangzhou', credential=credentials) request = UpdateDomainRecordRequest() request.set_accept_format('json') request.set_RecordId(_recordid) request.set_RR(_rr) request.set_Type(_type) request.set_Value(_value) response = client.do_action_with_exception(request) # python2: print(response) print(str(response, encoding='utf-8'))
def setDnsRecord(self, ip): request = UpdateDomainRecordRequest() request.set_accept_format('json') request.set_Type(self.config['Type']) request.set_Value(ip) request.set_RR(self.config['RR']) request.set_RecordId(self.config['RecordId']) response = self.client.do_action_with_exception(request) # python2: print(response) print(str(response, encoding='utf-8'))