【问题标题】:invoke telnet as a shell command from Groovy从 Groovy 调用 telnet 作为 shell 命令
【发布时间】:2016-07-24 01:41:11
【问题描述】:

如何让 groovy(在本例中为 groovysh)调用 telnet 客户端,以便显示来自服务器的回复?

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()
===> java.lang.UNIXProcess@8458f04
groovy:000> 
groovy:000> exit
thufir@mordor:~$ 

我知道有很多 Java telnet 库,但在这种情况下,我想将 telnet 作为 shell 命令执行。

【问题讨论】:

    标签: linux shell groovy io telnet


    【解决方案1】:

    execute() 为您提供 Java Process。在您的情况下为UNIXProcess。如果 telnet 以非交互方式执行(例如,您可以通过管道将其输出到文件),那么您可以读取 ProcessInputStream 以获取其输出:

    'telnet rainmaker.wunderground.com 3000'.execute().inputStream.eachLine { line ->
        println line
    }
    

    【讨论】:

    • 有没有办法得到最后一行? "按 Return 继续:"
    猜你喜欢
    • 2012-09-12
    • 1970-01-01
    • 2010-09-14
    • 2015-03-25
    • 2016-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多