博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Pinger2
阅读量:6884 次
发布时间:2019-06-27

本文共 2925 字,大约阅读时间需要 9 分钟。

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;

    

    // 开始时间

    private String startTime;


    // 结束时间

    private String endTime;

    

    /**

     * 构造函数

     * @param ip

     * @param timesNum

     */

    public Pinger(String ip,int timesNum){

        this.ip=ip;

        this.timesNum=timesNum;

    }

    

    /**

     * Ping后计算响应的个数

     * @return

     * @throws Exception

     */

    public int countReply() throws Exception{

        String destIp=ip;

        int maxCount=timesNum;

        

        LineNumberReader input = null;

        try {

            startTime=DateTimeUtil.getCurrDateTime();

            

            // 根据操作系统组合命令

            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);

            } else if (osName.startsWith("Linux")) {

                return parseLinuxMsg(reponse, 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());


                }

            }

            

            endTime=DateTimeUtil.getCurrDateTime();

        }

        

        return 0;

    }

    

    /**

     * ping次数和响应数相等算ping通

     * @return

     * @throws Exception

     */

    public boolean isPass() throws Exception{

        return countReply()==timesNum;

    }

    

    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 String getStartTime() {

        return startTime;

    }


    public String getEndTime() {

        return endTime;

    }

    

    public static void main(String[] args) throws Exception{

        Pinger p=new Pinger("www.163.com",5);

        

        System.out.println(p.countReply());

        System.out.println(p.getStartTime());

        System.out.println(p.getEndTime());

    }

}

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

你可能感兴趣的文章
seajs初尝 加载jquery返回null解决学习日志
查看>>
django项目自定义错误显示页面
查看>>
mysql跨服务器表备份
查看>>
Swift中声明initialize方法
查看>>
关于监听和监听日志大小
查看>>
JavaSE视频教程 第五章 访问权限控制
查看>>
远程桌面的设置方法
查看>>
转 iOS学习之Socket使用简明教程- AsyncSocket
查看>>
JAVA SOCKET connect超时设置是如何实现的?
查看>>
Extjs中左边treepanel右边panel动态加载jsp页面
查看>>
url解析
查看>>
MessageBox的常见用法
查看>>
RAID磁盘阵列
查看>>
python Function(函数)
查看>>
LINUX配置JMX监控tomcat7
查看>>
流媒体服务器之nginx的rtmp模块
查看>>
zabbix监控软件的使用排错
查看>>
003.android资源文件剖析(Resources)
查看>>
搭建Grunt开发环境
查看>>
我的友情链接
查看>>