简体   繁体   English

如何使用java实现expect“interact”命令

[英]how to implement expect “interact” command using java

I want to implement the expect "interact" command using java. 我想使用java实现expect“interact”命令。 In expect, it's possible to open an ssh session, authenticate and, then, use the "interact" command to give the control back to the user. 在期望中,可以打开ssh会话,进行身份验证,然后使用“interact”命令将控件返回给用户。 Is that possible with java? 这可能与Java? I've tried with expectJ, expect4J and expectForJava but there's little documentation and almost no examples of how to do this. 我尝试过expectJ,expect4J和expectForJava但是文档很少,几乎没有如何做到这一点的例子。 TIA. TIA。

Update: for "interact" command reference, please check this out: http://wiki.tcl.tk/3914 更新:对于“交互”命令参考,请查看: http//wiki.tcl.tk/3914

"Interact is an Expect command which gives control of the current process to the user, so that keystrokes are sent to the current process, and the stdout and stderr of the current process are returned." “Interact是一个Expect命令,它将当前进程的控制权交给用户,以便将键击发送到当前进程,并返回当前进程的stdout和stderr。”

These libraries might suit your needs better: 这些库可能更适合您的需求:

SSHJ SSHJ

https://github.com/shikhar/sshj https://github.com/shikhar/sshj

JSCH JSCH

http://www.jcraft.com/jsch/ http://www.jcraft.com/jsch/

In case anyone is interested, I have added basic interactive loop support to ExpectIt , my own open source Expect for Java implementation (sorry for self-promotion), since version 0.8. 如果有人感兴趣,我已经为ExpectIt添加了基本的交互式循环支持, 是我自己的开源Expect for Java实现(对不起自我推销),自0.8版以来。

Here is an example of interacting with the system input stream in Java 8: 以下是在Java 8中与系统输入流交互的示例

        try (final Expect expect = new ExpectBuilder()
                .withInputs(System.in)
                .build()) {
            expect.interact()
                    .when(contains("abc")).then(r -> System.out.println("A"))
                    .when(contains("xyz")).then(r -> System.err.println("B"))
                    .until(contains("exit"));
            System.out.println("DONE!");
        }
        System.in.close();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM