title: Java library to run multiple unrelated commands on a remote server via ssh
body: <p>My Java application has to work like this:</p>
  
  <ol>
  <li>User select bash commands in GUI and press "send."</li>
  <li>Application return distinct and independent answers for each command (e.g. we could store them in different files).</li>
  <li>Commands each run interactively, not in a batch (it can't be something like "ls\n pwd \n" etc)</li>
  <li>After each command, the application will check if the results are ok. If so, it will send the next command.</li>
  <li>We need to execute <code>su &lt;user&gt;</code> on the remote host.</li>
  </ol>
  
  <p>This will be a plugin for a bigger app, so answers like "try something else" (i.e. RPC, web services) will not help me :(</p>
  
  <p>As far as i understand i have to use SHELL or at least keep channel connected. </p>
  
  <p>I have tested jsch , sshj and ethz.ssh2 but with bad results. </p>
  
  <p>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.</p>
  
  <p>I've used ExpectJ (with a little hack of output stream). It has resolved points 1,3,4,5. </p>
  
  <p><strong>But</strong> 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 : <strong>send , wait for full answer, send, wait...</strong> I've tried some basic synchronization tricks but this end in timeouts and connection lost usually.  </p>