• Fuentes

 #372646  por adwind
 
Codigo para obtener los dns de no-ip y actualizar.

Autor: yopi :p



[syntax="Java"]
package cliente;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.regex.Pattern;

public class NoIpService {

private String user;
private String pass;

public NoIpService(String user, String pass) {
this.user = user;
this.pass = pass;
}

public String[] getDNS() {
String[] DNS = null;
try {
HttpURLConnection conexion = (HttpURLConnection) new URL("http://dynupdate.no-ip.com/list-hosts.php?email=" + user + "&pass=" + pass).openConnection();
BufferedReader br = new BufferedReader(new InputStreamReader(conexion.getInputStream()));
String t = "";
while (br.ready()) {
t = br.readLine();
}
if (t.contains("=")) {
return null;
}
t = t.replace("#,", "");
DNS = t.split(Pattern.quote("|"));
return DNS;
} catch (IOException ex) {
}
return DNS;
}

public String actualiza(String DNS, String ip) {
String texto = "Ocurrio un error";
String URL = "http://dynupdate.no-ip.com/dns?username=" + user + "&password=" + pass + "&hostname=" + DNS;
if (!ip.equals("")) {
URL += "&ip=" + ip;
}
try {
HttpURLConnection conexion = (HttpURLConnection) new URL(URL).openConnection();
BufferedReader br = new BufferedReader(new InputStreamReader(conexion.getInputStream()));
String resultado = br.readLine();
conexion.disconnect();
String dato[] = resultado.split(":");
int valor = Integer.parseInt(dato[1]);
switch (valor) {
case 0:
texto = "Sin cambios";
break;
case 1:
texto = "Actualizado";
break;
}
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
return texto;

}

public static void main(String[] args) {
NoIpService n = new NoIpService("[email protected]", "fulanitodetal");
//Nos regresa un array con los dns asociados al email.
String dns[] = n.getDNS();
//imprimimos los dns
for (int i = 0; i < dns.length; i++) {
System.out.println(dns);
}

//Para actualizarlos lo podemos hacer de esta manera.
for (int i = 0; i < dns.length; i++) {
//Si queremos actualizar con la ip publica se la podemos enviar como parametro o
//lo dejamos vaio n.actualiza(dns, "");
//para que sea el servidor de no-ip sea el que se encargue de detectar la ip publica de nuestra pc
n.actualiza(dns, "127.0.0.1");
}

}
}


[/syntax]