如你梦想和现实只是一步之遥,那么我觉得这一步就应该是执着和笨蛋式的坚持了吧。

java 批量测试主机能否ping通

java 独孤九剑 169℃

转自:https://www.blog-china.cn/blog/liuzaiqingshan/home/10/1504666983982

public class TestHostConnect {
   public static boolean isConnect(String ip){    
         boolean connect = false;    
         Runtime runtime = Runtime.getRuntime();    
         Process process;    
         try {    
             process = runtime.exec("ping " + ip);    
             InputStream is = process.getInputStream();     
             InputStreamReader isr = new InputStreamReader(is);     
             BufferedReader br = new BufferedReader(isr);     
             String line = null;     
             StringBuffer sb = new StringBuffer();     
             while ((line = br.readLine()) != null) {     
                 sb.append(line);     
             }     
             is.close();     
             isr.close();     
             br.close();     
      
             if (null != sb && !sb.toString().equals("")) {     
                 String logString = "";     
                 if (sb.toString().indexOf("TTL") > 0) {     
                     // 网络畅通      
                     connect = true;    
                 } else {     
                     // 网络不畅通      
                     connect = false;    
                 }     
             }     
         } catch (IOException e) {    
             e.printStackTrace();    
         }     
         return connect;    
     }    
          
     public static void main(String[] args) {
      String startString = "192.168.61.";
      System.out.println("可用IP:\n");
      for(int i = 0 ;i<255;i++){
       String ip = startString+i;
       boolean isConnect = isConnect(ip);
       if(!isConnect){
        System.out.println(ip);
       }
//       System.out.println("IP:\t"+ip+"\tping結果:\t"+(isConnect?"已经存在":"没有主机"));
       
      }
      
     }    
}

PS: 服务器页面生成花费时间 0.14秒 查询 40 次

转载请注明:KEEP3YUE » java 批量测试主机能否ping通

喜欢 (0)