how to implement expect “interact” command using java

百般思念 提交于 2019-12-12 08:44:56

问题


I want to implement the expect "interact" command using java. 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. Is that possible with java? I've tried with expectJ, expect4J and expectForJava but there's little documentation and almost no examples of how to do this. TIA.

Update: for "interact" command reference, please check this out: 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."


回答1:


These libraries might suit your needs better:

SSHJ

https://github.com/shikhar/sshj

JSCH

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




回答2:


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.

Here is an example of interacting with the system input stream in 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();


来源:https://stackoverflow.com/questions/15143619/how-to-implement-expect-interact-command-using-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!