通过java ping 各个主机的连通性

1 篇文章 0 订阅
1 篇文章 0 订阅
今天闲来无事,整理了一下以前的小东西,有点笨拙,实难拿出手,望大神 斧正。
需求:给了一些IP地址,让ping一下 那些地址是通的,哪些是不通的。
于是在网上查找了一下资料 自己整理了一下,就写出了这个小东西。

import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class PingServer {
public static void main(String[] args) {
List<String> list = new ArrayList<String>();
list.add("192.168.100.137");
list.add("192.168.100.138");
list.add("192.168.100.139");
list.add("192.168.100.140");
list.add("192.168.100.142");
 Map<String, String> map = new HashMap<String, String>();
 map = PingServer.ping(list);
for (Map.Entry<String, String> entry : map.entrySet()) {
   System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
  }
}

public static Map ping(List<String> ipList) {
String result = null;
 Map<String, String> map = new HashMap<String, String>();
for (String ip : ipList) {
if (ip != null && !"".equals(ip)) {
try {
result = hostReachable(ip);
if(result.equals("false")){
String msg=" 不可达";
map.put(ip, msg);
}else{
map.put(ip, result);
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
}
return map;
}

private static String hostReachable(String ip) throws UnsupportedEncodingException {//主机ip可达
        if(ip==null){
            return "false";
        }
StringBuffer sb = new StringBuffer();
Runtime run = Runtime.getRuntime();
Process process = null;
InputStream is = null;
try {
    Properties props = System.getProperties();
String os = props.getProperty("os.name");
// windows
if (os.startsWith("W")) {
    process = run.exec("ping " + ip + " -n 3 -w 8000");//ping3次 如果8000毫秒后没有收到结果为不通
// linux
} else {
if (ip.indexOf(":") < 0) {//没有“:” pingipv4 有":" pingipv6
process = run.exec("ping " + ip + " -c 3 -w 8000");
} else {
process = run.exec("ping6 " + ip + " -c 3 -w 8000");
}
}
is = process.getInputStream();
int t = 0;
while ((t = is.read()) != -1) {
sb.append((char) t);
}
} catch (IOException ex) {
return "false";
} finally {
try {
if (is != null) {
is.close();
}
if (process != null) {
process.destroy();
}

} catch (IOException e) {
return "false";
}
}
String resultUpperCase = sb.toString().toUpperCase();//stringbuffer 转换string类型并大写
int tag = resultUpperCase.indexOf("TTL");//查找第一个 字符所在的位置
int ms = resultUpperCase.indexOf("MS")+2;
String res = null;
if (resultUpperCase.getBytes().length != resultUpperCase.length()) {
String result = new String(sb.toString().getBytes("iso8859-1"),"gb2312"); 
//如果通 返回相应时间
// System.out.println("resultUpperCase:"+result.trim());//打印ping结果
if (tag >= 0) {
res = result.substring(result.indexOf("时间")+2,result.indexOf("ms")+2);//相应时间
// System.out.println("相应时间中文:"+res);
}

}else{
if (tag >= 0) {
res = resultUpperCase.substring(resultUpperCase.indexOf("TIME")+4,resultUpperCase.indexOf("MS")+2);//相应时间
// System.out.println("相应时间英文:"+res);
}
}

if (tag == -1) {//没有找到
return "false";
} else {
//log("ok");
            return res;
}
}

public static boolean isChinese(String str){//判断是否为中文
       boolean temp = false;
       Pattern p=Pattern.compile("[\\u4e00-\\u9fa5]");
       Matcher m=p.matcher(str);
       if(m.find()){
           temp =  true;
       }
       return temp;
   }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值