Hi,

I'm using jsch 0.1.37 and I have a class that does an SSHExec.
I basically want it to trap all stdout and stderr messages and pass them 
along (with the exit status) to another client class.
The problem I'm having is that when I execute a shell script that does:
exit 1
jsch throws an Exception:
java.lang.Exception: An unknown error occured on the remote host while 
running the command: ...

Is there a way to tell jsch not to throw an exception if exit status is 1?
I want it to throw other exceptions.... host not found, etc.

In my code, I've put some traces to see where the exception is being 
throw, but I can't even figure that out.
Here's my code:

public class SSHExecRunner {
  private static final int SSH_PORT = 22;
  private static final int RETRY_INTERVAL = 1000;
  private static final String TIMEOUT_MESSAGE = "Timeout period 
exceeded, connection dropped.";

  private SSHUserInfo userInfo;
  private SSHExecVariable sshvars;
  private String knownHosts;
  private int port;
  private Thread thread = null;

  private SSHExecRunner() {
  // private constructor
  }

  private SSHExecRunner(final SSHExecVariable _sshvars) {
  this.sshvars = _sshvars;
  this.userInfo = new SSHUserInfo();
  userInfo.setKeyfile(sshvars.keyfile);
  userInfo.setName(sshvars.username);
  userInfo.setPassphrase(sshvars.passphrase);
  userInfo.setPassword(sshvars.password);
  userInfo.setTrust(sshvars.trust);

  this.knownHosts = sshvars.knownhosts;
  if (knownHosts == null || "".equals(knownHosts)) {
      this.knownHosts = System.getProperty("user.home") + 
"/.ssh/known_hosts";
  }

  this.port = SSH_PORT;
  if (sshvars.port > -1) {
      this.port = sshvars.port;
  }
  }

  /**
   * SSHExec.run performs the actual ssh connection and runs the command 
on the remote host. It uses an SSHExecVariable for it's configuration and
   * returns results via an SSHExecResult
   *
   * @param sshvars
   *                the sshvars
   *
   * @return the sSH exec result
   * @throws JSchException
   */
  public static SSHExecResult run(final SSHExecVariable sshvars) throws 
Exception {
  System.out.println("ssh runner starting");
  final SSHExecRunner runner = new SSHExecRunner(sshvars);
  SSHExecResult result = null;
  Session session = null;
  try {
      session = runner.openSession();
      result = runner.executeCommand(session, sshvars.command);
  } catch (final Exception e) {
      System.out.println("caught exception in runner");
      throw e;
  } finally {
      if (session != null && session.isConnected()) {
      session.disconnect();
      }
  }
  return result;
  }

  /**
   * Open an ssh session.
   *
   * @return the opened session
   * @throws JSchException
   *                 on error
   */
  private Session openSession() throws JSchException {
  final JSch jsch = new JSch();
  if (null != userInfo.getKeyfile()) {
      jsch.addIdentity(userInfo.getKeyfile());
  }

  if (!userInfo.getTrust() && knownHosts != null) {
      jsch.setKnownHosts(knownHosts);
  }

  final Session session = jsch.getSession(userInfo.getName(), 
sshvars.host, port);
  session.setUserInfo(userInfo);
  session.connect();
  return session;
  }

  private SSHExecResult executeCommand(final Session session, final 
String cmd) throws Exception {
  final ByteArrayOutputStream os = new ByteArrayOutputStream();
  final ByteArrayOutputStream es = new ByteArrayOutputStream();
  String out = "";
  String err = "";
  int ec = 0;
  try {
      System.out.println("trying to execute ssh command");
      final ChannelExec channel;
      session.setTimeout(sshvars.sshtimeout);
      /* execute the command */
      channel = (ChannelExec) session.openChannel("exec");
      channel.setCommand(cmd);
      channel.setOutputStream(os);
      channel.setExtOutputStream(es);
      channel.connect();
      // wait for it to finish
      thread = new Thread() {
      @Override
      public void run() {
          while (!channel.isClosed()) {
          if (thread == null) {
              return;
          }
          try {
              sleep(RETRY_INTERVAL);
          } catch (final Exception e) {
              System.out.println("error sleeping " + e.getMessage());
          }
          }
      }
      };

      thread.start();
      thread.join(sshvars.sshtimeout);

      if (thread.isAlive()) {
      // ran out of time
      thread = null;
      throw new Exception(TIMEOUT_MESSAGE);
      } else {
      // success
      out = os.toString();
      err = es.toString();
      ec = channel.getExitStatus();
      }

      return new SSHExecResult(ec, out, err);

  } catch (final Exception e) {
      System.out.println("caught exception in wrapper");
      throw e;
  } finally {
      if (os != null) {
      os.close();
      }
      if (es != null) {
      es.close();
      }
  }

  }
}


-------------------------------------------------------------------------
Check out the new SourceForge.net Marketplace.
It's the best place to buy or sell services for
just about anything Open Source.
http://sourceforge.net/services/buy/index.php
_______________________________________________
JSch-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/jsch-users

Reply via email to