Pinger

简介:
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;


public class Pinger{
    // IP地址
    private String ip;
    
    // Ping次数    
    private int timesNum;
    
    /**
     * 构造函数
     * @param ip
     * @param timesNum
     */
    public Pinger(String ip,int timesNum){
        this.ip=ip;
        this.timesNum=timesNum;
    }
    
    /**
     * ping次数和响应数相等算ping通
     * @return
     * @throws Exception
     */
    public boolean isPass() throws Exception{
        String destIp=ip;
        int maxCount=timesNum;
        
        LineNumberReader input = null;
        try {
            // 根据操作系统组合命令
            String osName = System.getProperties().getProperty("os.name");
            String pingCmd = null;
            if (osName.startsWith("Windows")) {
                pingCmd = "cmd /c ping -n {0} {1}";
                pingCmd = MessageFormat.format(pingCmd, maxCount, destIp);
            } else if (osName.startsWith("Linux")) {
                pingCmd = "ping -c {0} {1}";
                pingCmd = MessageFormat.format(pingCmd, maxCount, destIp);
            } else {
                throw new Exception("not support OS");
            }
            
            // ping完得到响应
            Process process = Runtime.getRuntime().exec(pingCmd);
            InputStreamReader ir = new InputStreamReader(process
                    .getInputStream());
            input = new LineNumberReader(ir);
            String line;
            List<String> reponse = new ArrayList<String>();

            while ((line = input.readLine()) != null) {
                if (!"".equals(line)) {
                    reponse.add(line);
                    // System.out.println("====:" + line);
                }
            }
            
            // 分析响应
            if (osName.startsWith("Windows")) {
                return parseWindowsMsg(reponse, maxCount)==maxCount;
            } else if (osName.startsWith("Linux")) {
                return parseLinuxMsg(reponse, maxCount)==maxCount;
            }

        } catch (IOException e) {
            System.out.println("IOException   " + e.getMessage());

        } finally {
            if (null != input) {
                try {
                    input.close();
                } catch (IOException ex) {
                    System.out.println("close error:" + ex.getMessage());

                }
            }
        }
        
        return false;
    }
    
    private int parseWindowsMsg(List<String> reponse, int total) {
        int countTrue = 0;
        int countFalse = 0;
        for (String str : reponse) {
            if (str.startsWith("来自") || str.startsWith("Reply from")) {
                countTrue++;
            }
            if (str.startsWith("请求超时") || str.startsWith("Request timed out")) {
                countFalse++;
            }
        }
        return countTrue;
    }

    private int parseLinuxMsg(List<String> reponse, int total) {
        int countTrue = 0;
        for (String str : reponse) {
            if (str.contains("bytes from") && str.contains("icmp_seq=")) {
                countTrue++;
            }
        }
        return countTrue;
    }
    
    public static void main(String[] args) throws Exception{
        Pinger p=new Pinger("123.abc.xyz.com",5);
        
        System.out.println(p.isPass());
    }

}
























本文转自张昺华-sky博客园博客,原文链接:http://www.cnblogs.com/xiandedanteng/p/3370851.html,如需转载请自行联系原作者


相关文章
|
Kubernetes 容器 Perl
kubeadm初始化k8s集群延长证书过期时间
kubeadm初始化k8s集群延长证书过期时间
|
Kubernetes Linux Docker
银河麒麟v10离线安装docker二进制包
银河麒麟v10离线安装docker二进制包
2011 0
|
存储 Prometheus Kubernetes
k8s安装kube-promethues(超详细)
k8s安装kube-promethues(超详细)
6910 0
k8s安装kube-promethues(超详细)
|
IDE Java Maven
idea2020版Maven依赖成功导入但仍然报错找不到包解决
idea2020版Maven依赖成功导入但仍然报错找不到包解决
1789 0
idea2020版Maven依赖成功导入但仍然报错找不到包解决
|
Java Maven
【Maven】下载配置maven以及IDEA配置maven详情
【Maven】下载配置maven以及IDEA配置maven详情
2037 0
|
Prometheus 监控 Kubernetes
Prometheus+Grafana+Alertmanager搭建全方位的监控告警系统-超详细文档(上)
Prometheus+Grafana+Alertmanager搭建全方位的监控告警系统-超详细文档
|
存储 运维 监控
运维的容灾方案
运维的容灾方案
|
网络协议
Github下载速度太慢怎么办?完美解决
Github 下载速度太慢修复问题 助力快速解决 Github 下载项目时造成的 几K/S 的窘境 为初学者节省宝贵的时间,避免采坑! Github 下载速度过慢,非常影响我们开发,设计! 原因:为了网络安全,等其他因素。
58225 0
|
算法 计算机视觉
指针式仪表的表盘自动识别算法
指针式仪表的表盘自动识别算法
2209 0
指针式仪表的表盘自动识别算法
|
JSON API 数据格式
python对话通义千问
python对话通义千问
2782 1