]>
git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/jsch/RemoteExec.java
1 package org
.argeo
.slc
.jsch
;
3 import java
.io
.BufferedReader
;
4 import java
.io
.InputStream
;
5 import java
.io
.InputStreamReader
;
6 import java
.util
.ArrayList
;
9 import org
.apache
.commons
.io
.IOUtils
;
10 import org
.apache
.commons
.logging
.Log
;
11 import org
.apache
.commons
.logging
.LogFactory
;
12 import org
.argeo
.slc
.SlcException
;
14 import com
.jcraft
.jsch
.Channel
;
15 import com
.jcraft
.jsch
.ChannelExec
;
16 import com
.jcraft
.jsch
.Session
;
18 public class RemoteExec
extends AbstractJschTask
{
19 private final static Log log
= LogFactory
.getLog(RemoteExec
.class);
21 private List
<String
> commands
= new ArrayList
<String
>();
22 private String command
;
24 public void run(Session session
) {
25 if (command
!= null) {
26 if (commands
.size() != 0)
27 throw new SlcException(
28 "Specify either a single command or a list of commands.");
29 remoteExec(session
, command
);
31 if (commands
.size() == 0)
32 throw new SlcException(
33 "Neither a single command or a list of commands has been specified.");
35 for (String cmd
: commands
) {
36 remoteExec(session
, cmd
);
41 protected void remoteExec(Session session
, String command
) {
42 BufferedReader execIn
= null;
44 Channel channel
= session
.openChannel("exec");
45 ((ChannelExec
) channel
).setCommand(command
);
48 // channel.setXForwarding(true);
50 // channel.setInputStream(System.in);
51 channel
.setInputStream(null);
53 // channel.setOutputStream(System.out);
55 // FileOutputStream fos=new FileOutputStream("/tmp/stderr");
56 // ((ChannelExec)channel).setErrStream(fos);
57 ((ChannelExec
) channel
).setErrStream(System
.err
);
59 InputStream in
= channel
.getInputStream();
61 if (log
.isDebugEnabled())
63 .debug("Exec '" + command
+ "' on " + getSshTarget()
68 // byte[] tmp = new byte[1024];
70 execIn
= new BufferedReader(new InputStreamReader(in
));
72 while ((line
= execIn
.readLine()) != null) {
75 // while (in.available() > 0) {
76 // int i = in.read(tmp, 0, 1024);
79 // log.info(new String(tmp, 0, i));
81 if (channel
.isClosed()) {
82 log
.info("Remote execution exit status: "
83 + channel
.getExitStatus());
88 } catch (Exception ee
) {
92 } catch (Exception e
) {
93 throw new SlcException("Cannot execute remotely '" + command
94 + "' on " + getSshTarget(), e
);
96 IOUtils
.closeQuietly(execIn
);
100 public void setCommand(String command
) {
101 this.command
= command
;
104 public void setCommands(List
<String
> commands
) {
105 this.commands
= commands
;