automating telnet with Groovy
I am doing a gradle/OSGi build.
I have the OSGi bundle building fine, but want to automate the bundle deployment. I don't think there is a gradle task for this, so this becomes a groovy question. To deploy to an osgi container you do the following:
- telnet to the OSGi container port
- send ss 开发者_Python百科command to list the bundles
- parse out the bundle in question
- uninstall the bundle via a "uninstall [ID]" command
- install the bundle via an "install file:///path to bundle" url
- parse the results
- exit telnet session.
Is there a way to telnet to a port using Groovy and send commands and read the output?
Thanks for any help, phil
I don't know about telnet, but I worked with Groovy and SSH using the AntBuilder and the sshexec task like this:
class SshClient {
def host
def username
def password
def execute (def command) {
def ant = new AntBuilder()
ant.sshexec(host : host,
username : username,
password : password,
command : command,
trust : "true",
outputproperty : "result")
return ant.project.properties."result"
}
}
def ssh = new SshClient ( host: "myhost",
username : "myuser",
password : "secret")
println ssh.execute("ls")
You will need the ant-jsch.jar and jsch-0.1.33.jar or higher in your classpath.
This should be simply doable with a normal socket and Stream Readers / Writer. Telnet is just a frontend for simple socket i/o that are text based protocols.
So, to do your steps:
- Create a normal socket to the destination host/port
- Write "ss"
- Create an inputstreamreader
- Consume everything available
- Parse to find your bundle id
- Send "uninstall " + bundleId
- Consume the stream until command prompt arrives / uninstall finished
- Send "install file://path/"
- Consume the stream until command prompt arrives / install finished
- socket.close()
Yeah, I know this resembles the steps you already wrote, but since telnet is not a real protocol, but just a frontend to text sockets, this should be easily doable for yourself.
精彩评论