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 |
|
|
|
17 |
package net.brutex.xservices.ws;
|
|
|
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;
|
|
|
25 |
import net.brutex.xservices.util.RunTask;
|
|
|
26 |
import org.apache.tools.ant.taskdefs.ExecTask;
|
|
|
27 |
import org.apache.tools.ant.taskdefs.optional.net.RExecTask;
|
|
|
28 |
import org.apache.tools.ant.taskdefs.optional.net.TelnetTask;
|
|
|
29 |
import org.apache.tools.ant.taskdefs.optional.ssh.SSHExec;
|
|
|
30 |
import org.apache.tools.ant.types.Commandline;
|
|
|
31 |
|
|
|
32 |
/**
|
|
|
33 |
*
|
|
|
34 |
* @author Brian Rosenberger, bru@brutex.de
|
|
|
35 |
*/
|
|
|
36 |
@WebService(targetNamespace="http://ws.xservices.brutex.net", name="ExecuteService")
|
|
|
37 |
public class ExecuteService {
|
|
|
38 |
|
|
|
39 |
@WebMethod(operationName = "runCommand")
|
|
|
40 |
public ReturnCode runCommand(@WebParam(name = "executable") String cmd,
|
|
|
41 |
@WebParam(name = "argline") String args,
|
|
|
42 |
@WebParam(name = "timeout") long timeout) {
|
|
|
43 |
|
|
|
44 |
return executeCommand(cmd,
|
|
|
45 |
Commandline.translateCommandline(args),
|
|
|
46 |
null,
|
|
|
47 |
false,
|
|
|
48 |
null,
|
|
|
49 |
false,
|
|
|
50 |
true,
|
|
|
51 |
false,
|
|
|
52 |
timeout);
|
|
|
53 |
}
|
|
|
54 |
|
|
|
55 |
@WebMethod(operationName = "runCommandWithArgs")
|
|
|
56 |
public ReturnCode runCommandWithArgs(@WebParam(name = "executable") String cmd,
|
|
|
57 |
@WebParam(name = "arg") String[] args,
|
|
|
58 |
@WebParam(name = "timeout") long timeout) {
|
|
|
59 |
|
|
|
60 |
return executeCommand(cmd,
|
|
|
61 |
args,
|
|
|
62 |
null,
|
|
|
63 |
false,
|
|
|
64 |
null,
|
|
|
65 |
false,
|
|
|
66 |
true,
|
|
|
67 |
false,
|
|
|
68 |
timeout);
|
|
|
69 |
}
|
|
|
70 |
|
|
|
71 |
@WebMethod(operationName = "runCommandAsync")
|
|
|
72 |
public ReturnCode runCommandAsync(@WebParam(name = "executable") String cmd,
|
|
|
73 |
@WebParam(name = "argline") String args) {
|
|
|
74 |
|
|
|
75 |
return executeCommand(cmd,
|
|
|
76 |
Commandline.translateCommandline(args),
|
|
|
77 |
null,
|
|
|
78 |
true,
|
|
|
79 |
null,
|
|
|
80 |
false,
|
|
|
81 |
true,
|
|
|
82 |
false,
|
|
|
83 |
0);
|
|
|
84 |
}
|
|
|
85 |
|
|
|
86 |
@WebMethod(operationName = "runCommandAsyncWithArgs")
|
|
|
87 |
public ReturnCode runCommandAsyncWithArgs(@WebParam(name = "executable") String cmd,
|
|
|
88 |
@WebParam(name = "arg") String[] args) {
|
|
|
89 |
|
|
|
90 |
return executeCommand(cmd,
|
|
|
91 |
args,
|
|
|
92 |
null,
|
|
|
93 |
true,
|
|
|
94 |
null,
|
|
|
95 |
false,
|
|
|
96 |
true,
|
|
|
97 |
false,
|
|
|
98 |
0);
|
|
|
99 |
}
|
|
|
100 |
|
|
|
101 |
@WebMethod(operationName = "runCommandWithSSH")
|
|
|
102 |
public ReturnCode runCommandWithSSH(@WebParam(name = "host") String host,
|
|
|
103 |
@WebParam(name = "port") int port,
|
|
|
104 |
@WebParam(name = "username") String username,
|
|
|
105 |
@WebParam(name = "password") String password,
|
|
|
106 |
@WebParam(name = "command") String cmd,
|
|
|
107 |
@WebParam(name = "timeout") long timeout) {
|
|
|
108 |
|
|
|
109 |
return sshExec(host, username, password, port, cmd, timeout);
|
|
|
110 |
}
|
|
|
111 |
|
|
|
112 |
@WebMethod(operationName = "runCommandWithSSHKeyAuth")
|
|
|
113 |
public ReturnCode runCommandWithSSHKeyAuth(@WebParam(name = "host") String host,
|
|
|
114 |
@WebParam(name = "port") int port,
|
|
|
115 |
@WebParam(name = "username") String username,
|
|
|
116 |
@WebParam(name = "passphrase") String passphrase,
|
|
|
117 |
@WebParam(name = "keyfile") String keyfile,
|
|
|
118 |
@WebParam(name = "command") String cmd,
|
|
|
119 |
@WebParam(name = "timeout") long timeout) {
|
|
|
120 |
|
|
|
121 |
return sshExecWithCert(host, username, passphrase, keyfile, port, cmd, timeout);
|
|
|
122 |
}
|
|
|
123 |
|
|
|
124 |
@WebMethod(operationName = "rExec")
|
19 |
brianR |
125 |
public ReturnCode rExec(@WebParam(name = "host") HostConnection host,
|
6 |
brianR |
126 |
@WebParam(name = "command") String cmd,
|
|
|
127 |
@WebParam(name = "timeout") long timeout) {
|
19 |
brianR |
128 |
return rexec(host.hostname, host.port, host.user, host.password, cmd, timeout);
|
6 |
brianR |
129 |
}
|
|
|
130 |
|
|
|
131 |
@WebMethod(operationName = "telnet")
|
19 |
brianR |
132 |
public ReturnCode runTelnet(@WebParam(name = "host") HostConnection host,
|
6 |
brianR |
133 |
@WebParam(name = "prompt") String prompt,
|
|
|
134 |
@WebParam(name = "command") String cmd,
|
|
|
135 |
@WebParam(name = "expect") String expect,
|
|
|
136 |
@WebParam(name = "timeout") long timeout) {
|
19 |
brianR |
137 |
return telnet(host.hostname, host.port, host.user, host.password, cmd, timeout, prompt, expect);
|
6 |
brianR |
138 |
}
|
|
|
139 |
|
|
|
140 |
@WebMethod(exclude = true)
|
|
|
141 |
private ReturnCode executeCommand(String executable,
|
|
|
142 |
String[] args,
|
|
|
143 |
File dir,
|
|
|
144 |
boolean spawn,
|
|
|
145 |
String inputstring,
|
|
|
146 |
boolean newenvironment,
|
|
|
147 |
boolean vmlauncher,
|
|
|
148 |
boolean searchpath,
|
|
|
149 |
long timeout) {
|
|
|
150 |
ExecTask exe = new ExecTask();
|
|
|
151 |
RunTask runner = new RunTask(exe);
|
12 |
brianR |
152 |
|
|
|
153 |
/*
|
6 |
brianR |
154 |
Commandline cmdl = new Commandline();
|
|
|
155 |
cmdl.setExecutable(executable);
|
|
|
156 |
cmdl.addArguments(args);
|
|
|
157 |
System.out.println(cmdl.describeCommand());
|
12 |
brianR |
158 |
*/
|
|
|
159 |
|
|
|
160 |
exe.setExecutable(executable);
|
|
|
161 |
for (String s : args) {
|
|
|
162 |
exe.createArg().setValue(s);
|
|
|
163 |
}
|
|
|
164 |
|
6 |
brianR |
165 |
exe.setDir(dir);
|
|
|
166 |
if (spawn) {
|
|
|
167 |
exe.setSpawn(spawn);
|
|
|
168 |
} else {
|
|
|
169 |
exe.setTimeout(timeout);
|
|
|
170 |
exe.setInputString(inputstring);
|
|
|
171 |
exe.setOutputproperty("ExecuteService.stdout");
|
|
|
172 |
exe.setErrorProperty("ExecuteService.stderr");
|
|
|
173 |
exe.setResultProperty("ExecuteService.result");
|
|
|
174 |
}
|
|
|
175 |
|
|
|
176 |
exe.setNewenvironment(newenvironment);
|
|
|
177 |
exe.setVMLauncher(vmlauncher);
|
|
|
178 |
exe.setSearchPath(searchpath);
|
|
|
179 |
|
12 |
brianR |
180 |
return runner.postTask();
|
6 |
brianR |
181 |
}
|
|
|
182 |
|
|
|
183 |
@WebMethod(exclude = true)
|
|
|
184 |
private ReturnCode sshExec(String host,
|
|
|
185 |
String username,
|
|
|
186 |
String password,
|
|
|
187 |
int port,
|
|
|
188 |
String command,
|
|
|
189 |
long timeout) {
|
|
|
190 |
SSHExec sshexec = new SSHExec();
|
|
|
191 |
RunTask runner = new RunTask(sshexec);
|
|
|
192 |
sshexec.setHost(host);
|
|
|
193 |
sshexec.setUsername(username);
|
|
|
194 |
sshexec.setPassword(password);
|
|
|
195 |
sshexec.setPort(port);
|
|
|
196 |
sshexec.setCommand(command);
|
|
|
197 |
sshexec.setTrust(true);
|
|
|
198 |
sshexec.setTimeout(timeout);
|
|
|
199 |
sshexec.setOutputproperty("SSHExec.stdout");
|
12 |
brianR |
200 |
return runner.postTask();
|
6 |
brianR |
201 |
}
|
|
|
202 |
|
|
|
203 |
@WebMethod(exclude = true)
|
|
|
204 |
private ReturnCode sshExecWithCert(String host,
|
|
|
205 |
String username,
|
|
|
206 |
String passphrase,
|
|
|
207 |
String keyfile,
|
|
|
208 |
int port,
|
|
|
209 |
String command,
|
|
|
210 |
long timeout) {
|
|
|
211 |
SSHExec sshexec = new SSHExec();
|
|
|
212 |
RunTask runner = new RunTask(sshexec);
|
|
|
213 |
sshexec.setHost(host);
|
|
|
214 |
sshexec.setUsername(username);
|
|
|
215 |
sshexec.setKeyfile(keyfile);
|
|
|
216 |
sshexec.setPassphrase(passphrase);
|
|
|
217 |
sshexec.setPort(port);
|
|
|
218 |
sshexec.setCommand(command);
|
|
|
219 |
sshexec.setTrust(true);
|
|
|
220 |
sshexec.setTimeout(timeout);
|
|
|
221 |
sshexec.setOutputproperty("SSHExec.stdout");
|
12 |
brianR |
222 |
return runner.postTask();
|
6 |
brianR |
223 |
}
|
|
|
224 |
|
|
|
225 |
@WebMethod(exclude = true)
|
|
|
226 |
private ReturnCode rexec(String host,
|
|
|
227 |
int port,
|
|
|
228 |
String username,
|
|
|
229 |
String password,
|
|
|
230 |
String command,
|
|
|
231 |
long timeout) {
|
|
|
232 |
RExecTask rexec = new RExecTask();
|
|
|
233 |
RunTask runner = new RunTask(rexec);
|
|
|
234 |
rexec.setServer(host);
|
|
|
235 |
rexec.setPort(port);
|
|
|
236 |
rexec.setUserid(username);
|
|
|
237 |
rexec.setPassword(password);
|
|
|
238 |
rexec.setCommand(command);
|
|
|
239 |
rexec.setTimeout((int) Math.round(timeout));
|
|
|
240 |
|
12 |
brianR |
241 |
return runner.postTask();
|
6 |
brianR |
242 |
}
|
|
|
243 |
|
|
|
244 |
@WebMethod(exclude = true)
|
|
|
245 |
private ReturnCode telnet(String host,
|
|
|
246 |
int port,
|
|
|
247 |
String username,
|
|
|
248 |
String password,
|
|
|
249 |
String command,
|
|
|
250 |
long timeout, String prompt, String expect) {
|
|
|
251 |
TelnetTask rexec = new TelnetTask();
|
|
|
252 |
RunTask runner = new RunTask(rexec);
|
|
|
253 |
rexec.setServer(host);
|
|
|
254 |
rexec.setPort(port);
|
|
|
255 |
rexec.setUserid(username);
|
|
|
256 |
rexec.setPassword(password);
|
|
|
257 |
rexec.setTimeout((int) Math.round(timeout));
|
|
|
258 |
|
|
|
259 |
rexec.createRead().addText(prompt);
|
|
|
260 |
rexec.createWrite().addText(command);
|
|
|
261 |
rexec.createRead().addText(expect);
|
|
|
262 |
|
12 |
brianR |
263 |
return runner.postTask();
|
6 |
brianR |
264 |
}
|
|
|
265 |
}
|