]>
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
.apache
.commons
.logging
.Log
;
11 import org
.apache
.commons
.logging
.LogFactory
;
12 import org
.argeo
.slc
.SlcException
;
13 import org
.springframework
.core
.io
.Resource
;
14 import org
.springframework
.util
.StringUtils
;
16 import com
.jcraft
.jsch
.Channel
;
17 import com
.jcraft
.jsch
.Session
;
19 public class SshShell
extends AbstractJschTask
{
20 private final static Log log
= LogFactory
.getLog(SshShell
.class);
21 private Resource input
;
24 void run(Session session
) {
26 final Channel channel
= session
.openChannel("shell");
28 // Enable agent-forwarding.
29 // ((ChannelShell)channel).setAgentForwarding(true);
31 // channel.setInputStream(System.in);
32 // channel.setInputStream(input.getInputStream());
34 * // a hack for MS-DOS prompt on Windows.
35 * channel.setInputStream(new FilterInputStream(System.in){ public
36 * int read(byte[] b, int off, int len)throws IOException{ return
37 * in.read(b, off, (len>1024?1024:len)); } });
40 // channel.setOutputStream(System.out);
43 * // Choose the pty-type "vt102".
44 * ((ChannelShell)channel).setPtyType("vt102");
48 * // Set environment variable "LANG" as "ja_JP.eucJP".
49 * ((ChannelShell)channel).setEnv("LANG", "ja_JP.eucJP");
53 final BufferedWriter writer
= new BufferedWriter(
54 new OutputStreamWriter(channel
.getOutputStream()));
57 channel
.connect(3 * 1000);
59 // while (!channel.isConnected())
62 // } catch (InterruptedException e1) {
66 Thread writerThread
= new Thread("Shell writer " + getSshTarget()) {
71 if (log
.isDebugEnabled())
72 log
.debug("Start writing to shell");
74 BufferedReader reader
= null;
76 reader
= new BufferedReader(new InputStreamReader(input
79 while ((line
= reader
.readLine()) != null) {
80 if (!StringUtils
.hasText(line
))
85 writer
.append("exit");
88 // channel.disconnect();
89 } catch (IOException e
) {
90 throw new SlcException("Cannot write to shell on "
93 IOUtils
.closeQuietly(reader
);
99 BufferedReader execIn
= null;
101 execIn
= new BufferedReader(new InputStreamReader(channel
104 while ((line
= execIn
.readLine()) != null) {
105 if (!line
.trim().equals(""))
108 } catch (Exception e
) {
109 throw new SlcException("Cannot read from shell on "
110 + getSshTarget(), e
);
112 IOUtils
.closeQuietly(execIn
);
115 } catch (Exception e
) {
116 throw new SlcException("Cannot use SSH shell on " + getSshTarget(),
121 public void setInput(Resource input
) {