4

我知道已经以不同的方式解决了这个问题,但是我检查了 stackoverflow 并没有找到我正在寻找的答案。

为了简单起见:有没有办法在Windows下将Time ping 值获取到 IP 服务器?

我知道如何检查某些服务器是否可以访问,但我想有精确的值,就像我们可以在终端上阅读一样。

感谢您的帮助和理解。

4

4 回答 4

6

你可以这样做:

//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();
}

参考

更新: 根据您的需要

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);
        }
    }
}

写的有点仓促。

于 2012-08-22T16:42:54.403 回答
1

您可以调用ping命令并读取输出(如上一个答案中所述),或者如果您需要较低级别的访问权限(就像您可以使用 RAW 套接字一样),您可以查看jpcap java 库。

于 2012-08-22T16:47:41.350 回答
1

如此处所示,您将需要使用Runtime该类来发送 ping。您所需要做的就是解析输入流(可能使用正则表达式来获取时间 ping 值)。

于 2012-08-22T16:51:47.767 回答
0
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());
    }
}

}

输出:输入要ping的主机:8.8.8.8输入号码。要发送的数据包数:5 31.406 ms

进程以退出代码 0 结束

于 2017-07-27T09:31:27.783 回答