阿里云、腾讯云—-域名DDNS云解析到动态IP

DDNS(Dynamic Domain Name Server,动态域名服务)是将用户的动态IP地址映射到一个固定的域名解析服务上。

本教程详细介绍如何使用阿里云Alibaba Cloud SDK for Java实现动态域名解析(DDNS),腾讯云教程参见本文底部,此外这两大平台还支持API方式更新DNS,见文章底部

前提条件

在使用本教程前,请确保已完成以下操作:

  • 使用Alibaba Cloud SDK for Java,您需要一个阿里云账号和访问密钥(AccessKey)。 请在阿里云控制台中的AccessKey管理页面上创建和查看您的AccessKey。
  • 确保您已经安装了Alibaba Cloud SDK for Java,准确的SDK版本号,请参见阿里云开发工具包(SDK)
<dependencies> <!-- https://mvnrepository.com/artifact/com.aliyun/aliyun-java-sdk-core --> <dependency> <groupId>com.aliyun</groupId> <artifactId>aliyun-java-sdk-core</artifactId> <version>4.4.3</version> </dependency> <!-- https://mvnrepository.com/artifact/com.aliyun/aliyun-java-sdk-alidns --> <dependency> <groupId>com.aliyun</groupId> <artifactId>aliyun-java-sdk-alidns</artifactId> <version>2.0.10</version> </dependency> </dependencies>

代码示例

本文操作示例主要以代码形式体现,具体代码如下:

import com.aliyuncs.DefaultAcsClient; import com.aliyuncs.IAcsClient; import com.aliyuncs.alidns.model.v20150109.DescribeDomainRecordsRequest; import com.aliyuncs.alidns.model.v20150109.DescribeDomainRecordsResponse; import com.aliyuncs.alidns.model.v20150109.UpdateDomainRecordRequest; import com.aliyuncs.alidns.model.v20150109.UpdateDomainRecordResponse; import com.aliyuncs.exceptions.ClientException; import com.aliyuncs.profile.DefaultProfile; import com.google.gson.Gson; import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * 动态域名解析 */ public class DDNS { /** * 获取主域名的所有解析记录列表 */ private DescribeDomainRecordsResponse describeDomainRecords(DescribeDomainRecordsRequest request, IAcsClient client){ try { // 调用SDK发送请求 return client.getAcsResponse(request); } catch (ClientException e) { e.printStackTrace(); // 发生调用错误,抛出运行时异常 throw new RuntimeException(); } } /** * 获取当前主机公网IP */ private String getCurrentHostIP(){ // 这里使用jsonip.com第三方接口获取本地IP String jsonip = "https://jsonip.com/"; // 接口返回结果 String result = ""; BufferedReader in = null; try { // 使用HttpURLConnection网络请求第三方接口 URL url = new URL(jsonip); HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); urlConnection.setRequestMethod("GET"); urlConnection.connect(); in = new BufferedReader(new InputStreamReader( urlConnection.getInputStream())); String line; while ((line = in.readLine()) != null) { result += line; } } catch (Exception e) { e.printStackTrace(); } // 使用finally块来关闭输入流 finally { try { if (in != null) { in.close(); } } catch (Exception e2) { e2.printStackTrace(); } } // 正则表达式,提取xxx.xxx.xxx.xxx,将IP地址从接口返回结果中提取出来 String rexp = "(\\d{1,3}\\.){3}\\d{1,3}"; Pattern pat = Pattern.compile(rexp); Matcher mat = pat.matcher(result); String res=""; while (mat.find()) { res=mat.group(); break; } return res; } /** * 修改解析记录 */ private UpdateDomainRecordResponse updateDomainRecord(UpdateDomainRecordRequest request, IAcsClient client){ try { // 调用SDK发送请求 return client.getAcsResponse(request); } catch (ClientException e) { e.printStackTrace(); // 发生调用错误,抛出运行时异常 throw new RuntimeException(); } } private static void log_print(String functionName, Object result) { Gson gson = new Gson(); System.out.println("-------------------------------" + functionName + "-------------------------------"); System.out.println(gson.toJson(result)); } public static void main(String[] args) { // 设置鉴权参数,初始化客户端 DefaultProfile profile = DefaultProfile.getProfile( "cn-qingdao",// 地域ID "your-access-key-id",// 您的AccessKey ID "your-access-key-secret");// 您的AccessKey Secret IAcsClient client = new DefaultAcsClient(profile); DDNS ddns = new DDNS(); // 查询指定二级域名的最新解析记录 DescribeDomainRecordsRequest describeDomainRecordsRequest = new DescribeDomainRecordsRequest(); // 主域名 describeDomainRecordsRequest.setDomainName("example.com"); // 主机记录 describeDomainRecordsRequest.setRRKeyWord("ddnstest"); // 解析记录类型 describeDomainRecordsRequest.setType("A"); DescribeDomainRecordsResponse describeDomainRecordsResponse = ddns.describeDomainRecords(describeDomainRecordsRequest, client); log_print("describeDomainRecords",describeDomainRecordsResponse); List<DescribeDomainRecordsResponse.Record> domainRecords = describeDomainRecordsResponse.getDomainRecords(); // 最新的一条解析记录 if(domainRecords.size() != 0 ){ DescribeDomainRecordsResponse.Record record = domainRecords.get(0); // 记录ID String recordId = record.getRecordId(); // 记录值 String recordsValue = record.getValue(); // 当前主机公网IP String currentHostIP = ddns.getCurrentHostIP(); System.out.println("-------------------------------当前主机公网IP为:"+currentHostIP+"-------------------------------"); if(!currentHostIP.equals(recordsValue)){ // 修改解析记录 UpdateDomainRecordRequest updateDomainRecordRequest = new UpdateDomainRecordRequest(); // 主机记录 updateDomainRecordRequest.setRR("ddnstest"); // 记录ID updateDomainRecordRequest.setRecordId(recordId); // 将主机记录值改为当前主机IP updateDomainRecordRequest.setValue(currentHostIP); // 解析记录类型 updateDomainRecordRequest.setType("A"); UpdateDomainRecordResponse updateDomainRecordResponse = ddns.updateDomainRecord(updateDomainRecordRequest, client); log_print("updateDomainRecord",updateDomainRecordResponse); } } } }

运行结果

正确运行结果类似如下:

-------------------------------describeDomainRecords------------------------------- { "requestId": "6AE588B9-FAFA-45FE-8FBE-CACB196D3539", "totalCount": 1, "pageNumber": 1, "pageSize": 20, "domainRecords": [ { "domainName": "example.com", "recordId": "1846657850481xxxx", "rR": "ddnstest", "type": "A", "value": "1.1.1.1", "tTL": 600, "line": "default", "status": "ENABLE", "locked": false, "weight": 1 } ] } -------------------------------当前主机公网IP为:123.123.123.123------------------------------- -------------------------------updateDomainRecord------------------------------- { "requestId": "08DC052F-7DF7-4451-8214-8489EC15D96A", "recordId": "1846657850481xxxx" }

转载自 阿里云SDK:

阿里云API 更新DNS:

腾讯云API 更新DNS :

原文链接:https://blog.csdn.net/yfx000/article/details/122777011?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522168449620216800222830974%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fblog.%2522%257D&request_id=168449620216800222830974&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~blog~first_rank_ecpm_v1~times_rank-11-122777011-null-null.blog_rank_default&utm_term=NAS%E3%80%81%E7%BE%A4%E6%99%96%E3%80%81%E9%98%BF%E9%87%8C%E4%BA%91%E3%80%81%E5%9F%9F%E5%90%8D%E8%A7%A3%E6%9E%90%E3%80%81%E5%86%85%E7%BD%91%E7%A9%BF%E9%80%8F%E3%80%81ipv6%E3%80%81ddns%E3%80%81%E8%BD%BB%E9%87%8F%E7%BA%A7%E4%BA%91%E6%9C%8D%E5%8A%A1%E5%99%A8%E3%80%81%E9%93%81%E5%A8%81%E9%A9%AC%E3%80%81%E5%A8%81%E8%81%94%E9%80%9A%E3%80%81DSM%E3%80%81DSM6.0%E3%80%81%E7%BE%A4%E6%99%96nas%E3%80%81%E4%BA%91%E6%9C%8D%E5%8A%A1%E5%99%A8%E3%80%81%E8%9C%97%E7%89%9B%E6%98%9F%E9%99%85%E3%80%81%E9%BB%91%E7%BE%A4%E6%99%96%E3%80%81docker%E3%80%81%E5%AE%B9%E5%99%A8%E9%95%9C%E5%83%8F%E3%80%81%E5%9F%9F%E5%90%8D%E6%B3%A8%E5%86%8C%E3%80%81%E5%AE%9D%E5%A1%94%E3%80%81%E5%8F%8D%E5%90%91%E4%BB%A3%E7%90%86%E3%80%81nginx%E3%80%81frp%E3%80%81%E5%8A%A8%E6%80%81%E5%9F%9F%E5%90%8D%E8%A7%A3%E6%9E%90

© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享