Package com.jcraft.jsch

Examples of com.jcraft.jsch.ChannelExec


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

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

        if (mode.length() != 4) {
            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 (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();
            }
            throw (IOException) new IOException("Error during SCP transfer." + e.getMessage())
                    .initCause(e);
        }
    }
View Full Code Here


     * @throws RemoteScpException
     *             in case of problems on the target system (connection ok)
     */
    public void get(String remoteFile, OutputStream localTarget) throws IOException,
            RemoteScpException {
        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 RemoteScpException
     *             in case of problems on the target system (connection ok)
     */
    public FileInfo getFileinfo(String remoteFile) throws IOException, RemoteScpException {
        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

     */
    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

   * @param source The path to the contents to retrieve
   * @param target The target stream for the contents
   * @throws IOException
   */
  public void scp (VirtualPath source, OutputStream targetOutput) throws IOException {
    ChannelExec scp = this.getSCPChannel("scp -f " + source.toString("/"));
   
    try {
      InputStream input = scp.getInputStream();
      OutputStream output = scp.getOutputStream();
     
      this.sendAckResponse(output);
     
      while ( getAckResponse(input) != 'C' ) {
        for (int i=0; i<5; i++) input.read();
       
        long size = 0;
        while (true) {
          int ch = input.read();
          if (ch == ' ') break;
          size += (10 * ch);
        }
       
        StringBuffer filename = new StringBuffer();
        while (true) {
          int ch = input.read();
          if (ch == 0x0a) break;
          filename.append( (char) ch );
        }
       
        this.sendAckResponse(output);
       
        byte[] buffer = new byte[1024];
        while (true) {
          int read = input.read(buffer);
          if (read == -1) break;
         
          targetOutput.write(buffer, 0, read);
          size -= read;
         
          if (size == 0) break;
          if (size < 0) throw new SecureSCPException("File download overflow to");
        }
       
        if (this.getAckResponse(input) != 0)
          throw new SecureSCPException("Invalid ACK response when sending file " + source);
       
        this.sendAckResponse(output);
      }
    } finally {
      scp.disconnect();
    }
  }
View Full Code Here

   * @param source The source for the contents to send
   * @param target The path for the contents
   * @throws IOException
   */
  public void scp (VirtualFile source, VirtualPath target) throws IOException {
    ChannelExec scp = this.getSCPChannel("scp -p -t " + target.toString("/"));
    InputStream sourceInputStream = null;
   
    try {
      InputStream input = scp.getInputStream();
      OutputStream output = scp.getOutputStream();
     
      if (this.getAckResponse(input) != ACK_OK)
        throw new SecureSCPException("Invalid ACK response when sending file " + source);
     
      output.write(("C0644 " + source.getSize() + " " + source.getPath().toString("/") + "\n").getBytes());
      output.flush();
     
      if (this.getAckResponse(input) != ACK_OK)
        throw new SecureSCPException("Invalid ACK response when transfering file " + source);
     
      IOUtil.transfer(sourceInputStream = source.getInputStream(), output);
     
      this.sendAckResponse(output);
 
      if (this.getAckResponse(input) != ACK_OK)
        throw new SecureSCPException("Invalid ACK response when transfering file " + source);
     
      output.close();
    } finally {
      scp.disconnect();
      if (sourceInputStream != null) try { sourceInputStream.close(); } catch (IOException e) {}
    }
  }
View Full Code Here

   * @param command The command to execute
   * @return The channel for SCP transfers
   */
  private ChannelExec getSCPChannel (String command) {
    try {
      ChannelExec exec = (ChannelExec) this.session.openChannel("exec");
      exec.setCommand(command);
      exec.connect(timeout);
      return exec;
    } catch (JSchException e) {
      throw ThrowableManagerRegistry.caught(e);
    }
  }
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.