在局域网中我们可以通过在cmd中利用ipconfig(windows)或者ifconfig(linux)查看本机局域网内IP,可以通过ping IP来测试你与另一个IP网络是否通畅,我们也可以通过for /l %p in (1,1,254) do @ping 192.168.0.%p -n 1 -w 20 |find "reply from" /i来帮我们ping局域网内IP。
我们也可以通过java程序来帮助我们实现这个功能。(文章主要作为笔记,如有什么不对的地方请指正,谢谢)
方法(1)
public static boolean ping(String ipAddress) throws Exception { int timeOut = 3000 ; //超时应该在3秒以上 boolean status = InetAddress.getByName(ipAddress).isReachable(timeOut); // 回值是true时,说明host是可用的,false则不可。 return status; }
java.net.InetAddress类的使用可以参考http://www.cnblogs.com/hnrainll/archive/2012/01/09/2317515.html
方法(2)
public static void ping2(String ipAddress) throws Exception { String line = null; try { Process pro = Runtime.getRuntime().exec("ping " + ipAddress); BufferedReader buf = new BufferedReader(newInputStreamReader(pro.getInputStream())); //循环ping 该IP while ((line = buf.readLine()) != null) System.out.println(line); } catch (Exception ex) { System.out.println(ex.getMessage()); } }
java.lang.Process类的可以参考http://lavasoft.blog.51cto.com/62575/15599
方法(3)
public static boolean ping3(String ipAddress, int pingTimes, int timeOut) { BufferedReader in = null; Runtime r = Runtime.getRuntime(); String pingCommand = "ping " + ipAddress + " -n " + pingTimes + " -w " + timeOut; try { // 执行命令并获取输出 System.out.println(pingCommand); Process p = r.exec(pingCommand); if (p == null) { return false; } in = new BufferedReader(new InputStreamReader(p.getInputStream())); // 逐行检查输出,计算类似出现=23ms TTL=62字样的次数 int connectedCount = 0; String line = null; while ((line = in.readLine()) != null) { connectedCount += getCheckResult(line); } // 如果出现类似=23ms TTL=62这样的字样,出现的次数=测试次数则返回真 return connectedCount == pingTimes; } catch (Exception ex) { ex.printStackTrace(); // 出现异常则返回假 return false; } finally { try { in.close(); } catch (IOException e) { e.printStackTrace(); } } }
java.lang.Runtime类的简介可以参考http://lavasoft.blog.51cto.com/62575/15565/
上面的三个都可以实现ping ip
我们在main函数中循环遍历IP
public static void main(String[] args) throws Exception { String ipAddress = "192.168.0.2"; for(int i=1;i<255;i++){ String IpAddress="192.168.0."+i; if(ping(IpAddress)){ System.out.println(IpAddress); }else { System.out.print(i); } } //System.out.println(ping(ipAddress)); // ping2(ipAddress); // System.out.println(ping3(ipAddress, 5, 5000)); }}
其中IP的最后一位大部分情况下是部位0和255的但是并非IP地址的最后一位永远不可以是255或0,这是有条件的。例如:当一个网段的掩码为255.255.255.0的时候,通常IP地址的最后一位不可以是255或0。因为这种掩码的网段,最后一位是0的IP是网段的网络地址,而最后一位是255的IP是网段的广播地址,不能分配给主机。假设一个网段为172.16.0.0,它的掩码是255.255.254.0,那么它的主机可用地址范围是172.16.0.1到172.16.1.254,即172.16.1.0和172.16.0.255这两个地址是可用的。
另外一点,当我们单纯的for循环ping的时候是非常浪费时间的,所以我们可以利用多线程来提高效率
package PingIPAdress; import java.io.IOException; import java.net.InetAddress; public class PingIpaddressThreads implements Runnable{ int startIpNum; int endIpNum; public PingIpaddressThreads(int startIpNum,int endIpNum){ this.startIpNum=startIpNum; this.endIpNum=endIpNum; } @Override public void run(){ int outtime=300; boolean statu=false; for(int i=startIpNum;i<=endIpNum;i++) { String IPAddress = "192.168.0." + String.valueOf(i); try { statu = InetAddress.getByName(IPAddress).isReachable(outtime); } catch (IOException e) { e.printStackTrace(); } if (statu) { System.out.println(""); System.out.println(IPAddress); } else { //System.out.println(i); } } } }
public class PingIpMain { public static void main(String[] args){ int start; int end; long starttime=System.currentTimeMillis(); for(int i=0;i<4;i++){ start=i*51; end=(i+1)*51; if(start==0){ start=1; } if(end==255){ end=254; } Runnable runnable=new PingIpaddressThreads(start,end); Thread thread1=new Thread(runnable); thread1.start(); } long endtime=System.currentTimeMillis(); System.out.println("共用时"+(endtime-starttime)+"s"); } }
这里的共用时是线程创建完到这里的时间,并不是线程执行完的时间。而线程执行时间的统计我们可以使用CountDownLatch,可以看看http://www.importnew.com/15731.html
public class PingIpMain { public static void main(String[] args) throws InterruptedException { int start; int end; long starttime=System.currentTimeMillis(); Date date1=new Date(); System.out.println(date1); CountDownLatch countDownLatch=new CountDownLatch(4); for(int i=0;i<4;i++){ start=i*51; end=(i+1)*51; if(start==0){ start=1; } if(end==255){ end=254; } Runnable runnable=new PingIpaddressThreads(start,end,countDownLatch); Thread thread1=new Thread(runnable); thread1.start(); } countDownLatch.await(); long endtime=System.currentTimeMillis(); Date date2=new Date(); System.out.println(date2); System.out.println("共用时"+(endtime-starttime)/1000+"s"); } }
public class PingIpaddressThreads implements Runnable{ int startIpNum; int endIpNum; CountDownLatch countDownLatch; public PingIpaddressThreads(int startIpNum, int endIpNum, CountDownLatch countDownLatch){ this.startIpNum=startIpNum; this.endIpNum=endIpNum; this.countDownLatch=countDownLatch; } @Override public void run(){ int outtime=300; boolean statu=false; for(int i=startIpNum;i<=endIpNum;i++) { String IPAddress = "192.168.0." + String.valueOf(i); try { statu = InetAddress.getByName(IPAddress).isReachable(outtime); } catch (IOException e) { e.printStackTrace(); }finally { } if (statu) { System.out.println(""); System.out.println(IPAddress); } else { //System.out.println(i); } } countDownLatch.countDown(); } }