局域网扫描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 ...
随机推荐
- 应用程序之SingleViewApplication
理论概念学习 iOS运行原理 代码结构分析 代码初步实现 一.理论学习 1⃣️.每一个应用程序都有属于自己的UIWindow,继承自UIView 2⃣️.每一个满屏的UIView都由一个UIViewC ...
- DataTable行处理
DataTable dt=new DataTable(); 新增行: DataRow addDR= mydatatable.NewRow();addDR["ID"] = " ...
- sudo apt-get update 没有公钥,无法验证下列签名
在更新系统源后,输入sudo apt-get update之后出现提示: W: GPG 错误:http://archive.ubuntukylin.com:10006 xenial InRelease ...
- 数据结构(Java语言)——Stack简单实现
栈是限制插入和删除仅仅能在一个位置上进行的表.该位置是表的末端,叫做栈的顶top.对栈的基本操作有进栈push和出栈pop,前者相当于插入.后者这是删除最后插入的元素. 栈有时又叫先进先出FIFO表. ...
- DataView中的 Sort 排序
using System.Data; using System; public class A { static void Main(string[] args) { DataTable locati ...
- 【PyCharm编辑器】之无法导入引用手动新建的包或类,报:This inspection detects names that should resolve but don't. Due to dynamic dispatch and duck typing, this is possible in a limited but useful number of cases.
一.现象描述 如下图所示,手动新建个类包calculator.py,想在test.py文件引用它,发现一直报红线,引用失败 Unresolved reference 'calculator' less ...
- 2015最流行的Android组件、工具、框架大全(转)
转自:2015最流行的Android组件.工具.框架大全 Android 是目前最流行的移动操作系统之一. 随着新版本的不断发布, Android的功能也日益强大, 涌现了很多流行的应用程序, 也催生 ...
- rtsp转rtmp、hls网页直播服务器EasyNVR前端兼容性调试:ie下的 pointer-events- none
发现问题: 之前在做EasyNVR 的web页面开发过程中,力求的都是一个播放效果的.功能的展示.对于兼容性也有注意,但有些细节还是难免有所疏忽. 内部测试发现:由于我们是流媒体的实时视频直播,在we ...
- 九度OJ 1162:I Wanna Go Home(我想回家) (最短路径)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:870 解决:415 题目描述: The country is facing a terrible civil war----cities i ...
- Grunt学习笔记【7】---- grunt-contrib-less插件详解
本文主要讲如何使用Grunt实现less文件压缩. 一 说明 less是非常常用的样式框架之一,Grunt也提供了可以编译和打包less样式文件的插件:grunt-contrib-less. 实现原理 ...