HostDetectCtrl.java
5.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
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;
}*/
}