gpt4 book ai didi

java - 如何运行 PING 命令并获取 ping 主机摘要?

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:42:58 26 4
gpt4 key购买 nike

您好,我需要使用 Java 代码执行 PING 命令并获取 ping 主机的摘要。如何用 Java 实现?

最佳答案

按照 viralpatel 的规定,您可以使用 Runtime.exec()

下面是一个例子

class pingTest {

public static void main(String[] args) {
String ip = "127.0.0.1";
String pingResult = "";

String pingCmd = "ping " + ip;
try {
Runtime r = Runtime.getRuntime();
Process p = r.exec(pingCmd);

BufferedReader in = new BufferedReader(new
InputStreamReader(p.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null) {
System.out.println(inputLine);
pingResult += inputLine;
}
in.close();

} catch (IOException e) {
System.out.println(e);
}

}
}

输出

Pinging 127.0.0.1 with 32 bytes of data:
Reply from 127.0.0.1: bytes=32 time<1ms TTL=128
Reply from 127.0.0.1: bytes=32 time<1ms TTL=128
Reply from 127.0.0.1: bytes=32 time<1ms TTL=128
Reply from 127.0.0.1: bytes=32 time<1ms TTL=128

Ping statistics for 127.0.0.1:
Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 0ms, Maximum = 0ms, Average = 0ms

引用http://www.velocityreviews.com/forums/t146589-ping-class-java.html

关于java - 如何运行 PING 命令并获取 ping 主机摘要?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8815012/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com