Package com.jcraft.jsch

Examples of com.jcraft.jsch.ChannelExec


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

     * Tries to create a directory path on the target system
     * @param path 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

     * @param session 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

     * @throws IOException in case of network problems
     * @throws RemoteScpException in case of problems on the target system (connection ok)
     */
    public void put(byte[] data, String remoteFileName, String remoteTargetDirectory, String mode)
    throws IOException,RemoteScpException {
        ChannelExec channel = null;

        if ((remoteFileName == 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)) == false)
                throw new IllegalArgumentException("Invalid mode.");

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

        try {
            channel = getExecChannel();
            channel.setCommand(cmd);
            sendBytes(channel, data, remoteFileName, 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

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

     * @throws IOException in case of network problems
     * @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)) == false)
                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

     * @param localTarget OutputStream to store the data.
     * @throws IOException in case of network problems
     * @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

     * @return the file information got
     * @throws IOException 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

        assertTrue(file.exists());
        assertEquals(length, file.length());
    }

    protected String readFile(String path) throws Exception {
        ChannelExec c = (ChannelExec) session.openChannel("exec");
        OutputStream os = c.getOutputStream();
        InputStream is = c.getInputStream();
        c.setCommand("scp -f " + path);
        c.connect();
        String header = readLine(is);
        assertEquals("C0644 11 out.txt", header);
        int length = Integer.parseInt(header.substring(6, header.indexOf(' ', 6)));
        os.write(0);
        os.flush();

        byte[] buffer = new byte[length];
        length = is.read(buffer, 0, buffer.length);
        assertEquals(length, buffer.length);
        assertEquals(0, is.read());
        os.write(0);
        os.flush();

        c.disconnect();
        return new String(buffer);
    }
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.