invoke telnet as a shell command from Groovy

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-13 02:17:54

问题


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:~$ 

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


回答1:


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
}


来源:https://stackoverflow.com/questions/36403934/invoke-telnet-as-a-shell-command-from-groovy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!