【发布时间】: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
【问题讨论】:
-
我想你已经发现了 telnet 库存在的原因
标签: java linux groovy telnet processbuilder