JavaBlog.fr / Java.lu DEVELOPMENT,Java,Tools Java : Verify Accessibility Via PING Command

Java : Verify Accessibility Via PING Command

Hello,

Just a method in order to verify the accessibility of a host via PING command.
Examples With PING command
…With host name:

01Microsoft Windows [version 10.0.14393]
02(c) 2016 Microsoft Corporation. Tous droits réservés.
03 
04C:\Users\H>ping www.google.fr
05 
06Envoi d’une requête 'ping' sur www.google.fr [77.154.221.181] avec 32 octets de données :
07Réponse de 77.154.221.181 : octets=32 temps=47 ms TTL=53
08Réponse de 77.154.221.181 : octets=32 temps=319 ms TTL=53
09Réponse de 77.154.221.181 : octets=32 temps=51 ms TTL=53
10 
11Statistiques Ping pour 77.154.221.181:
12    Paquets : envoyés = 3, reçus = 3, perdus = 0 (perte 0%),
13Durée approximative des boucles en millisecondes :
14    Minimum = 47ms, Maximum = 319ms, Moyenne = 139ms

…With IP:

01C:\Users\H>ping 77.154.221.181
02 
03Envoi d’une requête 'Ping'  77.154.221.181 avec 32 octets de données :
04Réponse de 77.154.221.181 : octets=32 temps=56 ms TTL=53
05Réponse de 77.154.221.181 : octets=32 temps=341 ms TTL=53
06Réponse de 77.154.221.181 : octets=32 temps=51 ms TTL=53
07 
08Statistiques Ping pour 77.154.221.181:
09    Paquets : envoyés = 3, reçus = 3, perdus = 0 (perte 0%),
10Durée approximative des boucles en millisecondes :
11    Minimum = 51ms, Maximum = 341ms, Moyenne = 149ms

 

 

Java Source Code

01public static boolean isReachableByPing(String host) {
02    try{
03            String cmd = "";
04            if(System.getProperty("os.name").startsWith("Windows")) {  
05                    // For Windows
06                    cmd = "ping -n 1 " + host;
07            } else {
08                    // For Linux and OSX
09                    cmd = "ping -c 1 " + host;
10            }
11 
12            int nb_try = 10;
13            for (int i = 0; i <nb_try; i++) {
14                Process myProcess = Runtime.getRuntime().exec(cmd);
15                myProcess.waitFor();
16                if(myProcess.exitValue() == 0) {
17                    return true;
18                }else{
19                    Thread.sleep(250); // Sleep 250 ms
20                }
21            }
22            return false;
23    } catch( Exception e ) {
24            e.printStackTrace();
25            return false;
26    }
27}

 

… main test method:

01public static void main(String[] args) {
02 
03    if(isReachableByPing("www.google.fr")){
04        System.out.println("The host 'www.google.fr' is reachable by ping command.");
05    }else{
06        System.out.println("The host 'www.google.fr' is NOT reachable by ping command.");
07    }
08     
09    if(isReachableByPing("77.154.221.181")){
10        System.out.println("The host '77.154.221.181' is reachable by ping command.");
11    }else{
12    System.out.println("The host '77.154.221.181' is NOT reachable by ping command.");
13    }
14}

…outputs:

1The host 'www.google.fr' is reachable by ping command.
2The host '77.154.221.181' is reachable by ping command.

 

 

That’s all!!!

Huseyin OZVEREN

Leave a Reply

Your email address will not be published.

Time limit is exhausted. Please reload CAPTCHA.

Related Post