我在列表中有一组服务器。我想每隔30分钟ping一次这些服务器。我怎么能实现这个。这是一个带java代码的jsp程序。
答案 0 :(得分:1)
此代码将在每30分钟后执行以ping到给定的ip
public boolean ping(String ip) throws IOException, InterruptedException extends TimerTask {
public void run{
boolean isWindows = System.getProperty("os.name").toLowerCase().contains("win");
ProcessBuilder processBuilder = new ProcessBuilder("ping", isWindows? "-n" : "-c", "1", ip`enter code here`);
Process proc = processBuilder.start();
BufferedReader in =
new BufferedReader(
new InputStreamReader(proc.getInputStream()));
while (true) {
String line = in.readLine();
if (line == null)
break;
if(line.contains("Destination host unreachable")||line.contains("Request timed out")){
return false;
}
}
return true;
}
}
// And From your main() method or any other method
Timer timer = new Timer();
timer.schedule(new ping(ip), 0, (60*30*1000));