Ping value in java

2019-01-24 16:56发布

问题:

I know that this question has been approached under different ways, but I have checked stackoverflow and I didn't found the answer I was looking for.

To make it simple : Is there a way to get the Time ping value to an IP server under Windows ?

I know how to check if some servers are reachable, but I would like to have precise values, like we can read on terminal.

Thank you for your help and understanding.

回答1:

You can do something like this :

//The command to execute
String pingCmd = "ping " + ip + " -t";

//get the runtime to execute the command
Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec(pingCmd);     

//Gets the inputstream to read the output of the command
BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));

//reads the outputs
String inputLine = in.readLine();
while ((inputLine != null)) {
    if (inputLine.length() > 0) {
       ........
    }
    inputLine = in.readLine();
}

reference

UPDATE: As per your need

public class PingDemo {    
    public static void main(String[] args) {
        String ip = "localhost";
        String time = "";

        //The command to execute
        String pingCmd = "ping " + ip;

        //get the runtime to execute the command
        Runtime runtime = Runtime.getRuntime();
        try {
            Process process = runtime.exec(pingCmd);

            //Gets the inputstream to read the output of the command
            BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));

            //reads the outputs
            String inputLine = in.readLine();
            while ((inputLine != null)) {
                if (inputLine.length() > 0 && inputLine.contains("time")) {
                     time = inputLine.substring(inputLine.indexOf("time"));
                     break;                        
                }
                inputLine = in.readLine();
            }    
            System.out.println("time --> " + time);    
        } catch (Exception ex) {
            System.out.println(ex);
        }
    }
}

Written in little haste.



回答2:

You can invoke the ping command and read the output (as explained in the previous answer), or if you need a lower lever access (like you can do with RAW sockets), you can have a look at the jpcap java library.



回答3:

As shown here, you'll want to make use of the Runtime class to shell out a ping. All that's required of you is to parse the input stream (possibly using regex to get the time ping value).



回答4:

public static void main(String[] args) {

    System.out.println("Enter the host to be pinged : ");
    Scanner sc = new Scanner(in);
    String str = sc.next();
    System.out.println("Enter the no. of packets to be sent : ");
    int packets = sc.nextByte();
    String pingResult;
    int count=0;
    try{
        String command = "ping -c "+ packets +" -w 10 " + str;
        Process process = Runtime.getRuntime().exec(command);
        BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
        String inputLine;
        while ((inputLine = reader.readLine()) != null) {
            if(count==packets+4) {
                pingResult = (inputLine.substring(inputLine.indexOf("=")));
                pingResult = (pingResult.substring
                        (pingResult.indexOf("/")+1,pingResult.indexOf("/")+7));
                System.out.println(pingResult + " ms");
            }
            count++;
        }
        in.close();
        if(count==0)System.out.println("Wrong host entered.");
    }catch(Exception e){
        System.out.println("Exception caught: " + e.toString());
    }
}

}

Output: Enter the host to be pinged : 8.8.8.8 Enter the no. of packets to be sent : 5 31.406 ms

Process finished with exit code 0