Showing posts with label PING. Show all posts
Showing posts with label PING. Show all posts

Saturday, April 17, 2010

EJECUTAR UN COMANDO 'PING' DESDE JAVA.

En muchas ocaciones es necesario validar la conexiones que tengamos apuntando hacia diferentes servidores o PC en Red. Para esto una forma de validar conexion con ellos es hacer un simple 'PING' desde consola hacia dichas IPs o HOST. Bueno en este momento mostraré una de las formas para poder realizar este proceso desde JAVA y así validar.

El código es el siguiente:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
* @author Cesar Ricardo.
* @clase: JavaPing.java
* @descripción descripción de la clase.
* @author_web: http://frameworksjava2008.blogspot.com
http://viviendoconjavaynomoririntentandolo.blogspot.com
* @author_email: nombre del email del autor.
* @author_company: nombre de la compañía del autor.
* @fecha_de_creación: dd-mm-yyyy.
* @fecha_de_ultima_actualización: dd-mm-yyyy.
* @versión 1.0
*/
public class ManejoComandoPing{

private static final String COMANDO = "ping";
private static final String ESPACIO = " ";
private static final String IP_HOST = "www.google.com";

/**
* main
* @param argumentos
*/
public static void main( String[] argumentos ){

ManejoComandoPing javaPing = new ManejoComandoPing();

String IP = null;
String otroIP = IP_HOST;

//Validación Previa.
if( argumentos.length > 0 ){
IP = argumentos[ 0 ]; //Desde consola (DOS, UNIX, LINUX).
}
else{
if( (otroIP.equalsIgnoreCase( "" )) ){
IP = "localhost"; //HardCode.
}
else{
IP = otroIP;
}
}

//Validacion IP.
javaPing.validarPingIP( IP );
}

/**
* validarPingIP
* @param IP
*/
public void validarPingIP( String IP ){

String mensaje = null;
String pingCMD = COMANDO + ESPACIO + IP;

try{
Runtime ejecuta = Runtime.getRuntime();
Process proceso = ejecuta.exec( pingCMD );

InputStreamReader entrada = new InputStreamReader( proceso.getInputStream() );
BufferedReader buffer = new BufferedReader( entrada );

String linea = "";

for( ;(linea = buffer.readLine() ) != null; ){
System.out.println( linea );
mensaje += linea;
}

buffer.close();
}
catch( IOException e ){
System.out.println( e );
}
catch( Exception e ){
System.out.println( e );
}
}
}

La salida es la siguiente:

Haciendo ping a www.l.google.com [72.14.204.99] con 32 bytes de datos:

Respuesta desde 72.14.204.99: bytes=32 tiempo=110ms TTL=51

Respuesta desde 72.14.204.99: bytes=32 tiempo=108ms TTL=51

Respuesta desde 72.14.204.99: bytes=32 tiempo=110ms TTL=51

Respuesta desde 72.14.204.99: bytes=32 tiempo=108ms TTL=51