开发者

automating telnet with Groovy

开发者 https://www.devze.com 2023-01-11 18:39 出处:网络
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 d

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:

  1. telnet to the OSGi container port
  2. send ss 开发者_Python百科command to list the bundles
  3. parse out the bundle in question
  4. uninstall the bundle via a "uninstall [ID]" command
  5. install the bundle via an "install file:///path to bundle" url
  6. parse the results
  7. 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消