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 connect
I have created yet another Java expect library ExpectIt (not listed in Wikipedia) which implements interact loop. Have a look at this example.