阿里云IPV6动态域名解析java代码实现

  1. 打开 idea,创建一个 springboot 工程,将下面代码,工具类拷贝到工程中。使用了spring的定时任务,这个可根据需求自主修改。
package com.example.springboot222.service; import com.aliyuncs.IAcsClient; import com.example.springboot222.util.AliyunDnsUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import java.net.Inet4Address; import java.net.Inet6Address; import java.net.InetAddress; import java.net.NetworkInterface; import java.util.Enumeration; import java.util.HashMap; import java.util.Map; @Component @EnableScheduling public class Ipv6Schedule { private static Logger logger = LoggerFactory.getLogger(Ipv6Schedule.class); private static String regionId = ""; //必填固定值,必须为“cn-hanghou” private static String accessKeyId = "****"; // your accessKey private static String accessKeySecret = "******";// your accessSecret //时间表达式,1分钟执行一次ipv6地址更新 @Scheduled(cron="1 0/1 * * * ?") public void test1() throws Exception{ Enumeration<NetworkInterface> interfs = NetworkInterface.getNetworkInterfaces(); Map<String,String> reqMap = new HashMap<String,String>(); int i = 1; while (interfs.hasMoreElements()) { NetworkInterface interf = interfs.nextElement(); // if(interf.getDisplayName().contains("AX200")) { Enumeration<InetAddress> addres = interf.getInetAddresses(); while (addres.hasMoreElements()) { InetAddress in = addres.nextElement(); if (in instanceof Inet4Address) { logger.info("v4." + i + ":" + in.getHostAddress()); reqMap.put("v4."+i, in.getHostAddress()); } else if (in instanceof Inet6Address) { logger.info("v6." + i + ":" + in.getHostAddress()); reqMap.put("v6."+i, in.getHostAddress()); } i++; } // } } IAcsClient client = AliyunDnsUtils.getIAcsClient(accessKeyId,accessKeySecret); String recordId = AliyunDnsUtils.uploadIpv6ToAliyun(client,reqMap.get("v6.6")); } } 
package com.example.springboot222.util; 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.exceptions.ServerException; import com.aliyuncs.profile.DefaultProfile; import com.aliyuncs.profile.IClientProfile; import com.aliyuncs.utils.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * @auther Bin * @date 2021/4/27 15:16 * @description */ public class AliyunDnsUtils { private static Logger logger = LoggerFactory.getLogger(AliyunDnsUtils.class); public static IAcsClient getIAcsClient(String accessKeyId,String accessKeySecret){ String regionId = "cn-hangzhou"; //必填固定值,必须为“cn-hanghou” IClientProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessKeySecret); // 若报Can not find endpoint to access异常,请添加以下此行代码 // DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", "Alidns", "alidns.aliyuncs.com"); return new DefaultAcsClient(profile); } public static String uploadIpv6ToAliyun(IAcsClient client,String ipv6){ /* if (ipv6.contains("fe80:")){ logger.info("This IPV6 [{}] is not standard IPV6, It can't update the aliDns",ipv6); return null; }*/ DescribeDomainRecordsRequest request = new DescribeDomainRecordsRequest(); request.setDomainName("shyg.top"); //你申请的阿里域名,有特价的,一年1元 request.setRRKeyWord("ddns1"); //主机记录,相当于你的二级域名 DescribeDomainRecordsResponse response; try { response = client.getAcsResponse(request); DescribeDomainRecordsResponse.Record record = response.getDomainRecords().size()>0?response.getDomainRecords().get(0) : new DescribeDomainRecordsResponse.Record(); String old_ip = StringUtils.isEmpty(record.getValue())?"127.0.0.1":record.getValue(); // String cur_ip = "2408:8406:7840:4cee:4c15:fd0:a58a:8b2f"; // cur_ip = cur_ip.substring(cur_ip.indexOf("/")+1,cur_ip.length()); if(!old_ip.equals(ipv6)){ logger.info("原地址{"+old_ip+"},新地址{"+ipv6+"}。"); UpdateDomainRecordRequest udr_req = new UpdateDomainRecordRequest(); udr_req.setValue(ipv6); udr_req.setType(record.getType()); udr_req.setTTL(record.getTTL()); udr_req.setPriority(record.getPriority()); udr_req.setLine(record.getLine()); udr_req.setRecordId(record.getRecordId()); udr_req.setRR("ddns1"); @SuppressWarnings("unused") UpdateDomainRecordResponse udr_resp = client.getAcsResponse(udr_req); logger.info("新地址{"+ipv6+"}上传成功"); return udr_resp.getRecordId(); }else{ logger.info("原地址与新地址相同,不需要修改"); return null; } } catch (ServerException e1) { logger.error("IPV6上传失败!",e1); } catch (Exception e1) { logger.error("IPV6上传失败![{}]",e1); } return null; } }
  1. pom.xml配置如下,引入阿里域名sdk包
 <dependency> <groupId>com.aliyun</groupId> <artifactId>aliyun-java-sdk-core</artifactId> <version>4.3.2</version> </dependency> <dependency> <groupId>com.aliyun</groupId> <artifactId>aliyun-java-sdk-alidns</artifactId> <version>2.0.10</version> </dependency> <dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> <version>5.1.0</version> </dependency>

  1. 如果报错如下:

com.aliyuncs.exceptions.ClientException: MissingRecordId : RecordId is mandatory for this action.
RequestId : 4EFD97F4-B6D2-49BA-A53C-BC3C9FDCC712

主要原因是 配置的主机记录有误,导致查询解析记录为空 抛出的异常。主机记录配置截图如下:

阿里云IPV6动态域名解析java代码实现插图

如果没有,请添加一条记录。

阿里云IPV6动态域名解析java代码实现插图1

另外还需要配置下解析相关的配置:

(ps:记录值由于是必输字段,可以输入一个默认值:“2408:8406:7840:4cee:4c15:fd0:a58a:8b2f”)

阿里云IPV6动态域名解析java代码实现插图2

阿里云AccessKey ID获取方式,请参看搂住的另一篇文章。https://blog.csdn.net/shyg866/article/details/106277640

原文链接:https://blog.csdn.net/shyg866/article/details/116259499?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522168476299816800188539640%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fblog.%2522%257D&request_id=168476299816800188539640&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~blog~first_rank_ecpm_v1~times_rank-13-116259499-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 分享