Hi,
I was trying to program a simple echo ssh server to learn how to setup
mina. While I am able to login and send at least a prompt to the client,
I am not able to receive any characters from the client via the input
stream. Would be great if someone could take a look at the demo below.
thanks
Christian
----------- code --------
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.sshd.common.Factory;
import org.apache.sshd.common.channel.ChannelPipedInputStream;
import org.apache.sshd.server.Command;
import org.apache.sshd.server.Environment;
import org.apache.sshd.server.ExitCallback;
import org.apache.sshd.server.SshServer;
import
org.apache.sshd.server.auth.keyboard.DefaultKeyboardInteractiveAuthenticator;
import org.apache.sshd.server.auth.password.StaticPasswordAuthenticator;
import org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider;
public class EchoTest {
private class TestCommand implements Command {
private OutputStream fOutput;
private InputStream fInput;
@Override
public void destroy() throws Exception {
}
@Override
public void start(final Environment arg0) throws IOException {
fOutput.write("SSH > ".getBytes());
fOutput.flush();
if (fInput instanceof ChannelPipedInputStream) {
((ChannelPipedInputStream) fInput).setTimeout(0L);
}
int dataByte = 0;
do {
dataByte = fInput.read();
// local echo
fOutput.write(dataByte);
fOutput.flush();
} while (dataByte != -1);
System.out.println("stop listening");
}
@Override
public void setErrorStream(final OutputStream arg0) {
}
@Override
public void setExitCallback(final ExitCallback arg0) {
}
@Override
public void setInputStream(final InputStream arg0) {
fInput = arg0;
}
@Override
public void setOutputStream(final OutputStream arg0) {
fOutput = arg0;
}
};
public SshServer ssh() {
SshServer sshd = SshServer.setUpDefaultServer();
sshd.setPort(1234);
sshd.setKeyPairProvider(new SimpleGeneratorHostKeyProvider());
sshd.setKeyboardInteractiveAuthenticator(new
DefaultKeyboardInteractiveAuthenticator());
sshd.setPasswordAuthenticator(new
StaticPasswordAuthenticator(true));
sshd.setShellFactory(new Factory<Command>() {
@Override
public Command create() {
return new TestCommand();
}
});
return sshd;
}
public static void main(final String[] args) {
try {
new EchoTest().ssh().start();
while (true)
;
} catch (IOException e) {
// TODO handle this exception (but for now, at least know
it happened)
throw new RuntimeException(e);
}
}
}