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集群延长证书过期时间
|
网络虚拟化
VLAN(虚拟局域网)简介与配置指南
VLAN(虚拟局域网)简介与配置指南
842 1
|
存储 Prometheus Kubernetes
k8s安装kube-promethues(超详细)
k8s安装kube-promethues(超详细)
6967 0
k8s安装kube-promethues(超详细)
|
Kubernetes Linux Docker
银河麒麟v10离线安装docker二进制包
银河麒麟v10离线安装docker二进制包
2111 0
|
IDE Java Maven
idea2020版Maven依赖成功导入但仍然报错找不到包解决
idea2020版Maven依赖成功导入但仍然报错找不到包解决
1828 0
idea2020版Maven依赖成功导入但仍然报错找不到包解决
|
弹性计算 关系型数据库 数据库
手把手带你从自建 MySQL 迁移到云数据库,一步就能脱胎换骨
阿里云瑶池数据库来开课啦!自建数据库迁移至云数据库 RDS原来只要一步操作就能搞定!点击阅读原文完成实验就可获得一本日历哦~
|
10月前
|
安全 网络安全 网络架构
电脑提示“Waiting for Device”的解决方案
电脑提示“Waiting for Device”的解决方案
807 4
|
Java Maven
【Maven】下载配置maven以及IDEA配置maven详情
【Maven】下载配置maven以及IDEA配置maven详情
2087 0
|
Prometheus 监控 Kubernetes
Prometheus+Grafana+Alertmanager搭建全方位的监控告警系统-超详细文档(上)
Prometheus+Grafana+Alertmanager搭建全方位的监控告警系统-超详细文档
|
存储 弹性计算 Cloud Native
2024年 | 5月云大使推广奖励规则
简介: ①5月首单推广实付金额≥90元,领50元奖励。②5月推广累计当月首购总额激励活动最高奖励3万元。③【近期云大使规则升级】延长奖励周期、新增奖励订单类型、优化推广奖励限制、保护新手大使推广、缩短奖励发放周期。④推荐企业认证新用户首购最高奖励45%。