1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package net.smartlab.ssh;
24
25 import java.io.IOException;
26 import java.io.InputStream;
27 import java.io.OutputStream;
28 import ch.ethz.ssh2.ChannelCondition;
29 import ch.ethz.ssh2.Connection;
30 import ch.ethz.ssh2.Session;
31 import net.smartlab.web.BusinessException;
32
33
34
35
36
37
38
39
40 public class Shell {
41
42
43
44
45 private String username;
46
47
48
49
50 private String password;
51
52
53
54
55 private String host;
56
57
58
59
60 private int port = 22;
61
62
63
64
65 private long timeout = 1000;
66
67
68
69
70 private Connection connection;
71
72
73
74
75 private Session session;
76
77
78
79
80 private OutputStream out;
81
82
83
84
85 private InputStream in;
86
87
88
89
90 private byte[] buffer = new byte[4096];
91
92
93
94
95
96
97
98 public String getUsername() {
99 return username;
100 }
101
102
103
104
105
106
107 public void setUsername(String username) {
108 this.username = username;
109 }
110
111
112
113
114
115
116 public String getPassword() {
117 return password;
118 }
119
120
121
122
123
124
125 public void setPassword(String password) {
126 this.password = password;
127 }
128
129
130
131
132
133
134 public String getHost() {
135 return host;
136 }
137
138
139
140
141
142
143 public void setHost(String host) {
144 this.host = host;
145 }
146
147
148
149
150
151
152 public int getPort() {
153 return port;
154 }
155
156
157
158
159
160
161 public void setPort(int port) {
162 this.port = port;
163 }
164
165
166
167
168
169
170 public long getTimeout() {
171 return timeout;
172 }
173
174
175
176
177
178
179 public void setTimeout(long timeout) {
180 this.timeout = timeout;
181 }
182
183
184
185
186
187
188
189
190
191
192 public void open() throws BusinessException, IOException {
193 connection = new Connection(host, port);
194 connection.connect();
195 if (!connection.authenticateWithPassword(username, password)) {
196 throw new BusinessException("Authentication failed");
197 }
198 session = connection.openSession();
199 session.requestPTY("dumb", 80, 200, 0, 0, null);
200 session.startShell();
201 out = session.getStdin();
202 in = session.getStdout();
203 }
204
205
206
207
208
209
210 public String update() throws IOException {
211 StringBuffer result = new StringBuffer();
212 int status = session.waitForCondition(ChannelCondition.STDOUT_DATA, timeout);
213 while (status != 1) {
214 int read = in.read(buffer);
215 result.append(new String(buffer, 0, read));
216 status = session.waitForCondition(ChannelCondition.STDOUT_DATA, timeout);
217 }
218 return result.toString();
219 }
220
221
222
223
224
225
226
227 public String execute(String command) throws IOException {
228 out.write(command.getBytes());
229 out.write('\n');
230 out.flush();
231 return this.update();
232 }
233
234
235
236
237
238
239 public String interrupt() throws IOException {
240 out.write(0x03);
241 out.write(0x04);
242 out.flush();
243 return this.update();
244 }
245
246
247
248
249 public void close() {
250 session.close();
251 connection.close();
252 }
253
254
255
256
257 public String toString() {
258 return username + "@" + host + ":" + port;
259 }
260 }