如何避免将 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 环境)中运行。