Package com.jcraft.jsch

Examples of com.jcraft.jsch.ChannelExec


        if (useSystemIn) {
            istream = KeepAliveInputStream.wrapSystemIn();
        }

        try {
            final ChannelExec channel;
            session.setTimeout((int) maxwait);
            /* execute the command */
            channel = (ChannelExec) session.openChannel("exec");
            channel.setCommand(cmd);
            channel.setOutputStream(tee);
            channel.setExtOutputStream(tee);
            channel.setErrStream(teeErr);
            if (istream != null) {
                channel.setInputStream(istream);
            }
            channel.setPty(usePty);
            channel.connect();
            // wait for it to finish
            thread =
                new Thread() {
                    public void run() {
                        while (!channel.isClosed()) {
                            if (thread == null) {
                                return;
                            }
                            try {
                                sleep(RETRY_INTERVAL);
                            } catch (Exception e) {
                                // ignored
                            }
                        }
                    }
                };

            thread.start();
            thread.join(maxwait);

            if (thread.isAlive()) {
                // ran out of time
                thread = null;
                if (getFailonerror()) {
                    throw new BuildException(TIMEOUT_MESSAGE);
                } else {
                    log(TIMEOUT_MESSAGE, Project.MSG_ERR);
                }
            } else {
                // stdout to outputFile
                if (outputFile != null) {
                    writeToFile(out.toString(), append, outputFile);
                }
                // set errorProperty
                if (errorProperty != null) {
                    getProject().setNewProperty(errorProperty, errout.toString());
                }
                // stderr to errorFile
                if (errorFile != null) {
                    writeToFile(errout.toString(), appenderr, errorFile);
                }
                // this is the wrong test if the remote OS is OpenVMS,
                // but there doesn't seem to be a way to detect it.
                int ec = channel.getExitStatus();
                // set resultproperty
                if (resultProperty != null) {
                    getProject().setNewProperty(resultProperty, Integer.toString(ec));
                }
                if (ec != 0) {
View Full Code Here


     */
    public List list(String parent) throws IOException {
        Message.debug("SShRepository:list called: " + parent);
        ArrayList result = new ArrayList();
        Session session = null;
        ChannelExec channel = null;
        session = getSession(parent);
        channel = getExecChannel(session);
        URI parentUri = null;
        try {
            parentUri = new URI(parent);
        } catch (URISyntaxException e1) {
            // failed earlier
        }
        String fullCmd = replaceArgument(listCommand, parentUri.getPath());
        channel.setCommand(fullCmd);
        StringBuffer stdOut = new StringBuffer();
        StringBuffer stdErr = new StringBuffer();
        readSessionOutput(channel, stdOut, stdErr);
        if (channel.getExitStatus() != 0) {
            Message.error("Ssh ListCommand exited with status != 0");
            Message.error(stdErr.toString());
            return null;
        } else {
            BufferedReader br = new BufferedReader(new StringReader(stdOut.toString()));
View Full Code Here

     * @param session
     * @return
     * @throws JSchException
     */
    private ChannelExec getExecChannel(Session session) throws IOException {
        ChannelExec channel;
        try {
            channel = (ChannelExec) session.openChannel("exec");
        } catch (JSchException e) {
            throw new IOException();
        }
View Full Code Here

     *            to create
     * @param connnection
     *            to use
     */
    private void makePath(String path, Session session) throws IOException {
        ChannelExec channel = null;
        String trimmed = path;
        try {
            while (trimmed.length() > 0 && trimmed.charAt(trimmed.length() - 1) == fileSeparator)
                trimmed = trimmed.substring(0, trimmed.length() - 1);
            if (trimmed.length() == 0 || checkExistence(trimmed, session)) {
                return;
            }
            int nextSlash = trimmed.lastIndexOf(fileSeparator);
            if (nextSlash > 0) {
                String parent = trimmed.substring(0, nextSlash);
                makePath(parent, session);
            }
            channel = getExecChannel(session);
            String mkdir = replaceArgument(createDirCommand, trimmed);
            Message.debug("SShRepository: trying to create path: " + mkdir);
            channel.setCommand(mkdir);
            StringBuffer stdOut = new StringBuffer();
            StringBuffer stdErr = new StringBuffer();
            readSessionOutput(channel, stdOut, stdErr);
        } finally {
            if (channel != null)
                channel.disconnect();
        }
    }
View Full Code Here

     *            to use
     * @return true: object exists, false otherwise
     */
    private boolean checkExistence(String filePath, Session session) throws IOException {
        Message.debug("SShRepository: checkExistence called: " + filePath);
        ChannelExec channel = null;
        channel = getExecChannel(session);
        String fullCmd = replaceArgument(existCommand, filePath);
        channel.setCommand(fullCmd);
        StringBuffer stdOut = new StringBuffer();
        StringBuffer stdErr = new StringBuffer();
        readSessionOutput(channel, stdOut, stdErr);
        return channel.getExitStatus() == 0;
    }
View Full Code Here

      Session session = sessions.poll();
      if (session == null) {
        session = createSession(host);
      }
     
      ChannelExec channel = (ChannelExec) session.openChannel("exec");
      if (remoteHome != null) {
        command = "cd " + remoteHome + ";" + command;
      }
      channel.setCommand(command);
      channel.setInputStream(null);
     
      ((ChannelExec)channel).setErrStream(errStr);
     
      InputStream in=channel.getInputStream();
      long startTime = System.nanoTime();
      channel.connect();
     
      byte[] tmp=new byte[1024];
      while(true){
          while(true){
            int i=in.read(tmp, 0, 1024);
            if(i<0) {
              break;
            }
            outStr.write(tmp, 0, i);
          }
          if(channel.isClosed()){
            exitCode = channel.getExitStatus();
            break;
          }
      }
      channel.disconnect();
      long endTime = System.nanoTime();
      sessions.add(session);

      String outText = new String(outStr.toByteArray());
      String errText = new String(errStr.toByteArray());
View Full Code Here

  /**
   * @return
   * @throws JSchException
   */
  private ChannelExec getExecChannel() throws JSchException {
    ChannelExec channel;
    channel = (ChannelExec) session.openChannel("exec");
    return channel;
  }
View Full Code Here

   *             in case of network problems
   * @throws Exception
   *             in case of problems on the target system (connection ok)
   */
  public void put(String localFile, String remoteTargetDir, String remoteTargetName, String mode) throws Exception {
    ChannelExec channel = null;

    if ((localFile == null) || (remoteTargetName == null)) {
      throw new IllegalArgumentException("Null argument.");
    }

    if (mode != null) {
      if (mode.length() != MODE_LENGTH) {
        throw new IllegalArgumentException("Invalid mode.");
      }

      for (int i = 0; i < mode.length(); i++) {
        if (!Character.isDigit(mode.charAt(i))) {
          throw new IllegalArgumentException("Invalid mode.");
        }
      }
    }

    String cmd = "scp -t ";
    if (mode != null) {
      cmd = cmd + "-p ";
    }
    if (remoteTargetDir != null && remoteTargetDir.length() > 0) {
      cmd = cmd + "-d " + remoteTargetDir;
    }

    try {
      channel = getExecChannel();
      channel.setCommand(cmd);
      sendFile(channel, localFile, remoteTargetName, mode);
      channel.disconnect();
    } catch (JSchException e) {
      if (channel != null) {
        channel.disconnect();
      }
      e.printStackTrace();
      // LOGGER.error(e);
      throw new Exception("Error during SCP transfer." + e.getMessage());
    } catch (Exception e) {
View Full Code Here

   *             in case of network problems
   * @throws Exception
   *             in case of problems on the target system (connection ok)
   */
  private void get(String remoteFile, OutputStream localTarget) throws IOException, Exception {
    ChannelExec channel = null;

    if ((remoteFile == null) || (localTarget == null)) {
      throw new IllegalArgumentException("Null argument.");
    }

    String cmd = "scp -p -f " + remoteFile;

    try {
      channel = getExecChannel();
      channel.setCommand(cmd);
      receiveStream(channel, remoteFile, localTarget);
      channel.disconnect();
    } catch (JSchException e) {
      if (channel != null) {
        channel.disconnect();
      }
      throw (IOException) new IOException("Error during SCP transfer." + e.getMessage()).initCause(e);
    }
  }
View Full Code Here

   *             in case of network problems
   * @throws Exception
   *             in case of problems on the target system (connection ok)
   */
  public FileInfo getFileinfo(String remoteFile) throws IOException, Exception {
    ChannelExec channel = null;
    FileInfo fileInfo = null;

    if (remoteFile == null) {
      throw new IllegalArgumentException("Null argument.");
    }

    String cmd = "scp -p -f \"" + remoteFile + "\"";

    try {
      channel = getExecChannel();
      channel.setCommand(cmd);
      fileInfo = receiveStream(channel, remoteFile, null);
      channel.disconnect();
    } catch (JSchException e) {
      throw (IOException) new IOException("Error during SCP transfer." + e.getMessage()).initCause(e);
    } finally {
      if (channel != null) {
        channel.disconnect();
      }
    }
    return fileInfo;
  }
View Full Code Here

TOP

Related Classes of com.jcraft.jsch.ChannelExec

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.