I was looking for a way to update the DynDNS hosts pointing to my EC2 instances that run only on demand (controlled by an web application). Yes, there are apps and tools to let the server itself update his DynDNS hosts, but I want to do it from a central application. The DynDNS API description you find here, unfortunately there are no samples. But if you familiar with REST calls, shouldn’t be a big issue. For those who to get running quick, here the key code. To experiment, just create a simple Java application with Netbeans (or any other IDE), download the HttpComponents from Apache and add the library files to your project.
Libraries
commons-codec-1.4.jar
commons-logging-1.1.1.jar
httpclient-4.1.1.jar
httpclient-cache-4.1.1.jar
httpcore-4.1.jar
httpmime-4.1.1.jar
Sourcecode to update an IP address
void updateDynDNS() {
...
// replace with your own data !
String hostName = "whateveryourdomainnameis.selfip.org";
String hostIP = "1.2.3.5";
String userName = "user";
String userPassword = "password";
int responseCode;
try {
// Encode username and password
BASE64Encoder enc = new sun.misc.BASE64Encoder();
String userpassword = userName + ":" + userPassword;
String encodedAuthorization = enc.encode(userpassword.getBytes());
// Connect to DynDNS
URL url = new URL("http://members.dyndns.org/nic/update?hostname=" + hostName + "&myip=" + hostIP);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("User-Agent", "Demo DynDNS Updater");
connection.setRequestProperty("Authorization", "Basic " + encodedAuthorization);
// Execute GET
responseCode = connection.getResponseCode();
System.out.println(responseCode + ":" + connection.getResponseMessage());
// Print feedback
String line;
InputStreamReader in = new InputStreamReader((InputStream) connection.getContent());
BufferedReader buff = new BufferedReader(in);
do {
line = buff.readLine();
System.out.println(line);
} while (line != null);
connection.disconnect();
} catch (Exception ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
...
Up to you to embed into your application and do proper exception handling.
In a separate tutorial I will describe the basics of an EC2 controlling application using the AWS SDK for Java.