我想在 ping 主机后获取 ping 执行时间和结果字符串

发布于 2024-12-26 06:14:09 字数 45 浏览 2 评论 0原文

我想在 ping 主机后获取 ping 执行时间和结果字符串。我该怎么做呢?

I want to get the ping execution time and result in string after ping host. How can I do it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

相对绾红妆 2025-01-02 06:14:09
long currentTime = System.currentTimeMillis();
boolean isPinged = InetAddress.getByName(servername).isReachable(2000); // 2 seconds
currentTime = System.currentTimeMillis() - currentTime;
if(isPinged) {
    System.out.println("pinged successfully in "+ currentTime+ "millisecond");
} else {
    System.out.println("PIng failed.");
}

但这只会在Windows系统中使用ICMP ping。

long currentTime = System.currentTimeMillis();
boolean isPinged = InetAddress.getByName(servername).isReachable(2000); // 2 seconds
currentTime = System.currentTimeMillis() - currentTime;
if(isPinged) {
    System.out.println("pinged successfully in "+ currentTime+ "millisecond");
} else {
    System.out.println("PIng failed.");
}

But this will use ICMP ping only in windows system.

平生欢 2025-01-02 06:14:09
long start = System.currentTimeMillis();
long ping;




String[] command = { "cmd.exe", "/C", "ping 192.168.1.101" };
commandProcess = Runtime.getRuntime().exec(command);
BufferedReader buffy = new BufferedReader(new InputStreamReader(commandProcess.getInputStream()));
String readline;
while((readline = buffy.readLine())!=null){
System.out.println(readline);
if(readline.contains("reply")){
 long ping = System.currentTimeMillis();
 System.out.println("Pinged in:"+ ping);
 }
}
 long end = System.currentTimeMillis();
 String done = "Completed in times:" +start + ping +end;
long start = System.currentTimeMillis();
long ping;




String[] command = { "cmd.exe", "/C", "ping 192.168.1.101" };
commandProcess = Runtime.getRuntime().exec(command);
BufferedReader buffy = new BufferedReader(new InputStreamReader(commandProcess.getInputStream()));
String readline;
while((readline = buffy.readLine())!=null){
System.out.println(readline);
if(readline.contains("reply")){
 long ping = System.currentTimeMillis();
 System.out.println("Pinged in:"+ ping);
 }
}
 long end = System.currentTimeMillis();
 String done = "Completed in times:" +start + ping +end;
锦上情书 2025-01-02 06:14:09

这就是我使用它的方式 -

private static void checkPing(String hostName) {

    String[] command = { "cmd.exe", "/C", "ping " + hostName };
    try {
        Process p = Runtime.getRuntime().exec(command);
        BufferedReader buff = new BufferedReader(new InputStreamReader(p.getInputStream()));
        String readline;
        while ((readline = buff.readLine()) != null) {
            if (readline.contains("Reply")) {
                System.out.println("Pinged " + hostName + " in : "
                        + readline.substring(readline.indexOf("time=") + 5, readline.indexOf("ms")) + " ms");
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

为您提供每个 ping 请求的精确(可靠)ping 延迟(以毫秒为单位)。
如果需要,您可以添加其中的四个

This is how I used it -

private static void checkPing(String hostName) {

    String[] command = { "cmd.exe", "/C", "ping " + hostName };
    try {
        Process p = Runtime.getRuntime().exec(command);
        BufferedReader buff = new BufferedReader(new InputStreamReader(p.getInputStream()));
        String readline;
        while ((readline = buff.readLine()) != null) {
            if (readline.contains("Reply")) {
                System.out.println("Pinged " + hostName + " in : "
                        + readline.substring(readline.indexOf("time=") + 5, readline.indexOf("ms")) + " ms");
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

Gives you the exact (reliable) ping latency in milliseconds for each ping request.
You can then add the four of them, if required

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文