how to implement expect "interact" command using java
Asked Answered
A

2

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

Attenuant answered 28/2, 2013 at 19:5 Comment(5)
code.google.com/p/expect4j/wiki/Installation perhaps here?Eichelberger
@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).Attenuant
@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?Attenuant
@LeonardoKenji: Try this bitbucket.org/djarvis/jexpectWoundwort
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.)Ionopause
F
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();
Frivolity answered 21/8, 2015 at 12:22 Comment(4)
Hi Alexey - Really appreciate expectit - currently trying to get interact working. What is "r" in this example?Imprudent
@JonA 'r' is a lambda function parameter. Try to play with the complete example: github.com/Alexey1Gavrilov/ExpectIt/blob/master/expect-java8/…Frivolity
I'm using maven dependency, contains not resolved for me.. why idea?Triarchy
@Udhaya which version are you trying to use? This works find for me <dependency> <groupId>net.sf.expectit</groupId> <artifactId>expectit-core</artifactId> <version>0.8.1</version> </dependency>Frivolity
H
1

These libraries might suit your needs better:

SSHJ

https://github.com/shikhar/sshj

JSCH

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

Hobbie answered 20/8, 2013 at 13:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.