]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/jsch/RemoteExec.java
Improve SSH support
[gpl/argeo-slc.git] / runtime / org.argeo.slc.support.simple / src / main / java / org / argeo / slc / jsch / RemoteExec.java
1 package org.argeo.slc.jsch;
2
3 import java.io.BufferedReader;
4 import java.io.InputStream;
5 import java.io.InputStreamReader;
6 import java.util.ArrayList;
7 import java.util.List;
8
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;
13
14 import com.jcraft.jsch.Channel;
15 import com.jcraft.jsch.ChannelExec;
16 import com.jcraft.jsch.Session;
17
18 public class RemoteExec extends AbstractJschTask {
19 private final static Log log = LogFactory.getLog(RemoteExec.class);
20
21 private List<String> commands = new ArrayList<String>();
22 private String command;
23
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);
30 } else {
31 if (commands.size() == 0)
32 throw new SlcException(
33 "Neither a single command or a list of commands has been specified.");
34
35 for (String cmd : commands) {
36 remoteExec(session, cmd);
37 }
38 }
39 }
40
41 protected void remoteExec(Session session, String command) {
42 BufferedReader execIn = null;
43 try {
44 Channel channel = session.openChannel("exec");
45 ((ChannelExec) channel).setCommand(command);
46
47 // X Forwarding
48 // channel.setXForwarding(true);
49
50 // channel.setInputStream(System.in);
51 channel.setInputStream(null);
52
53 // channel.setOutputStream(System.out);
54
55 // FileOutputStream fos=new FileOutputStream("/tmp/stderr");
56 // ((ChannelExec)channel).setErrStream(fos);
57 ((ChannelExec) channel).setErrStream(System.err);
58
59 InputStream in = channel.getInputStream();
60
61 if (log.isDebugEnabled())
62 log
63 .debug("Exec '" + command + "' on " + getSshTarget()
64 + "...");
65
66 channel.connect();
67
68 // byte[] tmp = new byte[1024];
69 while (true) {
70 execIn = new BufferedReader(new InputStreamReader(in));
71 String line = null;
72 while ((line = execIn.readLine()) != null) {
73 log.info(line);
74 }
75 // while (in.available() > 0) {
76 // int i = in.read(tmp, 0, 1024);
77 // if (i < 0)
78 // break;
79 // log.info(new String(tmp, 0, i));
80 // }
81 if (channel.isClosed()) {
82 log.info("Remote execution exit status: "
83 + channel.getExitStatus());
84 break;
85 }
86 try {
87 Thread.sleep(1000);
88 } catch (Exception ee) {
89 }
90 }
91 channel.disconnect();
92 } catch (Exception e) {
93 throw new SlcException("Cannot execute remotely '" + command
94 + "' on " + getSshTarget(), e);
95 } finally {
96 IOUtils.closeQuietly(execIn);
97 }
98 }
99
100 public void setCommand(String command) {
101 this.command = command;
102 }
103
104 public void setCommands(List<String> commands) {
105 this.commands = commands;
106 }
107
108 }