0

See also a better question, more specific, on this topic.

I've just discovered expect, a tcl based scripting language for automating, among other things, telnet connections:

thufir@mordor:~/NetBeansProjects/expect$ 
thufir@mordor:~/NetBeansProjects/expect$ expect example rainmaker.wunderground.com 3000
spawn telnet rainmaker.wunderground.com 3000
Trying 38.102.137.140...
Connected to rainmaker.wunderground.com.
Escape character is '^]'.
------------------------------------------------------------------------------
*               Welcome to THE WEATHER UNDERGROUND telnet service!            *
------------------------------------------------------------------------------
*                                                                            *
*   National Weather Service information provided by Alden Electronics, Inc. *
*    and updated each minute as reports come in over our data feed.          *
*                                                                            *
*   **Note: If you cannot get past this opening screen, you must use a       *
*   different version of the "telnet" program--some of the ones for IBM      *
*   compatible PC's have a bug that prevents proper connection.              *
*                                                                            *
*           comments: jmasters@wunderground.com                              *
------------------------------------------------------------------------------

Press Return to continue:usage: send [args] string
    while executing
"send – – “\r”"
    (file "example" line 9)
thufir@mordor:~/NetBeansProjects/expect$ 

What mechanisms are available to either hook into expect with Java, or into Java with expect? Yes, there are http://tcljava.sourceforge.net/ as well as a few others, but they seem out of date.

If there's a tcl implementation in Java, and why not, that would work, I'm sure. However, I'm inferring, from the lack of updates to web pages, that nothing was ever fully implemented...

Wikipedia says:

Java

expect4j — an attempt at a Java clone of the original Expect
ExpectJ — a Java implementation of the Unix expect utility
Expect-for-Java — pure Java implementation of the Expect tool

However, expect4j says that it's an attempt, ExpectJ hasn't been changed since 2010, and Expect-for-Java also hasn't been touched in years.

If these libraries are used and functioning, my apologies, but the dates indicated that they aren't maintained.

I would rather use expect and Java separately, but so that they interact. Is there a hook or mechanism for passing messages between the two?

-----------------------------------------------------------UPDATE----------

**

Jacl is a Tcl 8.x interpreter written in Java. You can script your Java applications in Tcl.

**

http://wiki.tcl.tk/1215

However, the library hasn't been touched in some time. I'm not quite sure how well it works. I don't know whether it supports anything like expect or not, it looks like the various expect attempts are not being maintained.

Most likely the answer by Donal Fellows, that there's no real solution, is correct. However, it might just be possible to use this tcl library to initiate a telnet session, invoking real telnet, and not with process builder, but that's very tentative.

Community
  • 1
  • 1
Thufir
  • 8,216
  • 28
  • 125
  • 273
  • Define "interact" here? What **exactly** are you trying to do from java? What from expect? And how do you need them to "interact"? – Etan Reisner Apr 06 '16 at 11:51
  • hook into, pass messages between. How can I pass messages to a Java program from expect, and vice versa? see also http://stackoverflow.com/questions/15143619/how-to-implement-expect-interact-command-using-java for a similar question with a different solution (ssh is different from telnet). – Thufir Apr 06 '16 at 11:56
  • You want to use `expect` to automate a `telnet` connection? You want to control a java application from `expect`? You want to have java see the contents of the `telnet` connection? You want to write the `expect`-like logic *in* java? – Etan Reisner Apr 06 '16 at 12:26
  • something like that. Java doesn't hook into telnet well. From http://wiki.tcl.tk/1215 it looks like this is doable, although I'm just beginning to read documentation. Seems that `expect` drives, or hooks into, telnet much better (presumably because tcl derives from ?c?). I want to make the bulk of the program in Java, just use tcl to establish the connection. – Thufir Apr 06 '16 at 12:34
  • `expect` doesn't "hook" into anything. It acts as the input and output devices (by pretending to be a terminal of sorts) and then "drives" the spawned applications by "expect"ing certain output and providing the scripted input. That's it. You don't *need* expect to do that it just happens to exist and do its job well. You could do it all yourself if you wanted to bother. – Etan Reisner Apr 06 '16 at 12:39
  • @EtanReisner this http://stackoverflow.com/q/26541669/262852 is a more specific question, perhaps I phrased this question incorrectly or too vaguely. Is it the wrong terminology to say that A "hooks" "into" B? – Thufir Apr 11 '16 at 09:02
  • I don't see how that question is related to this question though. If you want to execute `expect` itself and just read its standard output I don't see how that should be a problem but it seems like maybe that's not what you want? – Etan Reisner Apr 13 '16 at 15:57

3 Answers3

1

What mechanisms are available to either hook into expect with Java, or into Java with expect?

None really. The problem is that expect does some really complicated low-level things to manage pseudo-terminals and that uses OS interfaces that the JRE doesn't expose. (It's even more tricky if you're on Windows, where it uses the system debug API.) You'd have to go to doing nasty stuff with native code.

The easy route is to run expect as a subprocess via ProcessBuilder.

Donal Fellows
  • 133,037
  • 18
  • 149
  • 215
1

As with my knowledge Swig would help you better in hooking up the the two languages. Let me know in case i was wrong

Venkata Vamsy
  • 92
  • 1
  • 6
  • http://www.swig.org/compat.html#SupportedLanguages lists Java and Ruby as well as tcl. Very, very intriguing. – Thufir Apr 11 '16 at 04:09
1

I have created yet another Java expect library ExpectIt (not listed in Wikipedia) which implements interact loop. Have a look at this example.

Alexey Gavrilov
  • 10,593
  • 2
  • 38
  • 48
  • Do you have to use `import org.apache.commons.net.telnet.TelnetClient` or would it be possible to use the system telnet? – Thufir Apr 26 '16 at 01:57
  • 1
    The library, as all the other Java libraries, uses standard Java streams which can come from a Telnet client, a socket connection, or any other stream source. Here is a telnet example: https://github.com/Alexey1Gavrilov/ExpectIt/blob/master/expectit-core/src/test/java/net/sf/expectit/TelnetExample.java – Alexey Gavrilov Apr 26 '16 at 07:12
  • This question morphed and got away from me, I narrowed it down to your library and asked [a more specific question here, with regards to the Java stream for a Telnet client](http://stackoverflow.com/questions/36865567/how-to-use-expectit-to-interact-with-the-telnet-stream). – Thufir Apr 26 '16 at 12:55