9

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."

  • https://code.google.com/p/expect4j/wiki/Installation perhaps here? – Sergey Benner Feb 28 '13 at 19:09
  • @SergeyBenner I could not find in expect4J, from the Java code, someone can call the "interact" command. The Expect object does only have "expect" and "send" methods. expectJ does have one explicitly, but it does not work (or I don't know how to use it properly). –  Feb 28 '13 at 19:45
  • @DaveJarvis I would like to take a look. Did you wrote it from the scratch or did you use any existent lib? Does it have the "interact" functionality? –  Feb 28 '13 at 19:46
  • @LeonardoKenji: Try this https://bitbucket.org/djarvis/jexpect – Dave Jarvis Mar 01 '13 at 01:39
  • 3
    Expect's `interact` is pretty high up the secret-sauce scale. Good luck! (It uses a lot of the funkier details of Unix virtual terminals in order to hook the one that the user is on to the one that the spawned application is on. It's also _really rarely implemented_ except in the original Expect.) – Donal Fellows Mar 01 '13 at 07:33

2 Answers2

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();
Alexey Gavrilov
  • 10,593
  • 2
  • 38
  • 48
  • Hi Alexey - Really appreciate expectit - currently trying to get interact working. What is "r" in this example? – Jon A Dec 28 '15 at 16:09
  • @JonA 'r' is a lambda function parameter. Try to play with the complete example: https://github.com/Alexey1Gavrilov/ExpectIt/blob/master/expect-java8/src/main/java/net/sf/expectit/java8/Java8Example.java – Alexey Gavrilov Dec 28 '15 at 18:20
  • I'm using maven dependency, contains not resolved for me.. why idea? – Nadhas Oct 05 '16 at 10:34
  • @Udhaya which version are you trying to use? This works find for me net.sf.expectit expectit-core 0.8.1 – Alexey Gavrilov Oct 05 '16 at 13:14
1

These libraries might suit your needs better:

SSHJ

https://github.com/shikhar/sshj

JSCH

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

Stephan Kristyn
  • 15,015
  • 14
  • 88
  • 147