TelnetThread.java
7.09 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
package com.sitech.util.upload;
import org.apache.commons.net.telnet.TelnetClient;
import org.apache.log4j.Logger;
import java.io.InputStream;
import java.io.PrintStream;
import java.util.List;
import java.util.Vector;
public class TelnetThread implements RomoteController {
private Logger logger = Logger.getLogger(TelnetThread.class);
// 是否认证完毕
private boolean autorized = false;
private String ip = null;
private int port = 23;
private String username = null;
private String password = null;
private String specPrompt = null;
// 存放命令执行结果
private StringBuffer result = new StringBuffer();
private TelnetClient telnet = new TelnetClient();
private InputStream is;
private PrintStream os;
public TelnetThread(String ip, int port, String username, String password) {
this.ip = ip;
this.port = port;
this.username = username;
this.password = password;
}
public TelnetThread(String ip, int port, String username, String password, String specPrompt) {
this.ip = ip;
this.port = port;
this.username = username;
this.password = password;
this.specPrompt = specPrompt;
}
public void initial() {
try {
telnet.connect(this.ip, this.port);
is = telnet.getInputStream();
os = new PrintStream(telnet.getOutputStream());
} catch (Exception e) {
logger.error("创建telnet连接失败.", e);
return;
}
if (!telnet.isConnected()) {
return;
}
read("login: ");
doCommand(username);
read("Password: ");
doCommand(password);
getResult();
}
public String read(String pattern) {
try {
char lastChar = pattern.charAt(pattern.length() - 1);
StringBuffer sb = new StringBuffer();
char ch = (char) is.read();
while (true) {
sb.append(ch);
if (ch == lastChar) {
if (sb.toString().endsWith(pattern)) {
logger.info(sb.toString());
return sb.toString();
}
}
ch = (char) is.read();
}
} catch (Exception e) {
logger.error("Exception while read " + pattern, e);
}
return null;
}
public String getResultAsStr(String cmd) {
doCommand(cmd);
return getResult();
}
public Vector<String> getResultAsVector(String command) {
Vector<String> vResult = new Vector<String>();
try{
this.doCommand(command);
String strResult = this.getResult();
if (strResult != null) {
String[] results = strResult.split("\n");
for(String line : results){
if(line.indexOf("have mail in") >=0 ){
continue;
}
vResult.add(line);
}
}
}catch (Exception e) {
logger.error("Exception while do command:" + command, e);
}
String logInfo = "-- do command: " + command +"\n";
for(String line : vResult){
logInfo += line + "\n";
}
logger.info(logInfo);
return vResult;
}
public String getResult() {
try {
char ch = (char) is.read();
do {
result.append(ch);
if (specPrompt != null && !specPrompt.trim().equals("")) {
if (result.toString().endsWith(specPrompt)) {
break;
}
} else if (((!result.toString().endsWith("HELP <command>")) && result.toString().endsWith(">"))
|| result.toString().endsWith("$")) {
break;
}
ch = (char) is.read();
} while (true);
autorized = true;
} catch (Exception e) {
autorized = false;
logger.error("Exception while getResult.", e);
return null;
}
String rs = result.toString();
int endIdx = rs.lastIndexOf("\n");
if(endIdx > 0){
rs = rs.substring(0, endIdx);
}
return rs;
}
public boolean isRunning(String keywords, String username) {
String command = "ps -ef | grep " + keywords;
if (username != null && !"".equals(username)) {
command += " | grep " + username;
}
command = command + " | grep -v grep";
doCommand(command);
String result = this.getResult();
if (result != null && result.indexOf(keywords) != -1) {
return true;
} else {
return false;
}
}
public boolean isRunning(String keywords) {
doCommand("ps -ef | grep " + keywords);
String result = this.getResult();
result = result.replaceAll("grep " + keywords, " ");
if (result != null && result.indexOf(keywords) != -1) {
return true;
} else {
return false;
}
}
public void doCommand(String command) {
try {
os.println(command);
os.flush();
result = new StringBuffer();
} catch (Exception e) {
logger.error("Exception while doCommand " + command, e);
}
}
public void receivedNegotiation(int negotiation_code, int option_code) {
}
public void close() {
try {
byte b = 0x03;
os.write(b);
os.flush();
this.doCommand("exit");
if (telnet != null && telnet.isConnected()) {
telnet.disconnect();
}
telnet = null;
} catch (Exception e) {
logger.error("Exception while close connnection.", e);
}
}
public boolean isAuthorized() {
return this.autorized;
}
public boolean isExistDir(String dir) {
boolean success = true;
this.doCommand("cd " + dir);
if (this.getResult().indexOf(dir + ": not found") != -1) {
success = false;
}
return success;
}
public int childCountOfDir(String dir) {
int count = 0;
try {
if (this.isExistDir(dir)) {
doCommand("cd " + dir + ";ls -l | wc -l");
String s = null;
s = getResult();
String[] s1 = s.split("\n");
if (s1 != null) {
count = new Integer(s1[1].trim()).intValue() - 1;
}
} else {
return -1;
}
} catch (Exception e) {
e.printStackTrace();
return -1;
}
return count;
}
public boolean createDir(String dir) {
boolean isExist = false;
if (dir != null && !dir.trim().equals("") && this.isExistDir(dir)) {
isExist = true;
}
// 逐层分析目录,如果没有则创建.
if (dir != null && !dir.trim().equals("") && !this.isExistDir(dir)) {
String[] split_dir = dir.split("/");
for (int i = 0; i < split_dir.length; i++) {
String path = "";
for (int j = 0; j <= i; j++) {
if (split_dir[j] != null && !split_dir[j].equals("")) {
path += "/" + split_dir[j];
}
}
if (path != null && !path.equals("")) {
if (!this.isExistDir(path)) {
this.doCommand("mkdir " + path);
this.getResult();
}
if (!this.isExistDir(path)) {
return false;
}
}
}
if (this.isExistDir(dir)) {
isExist = true;
}
}
return isExist;
}
public boolean isUsed(int port) {
this
.doCommand("netstat -an | grep -v TIME_WAIT | awk '{print $4}' | grep "
+ port + "");
String result = this.getResult();
if (result != null && result.indexOf("" + port) >= 0) {
return true;
}
return false;
}
public String getIpAddr() {
return this.ip;
}
public String executeCommand(String command) throws Exception
{
return "";
}
public String executeCommand(List<String> commandList) throws Exception
{
return "";
}
public RomoteController cloneObject()
{
return null;
}
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
TelnetThread tt = new TelnetThread("172.21.1.71", 23, "yfbnms",
"yfbnms");
tt.initial();
tt.doCommand("prtconf |grep 'Number Of Processors:'");
System.out.println(tt.getResult());
tt.close();
}
}
public String getError(){
return "";
}
}