从Groovy调用telnet作为shell命令 [英] invoke telnet as a shell command from Groovy

查看:232
本文介绍了从Groovy调用telnet作为shell命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这种情况下,我怎样才能得到groovy来调用telnet客户端,以便显示来自服务器的回复?

How can I get groovy, in this case groovysh, to invoke the telnet client so that replies from the server are displayed?

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命令。

I'm aware of numerous telnet libraries for Java, but in this case, want to to execute telnet as a shell command.

推荐答案

execute 给你一个Java Process 。在你的情况下,一个 UNIXProcess 。如果telnet以非交互方式执行(例如,您可以将其输出到一个文件),那么您可以阅读 Process InputStream 得到它的输出:

execute() gives you a Java Process. In your case a UNIXProcess. If telnet executes in a non-interactive fashion (ex. you can pipe it's output to a file), then you can read the Process's InputStream to get it's output:

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

这篇关于从Groovy调用telnet作为shell命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆