1

I want to control a remote system in Java via SSH using JSCH. The front end is a simple button GUI which triggers the execution of a command. Some of the controls are time critical, there should be no big delay between button press and command execution.

My problem: Every time a new channel is opened, the back-end needs about 8 seconds to initialize until the command is executed. (The back-end interface is implemented with RBSH afaik)

If I run a normal session via a console client, everything runs fine without bigger delays.

My question: Is there a way to initialize a channel to execute some commands and read the output(and only the command output) back sequentially?

I already figured out that session.openChannel("shell") could give the desired functionality, but I cant figure out how to do that properly.

EDIT: I'm not tied to JSCH. If there's another library which can do that, I'm also open for that

dnbjunkie
  • 21
  • 5

1 Answers1

2

You want an "exec" channel rather than a "shell" channel. SCP uses an exec channel, so look at one of the SCP examples or one of the SCP libraries on the Internet.

Alternately, if you control the remote server, you could define a "subsystem" for the command that you want to run, and run it through a subsystem channel. The big difference between an exec channel and a subsystem is who specifies the command to be executed. An exec channel will execute a command provided by the client. With a subsystem, the client just requests the subsystem by name, and the server runs the correct command (or provides the service in some other way). SFTP uses a subsystem called "sftp-server", so you could look at how Jsch's SFTP classes are implemented.

Kenster
  • 23,465
  • 21
  • 80
  • 106
  • If I get it right, both exec examples including closing the channel after the execution of a command. This is the case I have to avoid since every channel opening takes 8 seconds. For the subsystem idea: I have no control about the remote ssh implementation, it's a closed system. – dnbjunkie Sep 23 '13 at 18:09
  • Does it take 8 seconds _just_ to open a channel, or are you opening a new session each time as well? If it's taking 8 seconds to open any kind of channel, then maybe you should concentrate on fixing that. If you're stuck with this 8-second behavior, then there are ways to open a channel once and then run a sequence of commands without closing it. – Kenster Sep 23 '13 at 19:13
  • Have a look at this [example class](http://pastebin.com/F50640fV) The result of the call is http://pastebin.com/u5kAccaD – dnbjunkie Sep 23 '13 at 20:07