]>
git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/jsch/SshShell.java
2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org
.argeo
.slc
.jsch
;
19 import java
.io
.BufferedReader
;
20 import java
.io
.BufferedWriter
;
21 import java
.io
.IOException
;
22 import java
.io
.InputStreamReader
;
23 import java
.io
.OutputStreamWriter
;
25 import org
.apache
.commons
.io
.IOUtils
;
26 import org
.apache
.commons
.logging
.Log
;
27 import org
.apache
.commons
.logging
.LogFactory
;
28 import org
.argeo
.slc
.SlcException
;
29 import org
.springframework
.core
.io
.Resource
;
30 import org
.springframework
.util
.StringUtils
;
32 import com
.jcraft
.jsch
.Channel
;
33 import com
.jcraft
.jsch
.Session
;
35 public class SshShell
extends AbstractJschTask
{
36 private final static Log log
= LogFactory
.getLog(SshShell
.class);
37 private Resource input
;
40 void run(Session session
) {
42 final Channel channel
= session
.openChannel("shell");
44 // Enable agent-forwarding.
45 // ((ChannelShell)channel).setAgentForwarding(true);
47 // channel.setInputStream(System.in);
48 // channel.setInputStream(input.getInputStream());
50 * // a hack for MS-DOS prompt on Windows.
51 * channel.setInputStream(new FilterInputStream(System.in){ public
52 * int read(byte[] b, int off, int len)throws IOException{ return
53 * in.read(b, off, (len>1024?1024:len)); } });
56 // channel.setOutputStream(System.out);
59 * // Choose the pty-type "vt102".
60 * ((ChannelShell)channel).setPtyType("vt102");
64 * // Set environment variable "LANG" as "ja_JP.eucJP".
65 * ((ChannelShell)channel).setEnv("LANG", "ja_JP.eucJP");
69 final BufferedWriter writer
= new BufferedWriter(
70 new OutputStreamWriter(channel
.getOutputStream()));
73 channel
.connect(3 * 1000);
75 // while (!channel.isConnected())
78 // } catch (InterruptedException e1) {
82 Thread writerThread
= new Thread("Shell writer " + getSshTarget()) {
87 if (log
.isDebugEnabled())
88 log
.debug("Start writing to shell");
90 BufferedReader reader
= null;
92 reader
= new BufferedReader(new InputStreamReader(input
95 while ((line
= reader
.readLine()) != null) {
96 if (!StringUtils
.hasText(line
))
101 writer
.append("exit");
104 // channel.disconnect();
105 } catch (IOException e
) {
106 throw new SlcException("Cannot write to shell on "
107 + getSshTarget(), e
);
109 IOUtils
.closeQuietly(reader
);
113 writerThread
.start();
115 BufferedReader execIn
= null;
117 execIn
= new BufferedReader(new InputStreamReader(channel
120 while ((line
= execIn
.readLine()) != null) {
121 if (!line
.trim().equals(""))
124 } catch (Exception e
) {
125 throw new SlcException("Cannot read from shell on "
126 + getSshTarget(), e
);
128 IOUtils
.closeQuietly(execIn
);
131 } catch (Exception e
) {
132 throw new SlcException("Cannot use SSH shell on " + getSshTarget(),
137 public void setInput(Resource input
) {