static int checkAck(InputStream in) throws IOException {
    int b = in.read();
    // b may be 0 for success,
    //          1 for error,
    //          2 for fatal error,
    //          -1
    if (b == 0) return b;
    if (b == -1) return b;

    if (b == 1 || b == 2) {
      StringBuffer sb = new StringBuffer();
      int c;
      do {
        c = in.read();
        sb.append((char) c);
      } while (c != '\n');
      if (b == 1) { // error
        System.out.print(sb.toString());
      }
      if (b == 2) { // fatal error
        System.out.print(sb.toString());
      }
    }
    return b;
  }
  static int checkAck(InputStream in) throws IOException {
    int b = in.read();
    // b may be 0 for success,
    // 1 for error,
    // 2 for fatal error,
    // -1
    if (b == 0) return b;
    if (b == -1) return b;

    if (b == 1 || b == 2) {
      StringBuffer sb = new StringBuffer();
      int c;
      do {
        c = in.read();
        sb.append((char) c);
      } while (c != '\n');
      if (b == 1 || b == 2) { // error
        throw new IOException(
            Messages.ScpRetrieveFileTransfer_EXCEPTION_SCP_PROTOCOL + ": " + sb.toString());
      }
    }
    return b;
  }
Example #3
0
  /**
   * execute the given command in the remote host
   *
   * @param command
   * @return - command output in the remote host
   */
  public String exec(String command) {
    if (!connected) {
      throw new ActionFailedException("There is no session!");
    }
    StringBuffer data = new StringBuffer();
    OutputStream out = null;
    InputStream in = null;
    try {
      Channel channel;

      boolean channel_connected = false;
      int count = 0;
      while (!channel_connected) {

        try {
          channel = session.openChannel("exec");
          ((ChannelExec) channel).setCommand(command);

          out = channel.getOutputStream();
          in = channel.getInputStream();
          channel.connect();
          channel_connected = true;
        } catch (Exception e) {
          count++;
          String msg = e.getMessage();
          if (count < 5) {
            AutomationLogger.getInstance()
                .warn(
                    "Failed to connect to SSH server due to "
                        + msg
                        + ". Will try again in 1 second");
            if (msg.startsWith("session is down")) {
              AutomationLogger.getInstance().info("Try to re-connect session");
              connect();
            }
            Timer.sleep(1000);
          } else {
            throw new ActionFailedException("Failed to connect to SSH server due to " + e);
          }
        }
      }

      byte[] buf = new byte[1024];

      // read
      count = 0;
      while ((count = in.read(buf)) > 0) {
        data.append(new String(buf, 0, count));
      }

    } catch (Exception e) {
      AutomationLogger.getInstance().warn(e);
    } finally {
      try {
        in.close();
      } catch (Exception e) {
      }
      try {
        out.close();
      } catch (Exception e) {
      }

      if (channel != null) {
        channel.disconnect();
      }
    }
    return data.toString();
  }