从 Groovy 调用 telnet 作为 shell 命令
invoke telnet as a shell command from Groovy
如何让 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()
给你一个JavaProcess
。在你的情况下 UNIXProcess
。如果 telnet 以非交互方式执行(例如,您可以将其输出通过管道传输到文件),那么您可以读取 Process
的 InputStream
以获取其输出:
'telnet rainmaker.wunderground.com 3000'.execute().inputStream.eachLine { line ->
println line
}
如何让 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()
给你一个JavaProcess
。在你的情况下 UNIXProcess
。如果 telnet 以非交互方式执行(例如,您可以将其输出通过管道传输到文件),那么您可以读取 Process
的 InputStream
以获取其输出:
'telnet rainmaker.wunderground.com 3000'.execute().inputStream.eachLine { line ->
println line
}