3

我在 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);
        }
    }
}

当我开始这个时,我可以ssh -p 1022 test@localhost使用密码foobar并且它可以工作。身份验证成功后,我首先看到的是:

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也不起作用 - <code>top 继续运行。退出 top 的唯一方法是终止我的ssh进程或关闭 MINA 服务器。

我觉得我必须在这里做一些非常错误的事情。想法?

4

1 回答 1

0

“无作业控制”消息表明生成的 shell 未处于完全交互模式,双字母表示本地和远程字符回显不匹配。我只能假设 mac-os (/usr/bin/sh) 上的默认 shell 不是像 Linux 上那样的 bash 实现。尝试将 shell 工厂更改为 new ProcessShellFactory("/usr/bin/bash","-i")

对不起,我没有 Mac 来试试这个。

保罗

于 2020-12-13T08:11:35.387 回答