]> git.argeo.org Git - gpl/argeo-slc.git/blob - legacy/org.argeo.slc.support/src/org/argeo/slc/jsch/SshShell.java
Prepare next development cacle
[gpl/argeo-slc.git] / legacy / org.argeo.slc.support / src / org / argeo / slc / jsch / SshShell.java
1 package org.argeo.slc.jsch;
2
3 import java.io.BufferedReader;
4 import java.io.BufferedWriter;
5 import java.io.IOException;
6 import java.io.InputStreamReader;
7 import java.io.OutputStreamWriter;
8
9 import org.apache.commons.io.IOUtils;
10 import org.argeo.api.cms.CmsLog;
11 import org.argeo.slc.SlcException;
12 import org.springframework.core.io.Resource;
13 import org.springframework.util.StringUtils;
14
15 import com.jcraft.jsch.Channel;
16 import com.jcraft.jsch.Session;
17
18 public class SshShell extends AbstractJschTask {
19 private final static CmsLog log = CmsLog.getLog(SshShell.class);
20 private Resource input;
21
22 @Override
23 void run(Session session) {
24 try {
25 final Channel channel = session.openChannel("shell");
26
27 // Enable agent-forwarding.
28 // ((ChannelShell)channel).setAgentForwarding(true);
29
30 // channel.setInputStream(System.in);
31 // channel.setInputStream(input.getInputStream());
32 /*
33 * // a hack for MS-DOS prompt on Windows.
34 * channel.setInputStream(new FilterInputStream(System.in){ public
35 * int read(byte[] b, int off, int len)throws IOException{ return
36 * in.read(b, off, (len>1024?1024:len)); } });
37 */
38
39 // channel.setOutputStream(System.out);
40
41 /*
42 * // Choose the pty-type "vt102".
43 * ((ChannelShell)channel).setPtyType("vt102");
44 */
45
46 /*
47 * // Set environment variable "LANG" as "ja_JP.eucJP".
48 * ((ChannelShell)channel).setEnv("LANG", "ja_JP.eucJP");
49 */
50
51 // Writer thread
52 final BufferedWriter writer = new BufferedWriter(
53 new OutputStreamWriter(channel.getOutputStream()));
54
55 // channel.connect();
56 channel.connect(3 * 1000);
57
58 // while (!channel.isConnected())
59 // try {
60 // Thread.sleep(500);
61 // } catch (InterruptedException e1) {
62 // // silent
63 // }
64
65 Thread writerThread = new Thread("Shell writer " + getSshTarget()) {
66
67 @Override
68 public void run() {
69
70 if (log.isDebugEnabled())
71 log.debug("Start writing to shell");
72
73 BufferedReader reader = null;
74 try {
75 reader = new BufferedReader(new InputStreamReader(input
76 .getInputStream()));
77 String line = null;
78 while ((line = reader.readLine()) != null) {
79 if (!StringUtils.hasText(line))
80 continue;
81 writer.write(line);
82 writer.newLine();
83 }
84 writer.append("exit");
85 writer.newLine();
86 writer.flush();
87 // channel.disconnect();
88 } catch (IOException e) {
89 throw new SlcException("Cannot write to shell on "
90 + getSshTarget(), e);
91 } finally {
92 IOUtils.closeQuietly(reader);
93 }
94 }
95 };
96 writerThread.start();
97
98 BufferedReader execIn = null;
99 try {
100 execIn = new BufferedReader(new InputStreamReader(channel
101 .getInputStream()));
102 String line = null;
103 while ((line = execIn.readLine()) != null) {
104 if (!line.trim().equals(""))
105 log.info(line);
106 }
107 } catch (Exception e) {
108 throw new SlcException("Cannot read from shell on "
109 + getSshTarget(), e);
110 } finally {
111 IOUtils.closeQuietly(execIn);
112 }
113
114 } catch (Exception e) {
115 throw new SlcException("Cannot use SSH shell on " + getSshTarget(),
116 e);
117 }
118 }
119
120 public void setInput(Resource input) {
121 this.input = input;
122 }
123
124 }