如何使用Groovy或Java捕获telnet提示符?

时间:2016-04-06 18:02:17

标签: java linux groovy telnet processbuilder

如何捕获通过telnet发回的提示使用操作系统的telnet ?这个groovy one-liner不会捕获提示符,即Press Return to continue:

我相信这在telnet中称为“线路周转提示”。

groovysh:

thufir@mordor:~$ 
thufir@mordor:~$ groovysh
Groovy Shell (1.8.6, JVM: 1.8.0_72)
Type 'help' or '\h' for help.
--------------------------------------------------------------------------------------------------------------------------------------
groovy:000> 'telnet rainmaker.wunderground.com 3000'.execute().inputStream.eachLine { line -> println line }
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:

潜在的问题是Java使用ProcessBuilder来启动操作系统的telnet应用程序。但是,如上所述,并非所有输出都被捕获。当有提示时,最后一行被省略。

我没有使用telnet库(例如Apache telnet)的原因仅仅是因为Apache telnet与操作系统的telnet不同(如图所示)。是的,有图书馆,但它们并不完全相同。

读入每个字符,而不是整行?

另见:

https://stackoverflow.com/a/36405986/262852

https://superuser.com/a/1061649/55747

https://stackoverflow.com/a/36451474/262852

1 个答案:

答案 0 :(得分:0)

我猜测提示是由telnet生成的,而不是从服务器收到的,因此会将其写入stderr而不是stdout

也请阅读process' standard error stream,看看是否有此提示。