HostDetectCtrl.java 5.06 KB
package com.ui.ctrl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/hostDetect")
public class HostDetectCtrl {

    private Logger logger = LoggerFactory.getLogger(getClass());

    private static Map<String, String> msgMap = new HashMap<String, String>();

    @RequestMapping("/checkSshPort")
    @ResponseBody
    public String checkHostReachable(String changedIps) {
        if(changedIps!=null&&changedIps.length()>0){
            if(msgMap.containsKey(changedIps)){
                String str=msgMap.get(changedIps);
                if("allReachable".equals(str)){
                    msgMap.clear();
                }
                return str;
            }else{
                String firtDetect="NotReadyIP first=="+changedIps;
                msgMap.put(changedIps,firtDetect);

                //启动一个
                MyThread1 mh = new MyThread1(changedIps);
                new Thread(mh).start();
                return firtDetect;
            }
        }else{
            return "NotReadyIP is null "+changedIps;
        }

    }

    /////测试  ,可删除
    @RequestMapping("/checkSshPortTest")
    @ResponseBody
    public String checkSshPortTest(String changedIps) {
        return checkHostReachable_connect(changedIps);
    }

    class MyThread1 implements Runnable{
        private String changedIps;
        MyThread1(String changedIps){
            this.changedIps = changedIps;
        }
        public void run(){
            String t="";
            int times=0;
            do{
                times++;
                t=checkHostReachable_connect(changedIps);
                msgMap.put(changedIps,t);
                logger.info("Thread detect result times is  "+times +" , "+t);
            }while(times<=50&&!"allReachable".equals(t));

            if(times>50&&!"allReachable".equals(t)){
                msgMap.clear();
            }
        }
    }

    private String checkHostReachable_connect(String changedIps){
        if(changedIps!=null&&changedIps.length()>0){
            String  allReacheableStr="allReachable";
            for(String ip:changedIps.split(",")){
                if(org.apache.commons.lang.StringUtils.isNotBlank(ip)){
                    logger.error("begin checkHostReachable  "+ip);
                    boolean reachable=isHostConnectableWithShell(ip);
                    logger.error("checkHostReachable reachable "+reachable);
                    if(reachable==false){
                        allReacheableStr="NotReadyIP "+ip;
                        break;
                    }
                }
            }
            return allReacheableStr;
        }else{
            return "allReachable";
        }
    }


    private boolean isHostConnectableWithShell(String hostIP) {
        Process process = null;
        int timeout=10;//设置10秒
        //int port=22;
        List<String> processList = new ArrayList<String>();
        try {
            String cmd="nc -w "+timeout+" -vz "+hostIP+" 22";
            process = Runtime.getRuntime().exec(cmd);
            BufferedReader input = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line = "";
            while ((line = input.readLine()) != null) {
                processList.add(line);
            }
            input.close();
        } catch (IOException e) {
            // e.printStackTrace();
            logger.error("isHostConnectableWithShell error ",e);
        }
        if(processList.size()>0&&processList.get(0).indexOf("succeeded")>0){
            return true;
        }
        return false;
    }

    /*private  boolean isHostConnectable(String host) {
        int timeout=1*60*1000;//设置1分钟
        int port=22;
        Socket socket = new Socket();
        try {
            socket.connect(new InetSocketAddress(host, port), timeout);//设置1分钟 1*60*1000
        } catch(java.net.SocketTimeoutException e){
            logger.error("isHostConnectable SocketTimeoutException ",e);
            return false;
        } catch (IOException e) {
            // e.printStackTrace();
            logger.error("isHostConnectable IOException ",e);
            return false;
        } catch (Exception e) {
            // e.printStackTrace();
            logger.error("isHostConnectable Exception",e);
            return false;
        } finally {
            try {
                if(socket!=null){
                    socket.close();
                }
            } catch (IOException e) {
                logger.error("isHostConnectable finally ",e);
                return false;
            } catch (Exception e) {
                logger.error("isHostConnectable finally ",e);
                return false;
            }
        }
        return true;
    }*/



}