首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用ProcessShellFactory时,Apache MINA SSHD会话回显字符两次,控制字符不起作用

使用ProcessShellFactory时,Apache MINA SSHD会话回显字符两次,控制字符不起作用
EN

Stack Overflow用户
提问于 2020-12-09 11:06:09
回答 1查看 423关注 0票数 2

我在macOS 10.15.7上运行OpenJDK 14。我正在做一些概念验证代码,使用Apache Mina SSHD建立SSH服务器,然后连接到它。这就是我所拥有的:

代码语言:javascript
运行
复制
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,它就可以工作了。身份验证成功后,我首先看到的是:

代码语言:javascript
运行
复制
sh: no job control in this shell

然后,在提示符处,我键入的字符(包括换行符)被回显两次,而不是一次,导致所有内容都是dduupplliiccaatteedd:

代码语言:javascript
运行
复制
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服务器。

我觉得我一定是做错了什么。有什么想法?

EN

回答 1

Stack Overflow用户

发布于 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电脑来试用它。

保罗

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65210054

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档