ExpectIt:抑制控制台回显

ExpectIt: Suppress console echo

参考:ExpectIt : Trouble implementing a sudo -i

如何避免将 Expectit 的所有输出回显到我的 java 控制台?

这是我启动 SSH 的方式:

public void connect( String host, String user, String password ) throws Exception {
    this.host = host;
    this.user = user;
    this.password = password;
    ssh = new SSHClient();
    ssh.addHostKeyVerifier( new PromiscuousVerifier() );
    ssh.connect(host, port);
    ssh.authPassword(user, password);
    session = ssh.startSession();

    session.allocateDefaultPTY();
    shell = session.startShell();

    expect = new ExpectBuilder()
            .withOutput(shell.getOutputStream())
            .withInputs(shell.getInputStream(), shell.getErrorStream())
            .withInputFilters(removeColors(), removeNonPrintable())
            .withExceptionOnFailure()
            .withTimeout(30, TimeUnit.SECONDS)
            .build();

    String result = expect.expect( Matchers.contains( PROMPT ) ).getInput();
    consoleOut.add( result );

}

现在,从 Expect 获取的所有结果都将回显到控制台。我可以使用 "result" 字符串,但它也会转到 tomcat 控制台(我确定我不会将它发送到)。

expect.sendLine( command );
String result = expect.expect( Matchers.contains( PROMPT ) ).getInput();

我 运行 在 Tomcat 容器(网络环境)中。

您可以通过在会话开始时发送 stty -echo 命令来禁用命令回显。看一下 this 示例。

我建议 post 向 ExpectIt user group 提问,而不是在 Whosebug 上提问,因为目前只有少数人在使用这个库。