Пример #1
0
  /**
   * Execute the command on the destination path and parse the output to return an integer
   *
   * @param command
   * @param path
   * @return Process PID or null if can't get it.
   * @throws RemoteAccessException if connection down
   */
  private Integer execute(String command, Path path) throws RemoteAccessException {
    log.debug(command);
    ChannelExec channel = null;
    StringBuilder sb = new StringBuilder();
    try {
      channel = channelManager.getExecChannel(path);
      // TODO: Decor with tee to write on standard err and out stream and
      // alse remote scratch file

      channel.setCommand(command);

      channel.setInputStream(null);
      channel.setErrStream(System.err);

      InputStream in = channel.getInputStream();

      channel.connect();

      // Read Pid
      byte[] tmp = new byte[1024];
      while (true) {
        while (in.available() > 0) {
          int i = in.read(tmp, 0, 1024);
          if (i < 0) {
            break;
          }
          sb.append(new String(tmp, 0, i));
        }
        if (channel.isClosed()) {

          break;
        }
        try {
          Thread.sleep(1000);
        } catch (Exception e) {
          log.error("Runtime error. Thread Interupted.", e);
          throw new RemoteAccessException("Thread interupted", e);
        }
      }
      if (channel.getExitStatus() < 0) {
        throw new RemoteAccessException("SSH connection close abruptly");
      }

    } catch (Exception e) {
      throw new RemoteAccessException("Unable to SSH connect. Command: " + command, e);
    } finally {
      try {
        if (channel != null) {
          channel.disconnect();
        }
      } catch (Exception e) {
        log.error("SSH disconnect error", e);
      }
    }
    // parse pid
    Integer pid = null;
    try {
      pid = Integer.valueOf(sb.toString().trim());
    } catch (NumberFormatException e) {
      log.warn("Unable to convert the command output to PID. Output =  " + sb.toString());
    }
    return pid;
  }
Пример #2
0
 public JSch getJsch() {
   return channelManager.getJsch();
 }
Пример #3
0
 @Override
 public void removeItentity(SSHKeyManager sshkey) throws RemoveAuthentificationException {
   channelManager.removeIdentity(sshkey);
 }
Пример #4
0
 @Override
 public void addItentity(SSHKeyManager sshkey) throws AddAuthentificationException {
   channelManager.addIdentity(sshkey);
 }
Пример #5
0
 // TODO: to improve
 public void debug(boolean activate) {
   channelManager.debug(activate);
 }
Пример #6
0
  /*
   * (non-Javadoc)
   *
   * @see com.sysfera.godiet.Utils.RemoteAccess#copy(java.io.File,
   * java.lang.String)
   */
  @Override
  public void copy(ConfigurationFile localFile, String remotePath, Path path)
      throws RemoteAccessException {
    InputStream fis = null;

    Channel channel = null;
    try {
      channel = channelManager.getExecChannel(path);
      // exec 'scp -t rfile' remotely
      String command = "scp -p -t " + remotePath + "/";
      ((ChannelExec) channel).setCommand(command);

      // get I/O streams for remote scp
      OutputStream out = channel.getOutputStream();
      InputStream in = channel.getInputStream();

      channel.connect();

      if (checkAck(in) != 0) {
        throw new RemoteAccessException("Unable to scp");
        // on : " + user+ "@" + host + ":" + port);
      }

      // send "C0644 filesize filename", where filename should not include
      // '/'
      long filesize = localFile.getContents().getBytes().length;
      String localFileName = localFile.getAbsolutePath();
      command = "C0644 " + filesize + " ";
      if (localFileName.lastIndexOf('/') > 0) {
        command += localFileName.substring(localFileName.lastIndexOf('/') + 1);
      } else {
        command += localFileName;
      }
      command += "\n";
      out.write(command.getBytes());
      out.flush();
      if (checkAck(in) != 0) {
        throw new RemoteAccessException("Unable to scp ");
        // + user + "@" + host + ":" + port + "Command: " + command);
      }

      // send a content of lfile
      fis = new ByteArrayInputStream(localFile.getContents().getBytes());
      byte[] buf = new byte[1024];
      while (true) {
        int len = fis.read(buf, 0, buf.length);
        if (len <= 0) {
          break;
        }
        out.write(buf, 0, len); // out.flush();
      }
      fis.close();
      fis = null;
      // send '\0'
      buf[0] = 0;
      out.write(buf, 0, 1);
      out.flush();
      if (checkAck(in) != 0) {
        throw new RemoteAccessException("Error when close connection ");
        // + user +"@" + host + ":" + port + "Command: " + command);
      }
      out.close();
      channel.disconnect();
    } catch (Exception e) {
      throw new RemoteAccessException("Unable to scp"); // on :
      // " + user + "@"
      // + host + ":" + port, e);
    } finally {
      try {
        if (channel != null) {
          channel.disconnect();
        }
        try {
          if (fis != null) {
            fis.close();
          }
        } catch (Exception ee) {
        }
      } catch (Exception e) {
        log.error("SSH disconnect error", e);
      }
    }
  }