局域网扫描IP
今天有朋友去面试,被问到一个“如何扫描局域网IP”的问题(即找出局域网中当前已使用的IP),朋友回答的不好,回来问我,我首先想到的就是使用ping命令将局域网可分配的IP地址逐个遍历一遍,能ping通的就是已使用的。
那么基于思路,实现代码也没啥太难的,以java语言来实现。
linux下的代码:
public static boolean pingIp(String ip) {
     try {
            // ping -c 3 -w 100 中 ,-c 是指ping的次数 3是指ping 3次 ,-w 100
            // 以秒为单位指定超时间隔,是指超时时间为12秒 ,***这和ping速度相关谨慎更改***
            Process p = Runtime.getRuntime().exec("ping -c 1 -w 120 " + ip);
            int status = p.waitFor(); //阻塞等待
       if (status == 0) {
          return true;
            } else {
          return false;
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return false;
    }
windows下的实现:
public static boolean pingIp(String ip) {
        try {
            // ping -c 3 -w 100 中 ,-c 是指ping的次数 3是指ping 3次 ,-w 100
            // 以秒为单位指定超时间隔,是指超时时间为12秒 ,***这和ping速度相关谨慎更改***
            Process p = Runtime.getRuntime().exec("ping -n 1 -w 120 " + ip);
            InputStream input = p.getInputStream();
            BufferedReader in = new BufferedReader(new InputStreamReader(input));
            StringBuffer buffer = new StringBuffer();
            String line = null;
            while ((line = in.readLine()) != null) {
                buffer.append(line);
            }
            if(buf.toString().indexOf("请求超时") != -1) {
                return false;
            } else {
                return true;
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return false;
    }
请注意,上面两种平台下的实现,是不同的,linux下的那种实现明显要比windows下的那种实现效率高,那么为什么不都采用linux下的那种实现呢?这就是java
平台的Process.waitfor()方法的实现在windows下不能按接口定义返回状态。那么为什么windows就这样呢(我是无语了),经过测试,发现window下的ping命令有自身的bug:ping中指定的参数-w是来设置ping阻塞等待时间的,但是该参数在windows完全不起作用,这也就是在windows平台下使用Process.waitfor()不能正确返回命令执行状态的原因。哎,我只能说windows太过聪明,把真相都掩盖在他的面具下面,再次鄙视windows。
附Process.waitfor()方法的说明:
Causes the current thread to wait, if necessary, until the process represented by this Process object has terminated. This method returns immediately if the subprocess has already terminated. If the subprocess has not yet terminated, the calling thread will be blocked until the subprocess exits.
- Returns:
 - the exit value of the subprocess represented by this 
Processobject. By convention, the value0indicates normal termination. - Throws:
 - InterruptedException - if the current thread is interrupted by another thread while it is waiting, then the wait is ended and an 
InterruptedExceptionis thrown. 
局域网扫描IP的更多相关文章
- 通过安全策略限制局域网部分IP访问我的电脑
		
一旦电脑连上局域网,那么别人就容易进入自己的电脑,造成隐私被泄漏,这是我们最不愿发生的情况.因此,如果你的电脑并不需要向局域网其他用户共享资料,那么就建议采用策略,禁止局域网电脑访问自己的电脑,以保证 ...
 - python局域网alive ip侦听
		
python局域网alive ip侦听 作者:vpoet mails:vpoet_sir@163.com 注:写着玩,欢迎copy # -*- coding: cp936 -*- # coding = ...
 - CentOS7 设置局域网固定IP
		
题记: 在局域网内PC通常都是采用自动获取IP的方式从路由器拿到局域网IP的,每次PC启动后分配到的局域网IP都不一定相同.但是出于某些特殊的需求,例如要在局域网内做端口映射,需要将PC设置成使用固定 ...
 - [原创]K8Cscan插件之Cisco思科设备扫描(IP、设备型号、主机名、Boot、硬件版本)
		
[原创]K8 Cscan 大型内网渗透自定义扫描器 https://www.cnblogs.com/k8gege/p/10519321.html Cscan简介:何为自定义扫描器?其实也是插件化,但C ...
 - 在windows命令行批量ping局域网内IP
		
参考了博客园Alfred Zhao的文章<Windows平台ping测试局域网所有在用IP> 在cmd命令行运行如下命令即可: ,,) -w .%i | find "回复&quo ...
 - centos系统设置局域网静态IP
		
---恢复内容开始--- centos系统设置局域网静态IP 很多时候,我们并不希望漏油器重启之后,自己的服务器动态的获取IP,这样很不利,因为你可能装了mysql,redis,等软件,然后需要远程去 ...
 - 批量扫描IP端口程序 (适用于window&linux)
		
批量扫描IP端口,根据扫描IP导出IP命名的文件的结果.假设1.txt文件内容为127.0.0.1192.168.1.1然后我们获取文件内容IP进行扫描window .bat版本 :1.txt为文件名 ...
 - advanced ip scanner —— 局域网下 ip 及设备的扫描
		
advanced ip scanner 下载地址:Advanced IP Scanner - Download Free Network Scanner. 用于扫描当前局域网下全部设备及其 ip,构建 ...
 - 指定端口号,多线程扫描局域网内IP地址
		
小白第一次发博客,请各路大神不要喷,有错的地方还请不吝啬指教,谢谢....... 因为注释基本上已经说清楚啦,在这里就不多说什么啦,知识不够怕误人子弟 # -*- coding:utf-8 -*-im ...
 
随机推荐
- SharePoint 2013 对话框
			
The quick way to open a sharepoint 2013 dialog modal form is via Javascript below 1 2 3 4 5 function ...
 - .NET面试题汇总
			
目录 本次给大家介绍的是我收集以及自己个人保存一些.NET面试题 简介 1.C# 值类型和引用类型的区别 2.如何使得一个类型可以在foreach 语句中使用 3.sealed修饰的类有什么特点 4. ...
 - Android学习笔记(24):进度条组件ProgressBar及其子类
			
ProgressBar作为进度条组件使用,它还派生了SeekBar(拖动条)和RatingBar(星级评分条). ProgressBar支持的XML属性: Attribute Name Related ...
 - shell脚本学习笔记 (流编辑器sed)
			
sed意为流编辑器(Stream Editor),在Shell脚本和Makefile中作为过滤器使用很普遍,也就是把前一个程序的输出引入sed的输入,经过一系列编辑命令转换为另一种格式输出. sed不 ...
 - 1-2:CSS3课程入门之结构选择
			
E:nth-child(n) 表示E父元素中的第n个字节点 p:nth-child(odd){background:red}/*匹配奇数行*/ p:nth-child(even){background ...
 - JavaMelody  tomcat应用监控
			
1 下载相关jar包,maven地址 测试发现 1.57.0版本tomcat6工程登陆报错,改用版本 1.50.0是正常的 <dependency> <groupId>net. ...
 - RTSP转RTMP-HLS网页无插件视频直播-EasyNVR功能介绍-音频开启
			
EasyNVR简介 EasyNVR能够通过简单的摄像机通道配置.存储配置.云平台对接配置.CDN配置等,将统监控行业里面的高清网络摄像机IP Camera.NVR.移动拍摄设备接入到EasyNVR,E ...
 - 源码编译mysql 5.5+ 安装过程全记录
			
前言:从mysql 5.5版本开始,mysql源码安装开始使用cmake了,编译安装跟以前的版本有点不一样了. 一,安装步骤: 1.安装前准备工作 a.下载mysql源代码包,到mysql下载页面选择 ...
 - Elasticsearch集群UNASSIGNED
			
Elasticsearch集群UNASSIGNED http://shineforever.blog.51cto.com/1429204/1859734 http://www.searchtech.p ...
 - cocos2d-js添加360广告联盟插屏(通过jsb反射机制)
			
1.添加demo里的libs里的jar包 2.修改AndroidManifest.xml文件 添加权限: <uses-permission android:name="android. ...