use*_*914 4 java ip ping return-value
我知道这个问题已经以不同的方式处理,但我已经检查了stackoverflow并且我没有找到我正在寻找的答案.
为简单起见:有没有办法在Windows下为Windows服务器获取Time ping值?
我知道如何检查某些服务器是否可访问,但我希望有精确的值,就像我们可以在终端上阅读一样.
感谢您的帮助和理解.
你可以这样做:
//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();
}
Run Code Online (Sandbox Code Playgroud)
更新: 根据您的需要
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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
写得很匆忙.
| 归档时间: |
|
| 查看次数: |
3244 次 |
| 最近记录: |