如何运行PING命令并获取ping主机摘要? [英] How to run PING command and get ping host summary?
本文介绍了如何运行PING命令并获取ping主机摘要?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好,我需要使用Java代码执行 PING
命令,并获取ping主机的摘要。如何使用Java?
Hi I need to execute the PING
command using Java code and get summary of the ping host. How to do it in Java?
推荐答案
as viralpatel指定您可以使用 Runtime.exec / code>
as viralpatel specified you can use 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
这篇关于如何运行PING命令并获取ping主机摘要?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文