6 |
brianR |
1 |
/*
|
|
|
2 |
* Copyright 2010 Brian Rosenberger (Brutex Network)
|
|
|
3 |
*
|
|
|
4 |
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
5 |
* you may not use this file except in compliance with the License.
|
|
|
6 |
* You may obtain a copy of the License at
|
|
|
7 |
*
|
|
|
8 |
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
9 |
*
|
|
|
10 |
* Unless required by applicable law or agreed to in writing, software
|
|
|
11 |
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
12 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
13 |
* See the License for the specific language governing permissions and
|
|
|
14 |
* limitations under the License.
|
|
|
15 |
*/
|
|
|
16 |
|
46 |
brianR |
17 |
package net.brutex.xservices.ws.impl;
|
6 |
brianR |
18 |
|
|
|
19 |
import java.io.File;
|
|
|
20 |
import javax.jws.WebMethod;
|
|
|
21 |
import javax.jws.WebParam;
|
|
|
22 |
import javax.jws.WebService;
|
19 |
brianR |
23 |
import net.brutex.xservices.types.HostConnection;
|
6 |
brianR |
24 |
import net.brutex.xservices.types.ReturnCode;
|
46 |
brianR |
25 |
import net.brutex.xservices.util.BrutexNamespaces;
|
6 |
brianR |
26 |
import net.brutex.xservices.util.RunTask;
|
46 |
brianR |
27 |
import net.brutex.xservices.ws.ExecuteService;
|
70 |
brianR |
28 |
import net.brutex.xservices.ws.XServicesFault;
|
46 |
brianR |
29 |
|
6 |
brianR |
30 |
import org.apache.tools.ant.taskdefs.ExecTask;
|
|
|
31 |
import org.apache.tools.ant.taskdefs.optional.net.RExecTask;
|
|
|
32 |
import org.apache.tools.ant.taskdefs.optional.net.TelnetTask;
|
|
|
33 |
import org.apache.tools.ant.taskdefs.optional.ssh.SSHExec;
|
|
|
34 |
import org.apache.tools.ant.types.Commandline;
|
70 |
brianR |
35 |
import org.mozilla.javascript.Context;
|
|
|
36 |
import org.mozilla.javascript.Scriptable;
|
6 |
brianR |
37 |
|
|
|
38 |
/**
|
70 |
brianR |
39 |
*
|
6 |
brianR |
40 |
* @author Brian Rosenberger, bru@brutex.de
|
|
|
41 |
*/
|
70 |
brianR |
42 |
@WebService(targetNamespace = BrutexNamespaces.WS_XSERVICES, endpointInterface = "net.brutex.xservices.ws.ExecuteService", serviceName = "ExecuteService")
|
46 |
brianR |
43 |
public class ExecuteServiceImpl implements ExecuteService {
|
6 |
brianR |
44 |
|
70 |
brianR |
45 |
/*
|
|
|
46 |
* (non-Javadoc)
|
|
|
47 |
*
|
|
|
48 |
* @see
|
|
|
49 |
* net.brutex.xservices.ws.impl.ExecuteService#runCommand(java.lang.String,
|
|
|
50 |
* java.lang.String, long)
|
46 |
brianR |
51 |
*/
|
|
|
52 |
@WebMethod(operationName = "runCommand")
|
70 |
brianR |
53 |
public ReturnCode runCommand(@WebParam(name = "executable") String cmd,
|
|
|
54 |
@WebParam(name = "argline") String args,
|
|
|
55 |
@WebParam(name = "timeout") long timeout) {
|
6 |
brianR |
56 |
|
70 |
brianR |
57 |
return executeCommand(cmd, Commandline.translateCommandline(args),
|
|
|
58 |
null, false, null, false, true, false, timeout);
|
|
|
59 |
}
|
6 |
brianR |
60 |
|
70 |
brianR |
61 |
/*
|
|
|
62 |
* (non-Javadoc)
|
|
|
63 |
*
|
|
|
64 |
* @see
|
|
|
65 |
* net.brutex.xservices.ws.impl.ExecuteService#runCommandWithArgs(java.lang
|
|
|
66 |
* .String, java.lang.String[], long)
|
46 |
brianR |
67 |
*/
|
70 |
brianR |
68 |
|
46 |
brianR |
69 |
@WebMethod(operationName = "runCommandWithArgs")
|
70 |
brianR |
70 |
public ReturnCode runCommandWithArgs(
|
|
|
71 |
@WebParam(name = "executable") String cmd,
|
|
|
72 |
@WebParam(name = "arg") String[] args,
|
|
|
73 |
@WebParam(name = "timeout") long timeout) {
|
6 |
brianR |
74 |
|
70 |
brianR |
75 |
return executeCommand(cmd, args, null, false, null, false, true, false,
|
|
|
76 |
timeout);
|
|
|
77 |
}
|
6 |
brianR |
78 |
|
70 |
brianR |
79 |
/*
|
|
|
80 |
* (non-Javadoc)
|
|
|
81 |
*
|
|
|
82 |
* @see
|
|
|
83 |
* net.brutex.xservices.ws.impl.ExecuteService#runCommandAsync(java.lang
|
|
|
84 |
* .String, java.lang.String)
|
46 |
brianR |
85 |
*/
|
70 |
brianR |
86 |
|
46 |
brianR |
87 |
@WebMethod(operationName = "runCommandAsync")
|
70 |
brianR |
88 |
public ReturnCode runCommandAsync(
|
|
|
89 |
@WebParam(name = "executable") String cmd,
|
|
|
90 |
@WebParam(name = "argline") String args) {
|
6 |
brianR |
91 |
|
70 |
brianR |
92 |
return executeCommand(cmd, Commandline.translateCommandline(args),
|
|
|
93 |
null, true, null, false, true, false, 0);
|
|
|
94 |
}
|
6 |
brianR |
95 |
|
70 |
brianR |
96 |
/*
|
|
|
97 |
* (non-Javadoc)
|
|
|
98 |
*
|
|
|
99 |
* @see
|
|
|
100 |
* net.brutex.xservices.ws.impl.ExecuteService#runCommandAsyncWithArgs(java
|
|
|
101 |
* .lang.String, java.lang.String[])
|
46 |
brianR |
102 |
*/
|
70 |
brianR |
103 |
|
46 |
brianR |
104 |
@WebMethod(operationName = "runCommandAsyncWithArgs")
|
70 |
brianR |
105 |
public ReturnCode runCommandAsyncWithArgs(
|
|
|
106 |
@WebParam(name = "executable") String cmd,
|
|
|
107 |
@WebParam(name = "arg") String[] args) {
|
6 |
brianR |
108 |
|
70 |
brianR |
109 |
return executeCommand(cmd, args, null, true, null, false, true, false,
|
|
|
110 |
0);
|
|
|
111 |
}
|
6 |
brianR |
112 |
|
70 |
brianR |
113 |
/*
|
|
|
114 |
* (non-Javadoc)
|
|
|
115 |
*
|
|
|
116 |
* @see
|
|
|
117 |
* net.brutex.xservices.ws.impl.ExecuteService#runCommandWithSSH(java.lang
|
|
|
118 |
* .String, int, java.lang.String, java.lang.String, java.lang.String, long)
|
46 |
brianR |
119 |
*/
|
70 |
brianR |
120 |
|
46 |
brianR |
121 |
@WebMethod(operationName = "runCommandWithSSH")
|
70 |
brianR |
122 |
public ReturnCode runCommandWithSSH(
|
|
|
123 |
@WebParam(name = "host") HostConnection host,
|
|
|
124 |
@WebParam(name = "command") String cmd,
|
|
|
125 |
@WebParam(name = "timeout") long timeout) {
|
6 |
brianR |
126 |
|
70 |
brianR |
127 |
return sshExec(host.hostname, host.user, host.password, host.port, cmd,
|
|
|
128 |
timeout);
|
|
|
129 |
}
|
6 |
brianR |
130 |
|
70 |
brianR |
131 |
/*
|
|
|
132 |
* (non-Javadoc)
|
|
|
133 |
*
|
|
|
134 |
* @see
|
|
|
135 |
* net.brutex.xservices.ws.impl.ExecuteService#runCommandWithSSHKeyAuth(
|
|
|
136 |
* java.lang.String, int, java.lang.String, java.lang.String,
|
|
|
137 |
* java.lang.String, java.lang.String, long)
|
46 |
brianR |
138 |
*/
|
70 |
brianR |
139 |
|
46 |
brianR |
140 |
@WebMethod(operationName = "runCommandWithSSHKeyAuth")
|
70 |
brianR |
141 |
public ReturnCode runCommandWithSSHKeyAuth(
|
|
|
142 |
@WebParam(name = "host") HostConnection host,
|
|
|
143 |
@WebParam(name = "keyfile") String keyfile,
|
|
|
144 |
@WebParam(name = "command") String cmd,
|
|
|
145 |
@WebParam(name = "timeout") long timeout) {
|
6 |
brianR |
146 |
|
70 |
brianR |
147 |
return sshExecWithCert(host.hostname, host.user, host.password,
|
|
|
148 |
keyfile, host.port, cmd, timeout);
|
|
|
149 |
}
|
6 |
brianR |
150 |
|
70 |
brianR |
151 |
/*
|
|
|
152 |
* (non-Javadoc)
|
|
|
153 |
*
|
|
|
154 |
* @see
|
|
|
155 |
* net.brutex.xservices.ws.impl.ExecuteService#rExec(net.brutex.xservices
|
|
|
156 |
* .types.HostConnection, java.lang.String, long)
|
46 |
brianR |
157 |
*/
|
70 |
brianR |
158 |
|
46 |
brianR |
159 |
@WebMethod(operationName = "rExec")
|
70 |
brianR |
160 |
public ReturnCode rExec(@WebParam(name = "host") HostConnection host,
|
|
|
161 |
@WebParam(name = "command") String cmd,
|
|
|
162 |
@WebParam(name = "timeout") long timeout) {
|
|
|
163 |
return rexec(host.hostname, host.port, host.user, host.password, cmd,
|
|
|
164 |
timeout);
|
|
|
165 |
}
|
6 |
brianR |
166 |
|
70 |
brianR |
167 |
/*
|
|
|
168 |
* (non-Javadoc)
|
|
|
169 |
*
|
|
|
170 |
* @see
|
|
|
171 |
* net.brutex.xservices.ws.impl.ExecuteService#runTelnet(net.brutex.xservices
|
|
|
172 |
* .types.HostConnection, java.lang.String, java.lang.String,
|
|
|
173 |
* java.lang.String, long)
|
46 |
brianR |
174 |
*/
|
70 |
brianR |
175 |
|
46 |
brianR |
176 |
@WebMethod(operationName = "telnet")
|
70 |
brianR |
177 |
public ReturnCode runTelnet(@WebParam(name = "host") HostConnection host,
|
|
|
178 |
@WebParam(name = "prompt") String prompt,
|
|
|
179 |
@WebParam(name = "command") String cmd,
|
|
|
180 |
@WebParam(name = "expect") String expect,
|
|
|
181 |
@WebParam(name = "timeout") long timeout) {
|
|
|
182 |
return telnet(host.hostname, host.port, host.user, host.password, cmd,
|
|
|
183 |
timeout, prompt, expect);
|
|
|
184 |
}
|
6 |
brianR |
185 |
|
70 |
brianR |
186 |
|
|
|
187 |
public void runJScript(String script) throws XServicesFault {
|
12 |
brianR |
188 |
|
70 |
brianR |
189 |
try {
|
|
|
190 |
// Create and enter a Context. A Context stores information about
|
|
|
191 |
// the execution environment of a script.
|
|
|
192 |
Context cx = Context.enter();
|
|
|
193 |
cx.setOptimizationLevel(0);
|
|
|
194 |
cx.setLanguageVersion(Context.VERSION_1_7);
|
|
|
195 |
// cx is the Context instance you're using to run scripts
|
|
|
196 |
/*
|
|
|
197 |
* cx.setClassShutter(new ClassShutter() { public boolean
|
|
|
198 |
* visibleToScripts(String className) {
|
|
|
199 |
* if(className.startsWith("adapter")) return true;
|
|
|
200 |
* if(className.startsWith("java.lang.System") ||
|
|
|
201 |
* className.startsWith
|
|
|
202 |
* ("org.apache.tomcat.util.log.SystemLogHandler")) return true;
|
|
|
203 |
* System.out.println(className + " is blocked."); return false; }
|
|
|
204 |
* });
|
|
|
205 |
*/
|
12 |
brianR |
206 |
|
70 |
brianR |
207 |
// Initialise the standard objects (Object, Function, etc.). This
|
|
|
208 |
// must be done before scripts can be
|
|
|
209 |
// executed. The null parameter tells initStandardObjects
|
|
|
210 |
// to create and return a scope object that we use
|
|
|
211 |
// in later calls.
|
|
|
212 |
Scriptable scope = cx.initStandardObjects();
|
|
|
213 |
// Object wrappedOut = Context.javaToJS(System.out, scope);
|
|
|
214 |
// Object wrappedOut2 = Context.javaToJS(this, scope);
|
|
|
215 |
// scope.put("out", scope, wrappedOut);
|
|
|
216 |
// scope.put("exe", scope, wrappedOut2);
|
6 |
brianR |
217 |
|
70 |
brianR |
218 |
// Execute the script
|
|
|
219 |
// cx.evaluateString(scope, "importClass('java.lang.System');\n",
|
|
|
220 |
// "head", 1, null);
|
|
|
221 |
// cx.evaluateString(scope, "importPackage('java.util');\n", "head",
|
|
|
222 |
// 2, null);
|
|
|
223 |
Object obj = cx
|
|
|
224 |
.evaluateString(scope, script, "TestScript", 1, null);
|
6 |
brianR |
225 |
|
70 |
brianR |
226 |
} catch (Exception e) {
|
|
|
227 |
System.out.println(e.getMessage());
|
|
|
228 |
} finally {
|
|
|
229 |
// Exit the Context. This removes the association between the
|
|
|
230 |
// Context and the current thread and is an
|
|
|
231 |
// essential cleanup action. There should be a call to exit for
|
|
|
232 |
// every call to enter.
|
|
|
233 |
Context.exit();
|
|
|
234 |
}
|
6 |
brianR |
235 |
|
70 |
brianR |
236 |
}
|
6 |
brianR |
237 |
|
70 |
brianR |
238 |
@WebMethod(exclude = true)
|
|
|
239 |
private ReturnCode executeCommand(String executable, String[] args,
|
|
|
240 |
File dir, boolean spawn, String inputstring,
|
|
|
241 |
boolean newenvironment, boolean vmlauncher, boolean searchpath,
|
|
|
242 |
long timeout) {
|
|
|
243 |
ExecTask exe = new ExecTask();
|
|
|
244 |
RunTask runner = new RunTask(exe);
|
6 |
brianR |
245 |
|
70 |
brianR |
246 |
/*
|
|
|
247 |
* Commandline cmdl = new Commandline(); cmdl.setExecutable(executable);
|
|
|
248 |
* cmdl.addArguments(args); System.out.println(cmdl.describeCommand());
|
|
|
249 |
*/
|
6 |
brianR |
250 |
|
70 |
brianR |
251 |
exe.setExecutable(executable);
|
|
|
252 |
for (String s : args) {
|
|
|
253 |
exe.createArg().setValue(s);
|
|
|
254 |
}
|
6 |
brianR |
255 |
|
70 |
brianR |
256 |
exe.setDir(dir);
|
|
|
257 |
if (spawn) {
|
|
|
258 |
exe.setSpawn(spawn);
|
|
|
259 |
} else {
|
|
|
260 |
exe.setTimeout(timeout);
|
|
|
261 |
exe.setInputString(inputstring);
|
|
|
262 |
exe.setOutputproperty("ExecuteService.stdout");
|
|
|
263 |
exe.setErrorProperty("ExecuteService.stderr");
|
|
|
264 |
exe.setResultProperty("ExecuteService.result");
|
|
|
265 |
}
|
6 |
brianR |
266 |
|
70 |
brianR |
267 |
exe.setNewenvironment(newenvironment);
|
|
|
268 |
exe.setVMLauncher(vmlauncher);
|
|
|
269 |
exe.setSearchPath(searchpath);
|
|
|
270 |
|
|
|
271 |
return runner.postTask();
|
|
|
272 |
}
|
|
|
273 |
|
|
|
274 |
@WebMethod(exclude = true)
|
|
|
275 |
private ReturnCode sshExec(String host, String username, String password,
|
|
|
276 |
int port, String command, long timeout) {
|
|
|
277 |
SSHExec sshexec = new SSHExec();
|
|
|
278 |
RunTask runner = new RunTask(sshexec);
|
|
|
279 |
sshexec.setHost(host);
|
|
|
280 |
sshexec.setUsername(username);
|
|
|
281 |
sshexec.setPassword(password);
|
|
|
282 |
sshexec.setPort(port);
|
|
|
283 |
sshexec.setCommand(command);
|
|
|
284 |
sshexec.setTrust(true);
|
|
|
285 |
sshexec.setTimeout(timeout);
|
|
|
286 |
sshexec.setOutputproperty("SSHExec.stdout");
|
|
|
287 |
return runner.postTask();
|
|
|
288 |
}
|
|
|
289 |
|
|
|
290 |
@WebMethod(exclude = true)
|
|
|
291 |
private ReturnCode sshExecWithCert(String host, String username,
|
|
|
292 |
String passphrase, String keyfile, int port, String command,
|
|
|
293 |
long timeout) {
|
|
|
294 |
SSHExec sshexec = new SSHExec();
|
|
|
295 |
RunTask runner = new RunTask(sshexec);
|
|
|
296 |
sshexec.setHost(host);
|
|
|
297 |
sshexec.setUsername(username);
|
|
|
298 |
sshexec.setKeyfile(keyfile);
|
|
|
299 |
sshexec.setPassphrase(passphrase);
|
|
|
300 |
sshexec.setPort(port);
|
|
|
301 |
sshexec.setCommand(command);
|
|
|
302 |
sshexec.setTrust(true);
|
|
|
303 |
sshexec.setTimeout(timeout);
|
|
|
304 |
sshexec.setOutputproperty("SSHExec.stdout");
|
|
|
305 |
return runner.postTask();
|
|
|
306 |
}
|
|
|
307 |
|
|
|
308 |
@WebMethod(exclude = true)
|
|
|
309 |
private ReturnCode rexec(String host, int port, String username,
|
|
|
310 |
String password, String command, long timeout) {
|
|
|
311 |
RExecTask rexec = new RExecTask();
|
|
|
312 |
RunTask runner = new RunTask(rexec);
|
|
|
313 |
rexec.setServer(host);
|
|
|
314 |
rexec.setPort(port);
|
|
|
315 |
rexec.setUserid(username);
|
|
|
316 |
rexec.setPassword(password);
|
|
|
317 |
rexec.setCommand(command);
|
|
|
318 |
rexec.setTimeout((int) Math.round(timeout));
|
|
|
319 |
|
|
|
320 |
return runner.postTask();
|
|
|
321 |
}
|
|
|
322 |
|
|
|
323 |
@WebMethod(exclude = true)
|
|
|
324 |
private ReturnCode telnet(String host, int port, String username,
|
|
|
325 |
String password, String command, long timeout, String prompt,
|
|
|
326 |
String expect) {
|
|
|
327 |
TelnetTask rexec = new TelnetTask();
|
|
|
328 |
RunTask runner = new RunTask(rexec);
|
|
|
329 |
rexec.setServer(host);
|
|
|
330 |
rexec.setPort(port);
|
|
|
331 |
rexec.setUserid(username);
|
|
|
332 |
rexec.setPassword(password);
|
|
|
333 |
rexec.setTimeout((int) Math.round(timeout));
|
|
|
334 |
|
|
|
335 |
rexec.createRead().addText(prompt);
|
|
|
336 |
rexec.createWrite().addText(command);
|
|
|
337 |
rexec.createRead().addText(expect);
|
|
|
338 |
|
|
|
339 |
return runner.postTask();
|
|
|
340 |
}
|
6 |
brianR |
341 |
}
|