X-Git-Url: http://git.argeo.org/?a=blobdiff_plain;f=runtime%2Forg.argeo.slc.support.simple%2Fsrc%2Fmain%2Fjava%2Forg%2Fargeo%2Fslc%2Fjsch%2FSshShell.java;fp=runtime%2Forg.argeo.slc.support.simple%2Fsrc%2Fmain%2Fjava%2Forg%2Fargeo%2Fslc%2Fjsch%2FSshShell.java;h=2ec2b72a19602b8c18e7840738a95974a8eb2a8a;hb=7cb867319d0f8b1b0af3e7eb3f9833b091281320;hp=0000000000000000000000000000000000000000;hpb=c9672b7c16ee662ff452fff8405a67cac091a15e;p=gpl%2Fargeo-slc.git diff --git a/runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/jsch/SshShell.java b/runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/jsch/SshShell.java new file mode 100644 index 000000000..2ec2b72a1 --- /dev/null +++ b/runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/jsch/SshShell.java @@ -0,0 +1,125 @@ +package org.argeo.slc.jsch; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.OutputStreamWriter; + +import org.apache.commons.io.IOUtils; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.argeo.slc.SlcException; +import org.springframework.core.io.Resource; +import org.springframework.util.StringUtils; + +import com.jcraft.jsch.Channel; +import com.jcraft.jsch.Session; + +public class SshShell extends AbstractJschTask { + private final static Log log = LogFactory.getLog(SshShell.class); + private Resource input; + + @Override + void run(Session session) { + try { + final Channel channel = session.openChannel("shell"); + + // Enable agent-forwarding. + // ((ChannelShell)channel).setAgentForwarding(true); + + // channel.setInputStream(System.in); + // channel.setInputStream(input.getInputStream()); + /* + * // a hack for MS-DOS prompt on Windows. + * channel.setInputStream(new FilterInputStream(System.in){ public + * int read(byte[] b, int off, int len)throws IOException{ return + * in.read(b, off, (len>1024?1024:len)); } }); + */ + + // channel.setOutputStream(System.out); + + /* + * // Choose the pty-type "vt102". + * ((ChannelShell)channel).setPtyType("vt102"); + */ + + /* + * // Set environment variable "LANG" as "ja_JP.eucJP". + * ((ChannelShell)channel).setEnv("LANG", "ja_JP.eucJP"); + */ + + // Writer thread + final BufferedWriter writer = new BufferedWriter( + new OutputStreamWriter(channel.getOutputStream())); + + // channel.connect(); + channel.connect(3 * 1000); + + // while (!channel.isConnected()) + // try { + // Thread.sleep(500); + // } catch (InterruptedException e1) { + // // silent + // } + + Thread writerThread = new Thread("Shell writer " + getSshTarget()) { + + @Override + public void run() { + + if (log.isDebugEnabled()) + log.debug("Start writing to shell"); + + BufferedReader reader = null; + try { + reader = new BufferedReader(new InputStreamReader(input + .getInputStream())); + String line = null; + while ((line = reader.readLine()) != null) { + if (!StringUtils.hasText(line)) + continue; + writer.write(line); + writer.newLine(); + } + writer.append("exit"); + writer.newLine(); + writer.flush(); + // channel.disconnect(); + } catch (IOException e) { + throw new SlcException("Cannot write to shell on " + + getSshTarget(), e); + } finally { + IOUtils.closeQuietly(reader); + } + } + }; + writerThread.start(); + + BufferedReader execIn = null; + try { + execIn = new BufferedReader(new InputStreamReader(channel + .getInputStream())); + String line = null; + while ((line = execIn.readLine()) != null) { + if (!line.trim().equals("")) + log.info(line); + } + } catch (Exception e) { + throw new SlcException("Cannot read from shell on " + + getSshTarget(), e); + } finally { + IOUtils.closeQuietly(execIn); + } + + } catch (Exception e) { + throw new SlcException("Cannot use SSH shell on " + getSshTarget(), + e); + } + } + + public void setInput(Resource input) { + this.input = input; + } + +}