1

参考:ExpectIt:执行 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 获取的所有结果都将回显到控制台。我可以使用“结果”字符串,但它也可以发送到 tomcat 控制台(我确定我不会将它发送到)。

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

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

在此处输入图像描述

4

1 回答 1

1

stty -echo您可以通过在会话开始时发送命令来禁用命令回显。看看这个例子。

我建议将问题发布到 ExpectIt用户组,而不是 Stackoverflow,因为目前只有少数人在使用这个库。

于 2015-07-21T13:39:18.580 回答