Hello all,
I am writing a prototype application where I provide a web front end to a
ssh session (so I am not using System.in and System.out when interacting
with the ssh client that I have seen in other examples). Things are
working great using simple commands such as ls and cd etc. However, if I
want to allow users to invoke interactive commands such as ssh and then be
prompted for a password, the application is blocked in the reading of the
channel's inputstream. I have found some example code that use UserInfo
to write a custom implementation that overrides the showMessage(String
message) and thought that this might help me indicate that I need to write
something back to the client. However, this method is never called. Any
insight would be much appreciated.
Attached is some example code I am using to prototype with.
package com.ibm.todd.jsch;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.UserInfo;
public class SSHClient {
private String host;
private String password;
private InputStream inputStream;
private OutputStream outputStream;
private Channel channel;
private static final String ECHO_VALUE = "BLAH";
private static final String ECHO_COMMAND = "echo \"" + ECHO_VALUE
+ "\"";
private static final String ECHO_APPEND = ";" + ECHO_COMMAND;
private BufferedReader fromServer;
private OutputStream toServer;
public void setHost(String host) {
this.host = host;
}
public void setPassword(String password) {
this.password = password;
}
public void connect() throws JSchException, IOException{
JSch jsch=new JSch();
String user = host.substring(0, host.indexOf('@'));
String hostname = host.substring(host.indexOf('@')+1);
Session session = jsch.getSession(user, hostname, 22);
session.setPassword(password);
// FOR DEBUG ONLY!!!!!!! SHOULD COMMENT THIS OUT LATER
session.setConfig("StrictHostKeyChecking", "no");
session.setUserInfo(new MyUserInfo());
//session.connect();
session.connect(30000); // making a connection with timeout.
channel= session.openChannel("shell");
fromServer = new java.io.BufferedReader(new
java.io.InputStreamReader((channel.getInputStream())));
toServer = channel.getOutputStream();
channel.connect();
if(channel.isConnected()){
toServer.write(ECHO_COMMAND.getBytes());
toServer.write("\n".getBytes()) ;
toServer.flush();
}
}
public void send(String command) throws IOException {
command += ECHO_APPEND+ "\n";
toServer.write(command.getBytes());
toServer.flush();
}
public void getServerResponse(PrintWriter out) throws
IOException, InterruptedException {
int sleepCount = 0;
String line = "";
for (int i = 0; true; i++) {
System.out.println("getServerResponse(): isReady?" +
fromServer.ready() + " sleepCount " + sleepCount);
if(!fromServer.ready() ){
if( sleepCount < 10){
Thread.sleep(3000);
sleepCount ++;
} else{
break;
}
}else{
sleepCount = 0;
line = fromServer.readLine();
System.out.println("getServerResponse(): line -> " +
line);
if (line.equals(ECHO_VALUE)) {
break;
}else if (line.indexOf (ECHO_APPEND) > -1){
out.print(line.substring(0,line.indexOf(
ECHO_APPEND)));
out.print("\n");
out.flush();
}else if (line.indexOf (ECHO_COMMAND) > -1){
out.print(line.substring(0,line.indexOf(
ECHO_COMMAND)));
out.print("\n");
out.flush();
}else{
out.print(line);
out.print("\n");
out.flush();
}
}
}
}
public class MyUserInfo implements UserInfo{
// default behavior (stubbed methods for other required
methods)
@Override
public void showMessage(String arg0) {
System.out.println("showMessage -> " + arg0);
}
}
}
Thank you
Todd Kaplinger
Project Zero Architecture and Development
http://www.projectzero.org------------------------------------------------------------------------------
_______________________________________________
JSch-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/jsch-users