CMDTarget.java
3.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
package com.sitech.ismp.coll;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.InputStreamReader;
import java.util.Vector;
public class CMDTarget {
String ObjectIP = null;
static String CONTAINERCMD = "";
public String SHELLPATH = null;
boolean state = true;
static {
try {
CONTAINERCMD = "";
} catch (Exception e) {
CONTAINERCMD = "";
}
}
public CMDTarget() {
}
public void init() {
}
public void setContainer(String cCMD) {
this.CONTAINERCMD = cCMD;
}
public boolean getState() {
return state;
}
public boolean getPingState(String ipaddress) {
/*
* /etc/ping -c 5 172.16.1.5 5 packets transmitted, 5 packets received,
* 0% packet loss round-trip min/avg/max = 0/0/0 ms
*
*/
String cmd = "/etc/ping -c 5 " + ipaddress;
Vector aa = execute(cmd);
String value = "";
if (aa.size() > 0)
value = (String) aa.elementAt(0);
for (int i = 1; i < aa.size(); i++) {
value = value + "\n" + (String) aa.elementAt(i);
}
return true;
}
public void releaseResources() {
}
Vector recordSet = new Vector();
public Vector getKPISet(String cmdstr) {
// String cmd = RPCCMD + " " + ObjectIP + " " + user + " " + passwd + "
// " + cmdstr;
String cmd = cmdstr;
return execute(cmd);
}
public String getKPIValue(String cmdstr) {
// String cmd = RPCCMD + " " + ObjectIP + " " + user + " " + passwd + "
// " + cmdstr;
String cmd = cmdstr;
Vector aa = execute(cmd);
String value = "";
if (aa.size() > 0)
value = (String) aa.elementAt(0);
for (int i = 1; i < aa.size(); i++) {
value = value + "\n" + (String) aa.elementAt(i);
}
return value;
}
public String getCommandResult(String command) throws Exception {
return getKPIValue(command);
}
public boolean init(String host, String user, String shellpath)
throws Exception {
ObjectIP = host;
SHELLPATH = shellpath;
return true;
}
public boolean release() {
return true;
}
public String getSHELLPATH() {
return SHELLPATH;
}
private Vector execute(String cCommand) {
// String shellCommand="/nms/workdir/zang/ssh.sh "+shell_Command;
// container
Vector vResult = new Vector();
try {
String shellCommand = this.CONTAINERCMD + " " + cCommand;
// System.out.println("Command is :" + shellCommand);
Runtime sys = Runtime.getRuntime();
Process process = sys.exec(shellCommand);
DataInputStream in = new DataInputStream(process.getInputStream());
BufferedReader reader = new BufferedReader(
new InputStreamReader(in));
String line;
byte[] cbuf = new byte[1024];
do {
line = reader.readLine();
// len = reader.read(cbuf);
if (line == null) {
break;
} else {
vResult.addElement(line);
}
/*
* int len = in.read(cbuf); if(len<1){ break; } else{
* display(cbuf,len); //System.out.println(new String(cbuf)); }
*/
} while (true);
reader.close();
process.destroy();
} catch (Exception e) {
return null;
}
return vResult;
}
void display(byte[] cbuf, int len) {
for (int i = 0; i < len; i++) {
System.out.print(cbuf[i]);
System.out.print(" ");
}
}
}