Subversion Repositories XServices

Rev

Rev 12 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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