Java - Ping ip

 
Vista:
sin imagen de perfil

Ping ip

Publicado por Luis (2 intervenciones) el 07/07/2015 01:42:03
Buenas gente, tengo un pequeño problema. Necesito crear una aplicación en consola tal cual el "ping 192.168.1.1 -t" que hacemos en cmd. Mi código tal cual compila si errores, pero necesito que el mensaje "NO SE ALCANZA LA IP" se repita hasta que nuevamente haga ping. Se los agradecería


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
try{
 
            ip=capturaip.readLine();//Lee primera linea "IP"
            users=capturauser.readLine();//SEGUNDA LINEA "USUARIOS"
            InetAddress ipping=InetAddress.getByName(ip); //IPPING obtiene la cadena como direccion IP
            /*boolean respuesta=ipping.isReachable(8000);*/
            while(ipping.isReachable(8000)){//MIENTRAS RESPONDA EN TIEMPO
                System.out.println("LA IP "+ip+" RESPONDE..");
            }
                System.err.println("**************NO SE ALCANZA LA IP**************"); //<< NECESITO SEA UN BUCLE REPETITIVO HASTA QUE NUEVAMENTE HAGA PING
            {
                try{
                  Runtime.getRuntime().exec("/home/oscar/enviar_mensaje "+users+ " \"CAIDA DE SERVIDOR IP: "+ip+"\" ");
 
                }
                catch (Exception e){
                    System.err.println("ERROR: AL ENVIAR MSG");
                    System.err.println("/home/oscar/enviar_mensaje "+users+ " \"CAIDA DE SERVIDOR IP: "+ip+"\" ");
                }
            }
        }catch (Exception e){
 
            System.err.println("ERROR: AL REALIZAR PING");
 
        }
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
Imágen de perfil de Hugo

Ping ip

Publicado por Hugo (3 intervenciones) el 07/07/2015 17:54:28
Hola Luis.
Esta clase funciona haciendo ping a la ip que captures en consola, sólo modificala para que envié lo que quieres.
Clase Ping sencilla.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import java.io.IOException;
import java.net.InetAddress;
import java.util.Scanner;
import static java.lang.System.*;
 
/**
 * 
 */
 
/**
 * @author ?
 *
 */
public class Ping {
 
	/**
	 * @param args
	 */
	public static void main(String... args) {
		// TODO Auto-generated method stub
 
		out.println("Escribe la ip para hacer un ping");
		scan = new Scanner(System.in);
		leeIp = scan.nextLine();
		try {
			ping = InetAddress.getByName(leeIp);
			while (ping.isReachable(8000)) {
				System.out.println(leeIp + " - responde!");
			}
		} catch (IOException ex) {
			System.out.println(ex.getLocalizedMessage());
		}
	}
 
	private static String leeIp;
	private static Scanner scan;
	private static InetAddress ping;
}
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar
sin imagen de perfil

Ping ip

Publicado por Luis (2 intervenciones) el 07/07/2015 18:44:26
Hugo ante todo, muchas gracias por tu pronta respuesta. Probando de diferentes maneras llegue a esta solución y la comparto con ustedes.
Lo que deseaba hacer era ingresar un numero de ip cual fuera, y que esta me respondiera si se encuentra en linea o no, y mostrarlo mediante un bucle repetitivo sin fin. Un ejemplo claro sería el realizar un "ping 192.168.1.1 -t" desde la consola cmd.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
public class PingIP {
 
    public static void main(String[] args) {
 
        BufferedReader capturaip;
        capturaip = new BufferedReader(new InputStreamReader(System.in)); // Captura de ip
        String ip;
 
 
        try{
 
            ip=capturaip.readLine();//Lee primera linea "IP"
            InetAddress ipping=InetAddress.getByName(ip); //IPPING obtiene la cadena como direccion IP            
            boolean espera= true;
 
            while(espera==true){
                if(ipping.isReachable(8000))//MIENTRAS RESPONDA EN TIEMPO
                System.out.println("LA IP "+ip+" RESPONDE..");
                else{
                System.err.println("NO SE ALCANZA LA IP");
                    try{
                     Runtime.getRuntime().exec("/home/data/enviar_mensaje  \"CAIDA_SERVIDOR\""); //AQUI LO QUE HAGO ES LLAMAR A UNA APLICACION POP UP DE MENSAJERIA
                        }
                    catch (Exception e){
                        System.err.println("ERROR: enviar_mensaje");
                        }
                    }
                }
        }catch (Exception e){
 
            System.err.println("ERROR: AL REALIZAR PING");
 
        }
    }
}
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar