zoukankan      html  css  js  c++  java
  • Pinger2

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

  • 相关阅读:
    springboot拦截器的拦截配置和添加多个拦截器
    ASCII对照
    爬虫出现403错误解决办法
    PhantomJS在Selenium中被标记为过时的应对措施
    Selenium 之订制启动Chrome的选项(Options)
    Selenium+PhantomJS使用时报错原因及解决方案
    python爬虫之xpath的基本使用
    JSONObject类的引用必须jar包
    selenium之使用chrome浏览器测试(附chromedriver与chrome的对应关系表)
    PhantomJS 与python的结合
  • 原文地址:https://www.cnblogs.com/heyang78/p/3371730.html
Copyright © 2011-2022 走看看