for (int i = 0; i < 255; i++) {
String host = "10.116.1." + i;
int timeOut = 3000;
try {
boolean status = InetAddress.getByName(host).isReachable(
timeOut);
if (status) {
System.out.println(host);
}
} catch (IOException e) {
e.printStackTrace();
}
}
=============================================
for (int i = 0; i < 255; i++) {
String ip = "10.116.4." + i;
// StringBuffer buf = new StringBuffer();
String s = null;
Process process;
try {
process = Runtime.getRuntime().exec(
"cmd /c " + "ping " + ip + "-n 1");
// process = Runtime.getRuntime().exec("cmd /c " +
// "ping 10.116.1.41");
BufferedReader br = new BufferedReader(new InputStreamReader(
process.getInputStream()));
while ((s = br.readLine()) != null) {
// buf.append(s + "/r/n");
if (s.contains("Received = 4")) {
System.out.println("该ip连接在网络中" + ip);
continue;
}
}
process.waitFor();
// System.out.println(buf);
} catch (Exception ex) {
ex.printStackTrace();
}
}
本文介绍两种使用Java批量探测同一网段内所有IP地址的方法。一种是利用InetAddress类检查主机是否可达;另一种则是通过执行ping命令并解析输出来判断目标IP是否在线。这两种方法均可实现对指定范围内IP的有效扫描。
9882

被折叠的 条评论
为什么被折叠?



