在阿里云上买的域名: *******.love
现在都是动态IP,每次IP变了就得从新在阿里云后台改。
用阿里云解析提供的接口,可以自动修改解析的IP。
思路是每次开机轮询IP是否变化,如果变化了,就按接口文档提供的格式,发一个post请求实现修改。这个修改会有几分钟的间隔才会生效。
这样在家开启这个程序,或者导出exe放启动文件里,开机启动。便可以实时更新域名对应的ip地址。
关键代码:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.URL;
import java.net.URLConnection;
import java.util.Date;
import java.util.List;
import com.aliyun.api.AliyunClient;
import com.aliyun.api.AliyunConstants;
import com.aliyun.api.DefaultAliyunClient;
import com.aliyun.api.dns.dns20150109.request.DescribeSubDomainRecordsRequest;
import com.aliyun.api.dns.dns20150109.request.UpdateDomainRecordRequest;
import com.aliyun.api.dns.dns20150109.response.DescribeSubDomainRecordsResponse;
import com.aliyun.api.dns.dns20150109.response.UpdateDomainRecordResponse;
import com.aliyun.api.domain.Record;
public class DemoListDomains {
private static AliyunClient client;
static {
String serverUrl = "http://dns.aliyuncs.com/";
String accessKeyId = "yourId";
String accessKeySecret = "yourSecret";
client = new DefaultAliyunClient(serverUrl, accessKeyId, accessKeySecret, AliyunConstants.FORMAT_JSON);
}
public static void main(String[] args) {
while(true){
try {
// 获取域名信息
DescribeSubDomainRecordsRequest ddi_req = new DescribeSubDomainRecordsRequest();
ddi_req.setTimestamp(new Date().getTime());
DescribeSubDomainRecordsResponse ddi_resp = new DescribeSubDomainRecordsResponse();
ddi_req.setSubDomain("your域名");
ddi_resp = client.execute(ddi_req);
List<Record> list = ddi_resp.getDomainRecords();
boolean changed = false;
for (Record record : list) {
String ip = getMyIPOutter();// 获得本机公网IP
System.out.println("old_ip : " + record.getValue());
System.out.println("outter_ip : " + ip);
if (!ip.equals(record.getValue())) {
// 根据传入参数修改解析记录
UpdateDomainRecordRequest udr_req = new UpdateDomainRecordRequest();
udr_req.setTimestamp(new Date().getTime());
udr_req.setRecordId(record.getRecordId());
udr_req.setrR(record.getrR());
udr_req.setValue(ip);
udr_req.setType(record.getType());
udr_req.settTL(record.gettTL());
udr_req.setPriority(record.getPriority());
udr_req.setLine(record.getLine());
UpdateDomainRecordResponse udr_resp = new UpdateDomainRecordResponse();
udr_resp = client.execute(udr_req);
if (udr_resp.getErrorCode() == null) {
System.out.println("success");
changed = true;
} else {
System.out.println("error");
}
}
}
if (!changed) {
System.out.println("need not change");
}
} catch (Exception e) {
e.printStackTrace();
System.out.println("error");
}
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
private static String getMyIPOutter() throws IOException {
InputStream ins = null;
try {
URL url = new URL("http://1212.ip138.com/ic.asp");
URLConnection con = url.openConnection();
ins = con.getInputStream();
InputStreamReader isReader = new InputStreamReader(ins, "GB2312");
BufferedReader bReader = new BufferedReader(isReader);
StringBuffer webContent = new StringBuffer();
String str = null;
while ((str = bReader.readLine()) != null) {
if (str.contains("您的IP是:[")) {
webContent.append(str);
break;
}
}
int start = webContent.indexOf("[") + 1;
int end = webContent.indexOf("]");
return webContent.substring(start, end);
} finally {
if (ins != null) {
ins.close();
}
}
}
@SuppressWarnings("unused")
private static String getMyIPLocal() throws IOException {
InetAddress ia = InetAddress.getLocalHost();
return ia.getHostAddress();
}
}
效果:
需要导入两个jar:一个阿里云官方的sdk,一个commons-logging-1.2.jar
来源:CSDN
作者:番茄发烧了
链接:https://blog.csdn.net/bless2015/article/details/51284259