java编写根据ip判断地址是否ping通

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;


public class test {


public static void main(String[] args) {
String ip = "192.168.33.111";
if (isConnect(ip)) {
System.out.println("网络状态:"+"网络能ping通");
} else {
System.out.println("网络状态:"+"网络ping不通");
}
}


/**
* 根据ip判断当前ip是否能够ping通

* @param ip
* @return
*/
public static boolean isConnect(String ip) {
boolean bool = false;
Runtime runtime = Runtime.getRuntime();
try {
Process process = runtime.exec("ping " + ip);
InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line = null;
StringBuffer sb = new StringBuffer();
while ((line = br.readLine()) != null) {
sb.append(line);
// 优化速度
if (line.indexOf("请求超时") >= 0) {
// System.out.println(ip + "网络断开,时间 " + new Date());
return false;
}
}
is.close();
isr.close();
br.close();


if (null != sb && !sb.toString().equals("")) {
if (sb.toString().indexOf("TTL") > 0) {
// 网络畅通
// System.out.println(ip + "网络正常 ,时间" + new Date());
bool = true;
} else {
// 网络不畅通
// System.out.println(ip + "网络断开,时间 " + new Date());
bool = false;
}
}
} catch (Exception e) {
e.printStackTrace();
}
return bool;
}
}

你可能感兴趣的:(技术开发,Java技术开发)