<div> <pre><span style="color: #008000">/** * 检测IP是否可以ping通 * @param IP * @return */</span> <span style="color: #0000ff">private</span> <span style="color: #0000ff">static</span> String pingIP(String IP) { BufferedReader in = <span style="color: #0000ff">null</span>; Runtime rt = Runtime.getRuntime(); <span style="color: #0000ff">boolean</span> FoundMatch = <span style="color: #0000ff">false</span>; String pingCommand = "<span style="color: #8b0000">ping </span>" + IP + "<span style="color: #8b0000"> -w </span>" + 3000; <span style="color: #0000ff">try</span> { Process pro = rt.exec(pingCommand); in = <span style="color: #0000ff">new</span> BufferedReader(<span style="color: #0000ff">new</span> InputStreamReader(pro.getInputStream())); String line = in.readLine(); <span style="color: #0000ff">while</span> (line != <span style="color: #0000ff">null</span>) { <span style="color: #0000ff">try</span> { Pattern Regex = Pattern.compile("<span style="color: #8b0000">(T|t){2}(L|l)</span>", Pattern.CANON_EQ); Matcher RegexMatcher = Regex.matcher(line); FoundMatch = RegexMatcher.find(); <span style="color: #0000ff">if</span> (FoundMatch) { pro.destroy(); <span style="color: #0000ff">return</span> IP.trim(); } } <span style="color: #0000ff">catch</span> (PatternSyntaxException ex) { <span style="color: #008000">// Syntax error in the regular expression</span> ex.getMessage(); } line = in.readLine(); } } <span style="color: #0000ff">catch</span> (IOException e) { <span style="color: #008000">// TODO Auto-generated catch block</span> System.out.println(e.getMessage()); } <span style="color: #0000ff">return</span> <span style="color: #0000ff">null</span>; }</pre> </div>
转载于:https://my.oschina.net/sorthWindow/blog/110820