9

With 'expect', one can execute SSH commands and parse the output of those commands to alter program flow. I'd like to do this with Java. That is, I want my Java app to launch a SSH session, execute a command on the remote server, and depend on the output of that command execute the next command, without needing to start a new SSH session.

Is this possible in Java?

Thanks

jtnire
  • 1,348
  • 5
  • 21
  • 32

3 Answers3

7

Self-promoting my project: after looking at other Java-based Expect libraries, I decided to write my own... hope you find it useful. Please open issues in GitHub if you find any problem.

https://github.com/ronniedong/Expect-for-Java

For SSH, I suggest using JSch, as the example in the readme file.

Ronnie
  • 681
  • 7
  • 6
  • Indeed, this is elegant and simple, and is only coupled to the `java.io.InputStream` and `javax.io.OutputStream` interfaces (which are typically the constructs available from SSH libraries such as JSch or Ganymed), yet makes intelligent behind-the-scenes use of NIO construct for producer/consumer scenarios, `java.nio.channels.Pipe`. It's better than Expect4J (all those dependencies!) and better than Enchanter (un-maintained, synchronization bugs, no regex) or ExpectJ (no regex). Thankyou! – David Bullock Aug 29 '13 at 01:28
3

Try ExpectJ.   

tbodt
  • 16,609
  • 6
  • 58
  • 83
Rom1
  • 3,167
  • 2
  • 22
  • 39
  • That's an interesting project, however not really what I'm looking for here as I'd like to keep SSH pure Java - better exception handling that way. – jtnire Jun 18 '11 at 09:59
3

Yes, it's possible. There are some libraries, but I suggest Ganymed SSH-2. You can download it here, and there are also a lot of examples of using functions...

Vito Gentile
  • 13,336
  • 9
  • 61
  • 96
  • This looks good! However, the site says that the code isn't maintained anymore. Do you have experience with this code? Is it stable or still a bit buggy? The examples seems to fit the bill nicely – jtnire Jun 18 '11 at 09:58
  • Well, according to my experience it seems to work fine, but I'm not 100% sure... – Vito Gentile Jun 18 '11 at 11:34