publicvoid Ping(String ip) throws Exception { // 鏈澶?0涓嚎紼?/span> while (threadCount > 30) Thread.sleep(50); threadCount += 1; PingIp p = new PingIp(ip); p.start(); }
publicvoid PingAll() throws Exception { // 棣栧厛寰楀埌鏈満鐨処P錛屽緱鍒扮綉孌?br /> //InetAddress host = InetAddress.getLocalHost(); //InetAddress host = InetAddress.getByName(null); InetAddress host = InetAddress.getByName("127.0.0.1"); String hostAddress = host.getHostAddress(); int k = 0; k = hostAddress.lastIndexOf("."); String ss = hostAddress.substring(0, k + 1); for (int i = 1; i <= 255; i++) { // 瀵規(guī)墍鏈夊眬鍩熺綉Ip String iip = ss + i; Ping(iip); }
// 絳夌潃鎵鏈塒ing緇撴潫 while (threadCount > 0) Thread.sleep(50); }
publicstaticvoid main(String[] args) throws Exception { ReadIp ip = new ReadIp(); ip.PingAll(); Set entries = ping.entrySet(); Iterator iter = entries.iterator();
String k; while (iter.hasNext()) { Map.Entry entry = (Map.Entry) iter.next(); String key = (String) entry.getKey(); String value = (String) entry.getValue();
if (value.equals("true")) System.out.println(key + "-->" + value); } }
class PingIp extends Thread { public String ip; // IP
public PingIp(String ip) { this.ip = ip; }
publicvoid run() { try { Process p = Runtime.getRuntime().exec( "ping " + ip + " -w 300 -n 1"); InputStreamReader ir = new InputStreamReader(p.getInputStream()); LineNumberReader input = new LineNumberReader(ir); // 璇誨彇緇撴灉琛?/span> for (int i = 1; i < 7; i++) input.readLine(); String line = input.readLine();