how to hook into system level expect with Java?

2020-05-03 11:07发布

问题:

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.

回答1:

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



回答2:

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.



回答3:

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