JAVA 阿里云域名实现DDNS(ipv6地址)

import com.aliyun.alidns20150109.models.UpdateDomainRecordRequest; import com.aliyun.tea.TeaException; import com.aliyun.teaopenapi.models.Config; import com.aliyun.teautil.models.RuntimeOptions; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.*; import java.util.Enumeration; import java.util.Map; public class RefreshAliDDNS { /** * 使用AK&SK初始化账号Client */ public static com.aliyun.alidns20150109.Client createClient(String accessKeyId, String accessKeySecret) throws Exception { Config config = new Config() // 您的 AccessKey ID .setAccessKeyId(accessKeyId) // 您的 AccessKey Secret .setAccessKeySecret(accessKeySecret); // 访问的域名 config.endpoint = "alidns.cn-shanghai.aliyuncs.com"; return new com.aliyun.alidns20150109.Client(config); } /** * 获取ipv6地址 */ public static String getIpAddress() throws SocketException { Enumeration<NetworkInterface> enumeration = NetworkInterface.getNetworkInterfaces(); while (enumeration.hasMoreElements()) { NetworkInterface network = enumeration.nextElement(); if (!network.isVirtual() && network.isUp()) { Enumeration<InetAddress> addresses = network.getInetAddresses(); while (addresses.hasMoreElements()) { InetAddress address = (InetAddress) addresses.nextElement(); if ((address instanceof Inet4Address || address instanceof Inet6Address)) { if( address.getHostAddress().startsWith("2409") || address.getHostAddress().startsWith("2408") || address.getHostAddress().startsWith("240e") || address.getHostAddress().startsWith("240a")){ return address.getHostAddress().substring( 0,address.getHostAddress().lastIndexOf("%")); } } } } } return ""; } public static void init() throws Exception { String ipv6A = getIpAddress(); if( "".equals(ipv6A)){ System.out.println("-----未获取到IPV6地址------"); return; } System.out.println("-----获取到IPV6地址" + ipv6A +"------"); com.aliyun.alidns20150109.Client client = createClient("accessKeyId", "accessKeySecret"); UpdateDomainRecordRequest updateDomainRecordRequest = new UpdateDomainRecordRequest() .setRecordId("799061230780853248")//记录RecordId .setValue(ipv6A) .setType("AAAA") .setRR("@");//域名前缀 RuntimeOptions runtime = new RuntimeOptions(); try { // 复制代码运行请自行打印 API 的返回值 client.updateDomainRecordWithOptions(updateDomainRecordRequest, runtime); System.out.println("success."); } catch (TeaException error) { Map<String, Object> data = error.getData(); if( "DomainRecordDuplicate".equals( data.get("Code"))){ System.out.println("-----IPV6地址与线上一致,无需设置------"); }else { // 如有需要,请打印 error com.aliyun.teautil.Common.assertAsString(error.message); error.printStackTrace(); } } catch (Exception _error) { TeaException error = new TeaException(_error.getMessage(), _error); // 如有需要,请打印 error com.aliyun.teautil.Common.assertAsString(error.message); _error.printStackTrace(); } } public static void main(String[] args) throws Exception { init(); } }

原文链接:https://blog.csdn.net/weixin_43685606/article/details/128148126

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