]>
git.argeo.org Git - gpl/argeo-slc.git/blob - legacy/org.argeo.slc.support/src/org/argeo/slc/jsch/SshShell.java
1 package org
.argeo
.slc
.jsch
;
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
;
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
;
15 import com
.jcraft
.jsch
.Channel
;
16 import com
.jcraft
.jsch
.Session
;
18 public class SshShell
extends AbstractJschTask
{
19 private final static CmsLog log
= CmsLog
.getLog(SshShell
.class);
20 private Resource input
;
23 void run(Session session
) {
25 final Channel channel
= session
.openChannel("shell");
27 // Enable agent-forwarding.
28 // ((ChannelShell)channel).setAgentForwarding(true);
30 // channel.setInputStream(System.in);
31 // channel.setInputStream(input.getInputStream());
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)); } });
39 // channel.setOutputStream(System.out);
42 * // Choose the pty-type "vt102".
43 * ((ChannelShell)channel).setPtyType("vt102");
47 * // Set environment variable "LANG" as "ja_JP.eucJP".
48 * ((ChannelShell)channel).setEnv("LANG", "ja_JP.eucJP");
52 final BufferedWriter writer
= new BufferedWriter(
53 new OutputStreamWriter(channel
.getOutputStream()));
56 channel
.connect(3 * 1000);
58 // while (!channel.isConnected())
61 // } catch (InterruptedException e1) {
65 Thread writerThread
= new Thread("Shell writer " + getSshTarget()) {
70 if (log
.isDebugEnabled())
71 log
.debug("Start writing to shell");
73 BufferedReader reader
= null;
75 reader
= new BufferedReader(new InputStreamReader(input
78 while ((line
= reader
.readLine()) != null) {
79 if (!StringUtils
.hasText(line
))
84 writer
.append("exit");
87 // channel.disconnect();
88 } catch (IOException e
) {
89 throw new SlcException("Cannot write to shell on "
92 IOUtils
.closeQuietly(reader
);
98 BufferedReader execIn
= null;
100 execIn
= new BufferedReader(new InputStreamReader(channel
103 while ((line
= execIn
.readLine()) != null) {
104 if (!line
.trim().equals(""))
107 } catch (Exception e
) {
108 throw new SlcException("Cannot read from shell on "
109 + getSshTarget(), e
);
111 IOUtils
.closeQuietly(execIn
);
114 } catch (Exception e
) {
115 throw new SlcException("Cannot use SSH shell on " + getSshTarget(),
120 public void setInput(Resource input
) {