【问题标题】:how can I capture the telnet prompt using either Groovy or Java?如何使用 Groovy 或 Java 捕获 telnet 提示?
【发布时间】:2016-07-27 07:34:09
【问题描述】:

我如何捕获通过 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

【问题讨论】:

  • 我想你已经发现了 telnet 库存在的原因

标签: java linux groovy telnet processbuilder


【解决方案1】:

我猜提示是telnet生成的,不是从服务器收到的,所以写到stderr而不是stdout

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

【讨论】:

    猜你喜欢
    • 2012-07-12
    • 1970-01-01
    • 2013-03-28
    • 1970-01-01
    • 1970-01-01
    • 2017-07-22
    • 2012-03-01
    • 2020-10-04
    • 1970-01-01
    相关资源
    最近更新 更多