我在macOS 10.15.7上运行OpenJDK 14。我正在做一些概念验证代码,使用Apache Mina SSHD建立SSH服务器,然后连接到它。这就是我所拥有的:
import java.io.IOException;
import java.nio.file.Paths;
import java.util.Arrays;
import org.apache.sshd.common.cipher.BuiltinCiphers;
import org.apache.sshd.common.util.logging.AbstractLoggingBean;
import org.apache.sshd.server.ServerBuilder;
import org.apache.sshd.server.SshServer;
import org.apache.sshd.server.auth.AsyncAuthException;
import org.apache.sshd.server.auth.password.PasswordAuthenticator;
import org.apache.sshd.server.auth.password.PasswordChangeRequiredException;
import org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider;
import org.apache.sshd.server.session.ServerSession;
import org.apache.sshd.server.shell.InteractiveProcessShellFactory;
import org.apache.sshd.server.shell.ProcessShellFactory;
public class FunctionalTest
{
private static class TestAuthenticator
extends AbstractLoggingBean
implements PasswordAuthenticator
{
@Override
public boolean authenticate(String username, String password, ServerSession session)
throws PasswordChangeRequiredException, AsyncAuthException
{
if ("test".equals(username) && "foobar".equals(password))
{
this.log.info("authenticate({}[{}]: accepted", username, session);
return true;
}
this.log.warn("authenticate({}[{}]: rejected", username, session);
return false;
}
}
public static void main(String... args) throws IOException
{
SshServer sshd = SshServer.setUpDefaultServer();
sshd.setHost("0.0.0.0");
sshd.setPort(1022);
sshd.setShellFactory(InteractiveProcessShellFactory.INSTANCE);
sshd.setPasswordAuthenticator(new TestAuthenticator());
sshd.setCipherFactories(Arrays.asList(BuiltinCiphers.aes256ctr, BuiltinCiphers.aes192ctr));
sshd.setKeyExchangeFactories(ServerBuilder.setUpDefaultKeyExchanges(false));
sshd.setKeyPairProvider(new SimpleGeneratorHostKeyProvider(Paths.get("key.ser")));
sshd.start();
try
{
Thread.sleep(3_600_000);
}
catch(InterruptedException e)
{
System.out.println("Caught interrupt ... stopping server.");
sshd.stop(true);
}
}
}
当我启动这个程序时,我可以使用密码foobar
进行ssh -p 1022 test@localhost
,它就可以工作了。身份验证成功后,我首先看到的是:
sh: no job control in this shell
然后,在提示符处,我键入的字符(包括换行符)被回显两次,而不是一次,导致所有内容都是dduupplliiccaatteedd:
williamsn:mina-test williamsn$ llss --aall
total 24
... (list of files)
williamsn:mina-test williamsn$ eecchhoo hheelllloo
hello
williamsn:mina-test williamsn$
此外,如果我运行像top
这样的交互式命令,它无法识别我的输入,并且控制字符也不起作用。ttoopp
启动(尽管它的输出是丑陋的和附加的,而不是替换屏幕),但是如果我输入q
退出(在本例中,q
不会回显两次),top
不会退出以响应q
。它一直在继续。ctrl+c
也不起作用--top
一直在运行。退出顶层的唯一方法是杀死我的ssh
进程或关闭MINA服务器。
我觉得我一定是做错了什么。有什么想法?
发布于 2020-12-13 16:11:35
"no job control“消息表明派生的shell未处于完全交互模式,两个字母表示本地和远程字符echo不匹配。我只能假设mac-os (/usr/bin/sh )上的默认shell不是像Linux上那样的bash实现。尝试将外壳工厂更改为新的ProcessShellFactory("/usr/bin/bash","-i")
对不起,我没有一台mac电脑来试用它。
保罗
https://stackoverflow.com/questions/65210054
复制相似问题