从Groovy中调用telnet作为shell命令



如何让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命令执行。

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

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

最新更新