2

My Java application has to work like this:

  1. User select bash commands in GUI and press "send."
  2. Application return distinct and independent answers for each command (e.g. we could store them in different files).
  3. Commands each run interactively, not in a batch (it can't be something like "ls\n pwd \n" etc)
  4. After each command, the application will check if the results are ok. If so, it will send the next command.
  5. We need to execute su <user> on the remote host.

This will be a plugin for a bigger app, so answers like "try something else" (i.e. RPC, web services) will not help me :(

As far as i understand i have to use SHELL or at least keep channel connected.

I have tested jsch , sshj and ethz.ssh2 but with bad results.

I've dug throu stackoverflow answers for questions like: "sending-commands-to-server-via-jsch-shell-channel" etc. But they all focus on sending whole commands in one line. I need an interactive, persistent SSH session.

I've used ExpectJ (with a little hack of output stream). It has resolved points 1,3,4,5.

But there is a problem with point 2. In my app I need to get separated answer. But we will not know their length. Command prompts can be different. Anyone knows how to "hack" ExpectJ so it will be some how more synchronized? I am looking for acting like this : send , wait for full answer, send, wait... I've tried some basic synchronization tricks but this end in timeouts and connection lost usually.

3
  • possible duplicate of SSH library for Java Commented Oct 2, 2012 at 12:30
  • why is it not possible to read one command per time and execute it and then if it is ok then read the next command and execute ? Commented Oct 2, 2012 at 12:39
  • the problem is that i have to keep session alive. mostly it refers to being other user then at the start (i can not use sudo in this commands) Commented Oct 2, 2012 at 12:48

2 Answers 2

2

You should use ExpectJ, a Java implementation of the Unix expect utility.

Sign up to request clarification or add additional context in comments.

3 Comments

As far as i tested it its working. ExpectJ + Jsch. Have some problems with getting each answer separated but i hope i can solve it (i think there is some kind of queue implemented in the ExpectJ and each "send" works in other thread)
In my app I need to get separated answer. But we will not know their length. Command prompts can be different. Do know how to "hack" ExpectJ so it will be some how more synchronized? I am looking for acting like this : send , wait for full answer, send, wait... I've tried some basic synchronization tricks but this end in timeouts and connection lost usually. Or maybe some trick to check if nothing new is going to appear in outputstream ?
Open a new StackOverflow question, and make sure ExpectJ is in the title.
0

not sure if you still have the problems, in any case, it might contribute to other people.

ExpectJ is indeed the Java implementation of Unix expect. and you should definitely buy the "explore expect book" then look into it, it is worth it.

For your question: when you spawn a process, you listen to the return output, match it to a prompt, then send some command. if you want to analyze the output, you buffer that output, and do some actions before the next send()

to do so, you need to use the interact() method of the spawn class you used. http://expectj.sourceforge.net/apidocs/index.html

and for interact and how it works: http://oreilly.com/catalog/expect/chapter/ch03.html look for this part: "The interact Command"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.