您的位置:首页 > 理论基础 > 计算机网络

Java ping命令,查看ip是否在网络中连接

2011-04-22 11:18 218 查看
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();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐