Package com.jcraft.jsch

Examples of com.jcraft.jsch.ChannelExec


    }

    public Streams executeCommand( String command, boolean ignoreFailures )
        throws CommandExecutionException
    {
        ChannelExec channel = null;
        BufferedReader stdoutReader = null;
        BufferedReader stderrReader = null;
        try
        {
            channel = (ChannelExec) session.openChannel( EXEC_CHANNEL );

            channel.setCommand( command + "\n" );

            InputStream stdout = channel.getInputStream();
            InputStream stderr = channel.getErrStream();

            channel.connect();

            stdoutReader = new BufferedReader( new InputStreamReader( stdout ) );
            stderrReader = new BufferedReader( new InputStreamReader( stderr ) );

            Streams streams = CommandExecutorStreamProcessor.processStreams( stderrReader, stdoutReader );

            if ( streams.getErr().length() > 0 && !ignoreFailures )
            {
                int exitCode = channel.getExitStatus();
                throw new CommandExecutionException( "Exit code: " + exitCode + " - " + streams.getErr() );
            }

            return streams;
        }
        catch ( IOException e )
        {
            throw new CommandExecutionException( "Cannot execute remote command: " + command, e );
        }
        catch ( JSchException e )
        {
            throw new CommandExecutionException( "Cannot execute remote command: " + command, e );
        }
        finally
        {
            IOUtil.close( stdoutReader );
            IOUtil.close( stderrReader );
            if ( channel != null )
            {
                channel.disconnect();
            }
        }
    }
View Full Code Here


        try {
            // execute the command
            Session session = openSession();
            session.setTimeout((int) maxwait);
            final ChannelExec channel = (ChannelExec) session.openChannel("exec");
            channel.setCommand(command);
            channel.setOutputStream(tee);
            channel.connect();

            // wait for it to finish
            thread =
                new Thread() {
                    public void run() {
                        while (!channel.isEOF()) {
                            if (thread == null) {
                                return;
                            }
                            try {
                                sleep(500);
                            } 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 {
                // completed successfully
                if (outputProperty != null) {
                    getProject().setProperty(outputProperty, out.toString());
                }
                if (outputFile != null) {
                    writeToFile(out.toString(), append, outputFile);
                }

                // 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();
                if (ec != 0) {
                    String msg = "Remote command failed with exit status " + ec;
                    if (getFailonerror()) {
                        throw new BuildException(msg);
                    } else {
View Full Code Here

    public AbstractSshMessage(Session session) {
        this.session = session;
    }

    protected Channel openExecChannel(String command) throws JSchException {
        ChannelExec channel = (ChannelExec) session.openChannel("exec");
        channel.setCommand(command);

        return channel;
    }
View Full Code Here

    @Override
    void execute(Session sshSession) throws SshException {
        InputStream in = null;
        OutputStream out = null;
        FileOutputStream fos = null;
        ChannelExec channel = null;
        try {
            try {
                long fileSize;
                String cmd = SCP_DOWNLOAD_COMMAND + this.scpFile.getRemoteFullPath();

                channel = setUpChannel(sshSession, cmd);
                fos = new FileOutputStream(scpFile.getLocalFile());
                in = channel.getInputStream();
                out = channel.getOutputStream();

                channel.connect();

                sendAck(out);

                fileSize = getFileSizeFromStream(in);
                skipFileName(in);

                sendAck(out);

                writePayloadToFile(in, out, fos, fileSize);
            } finally {
                if (out != null)
                    out.close();
                if (in != null)
                    in.close();
                if (fos != null)
                    fos.close();
                if (channel != null)
                    channel.disconnect();
            }
        } catch (Exception e) {
            throw new SshException(e);
        }
    }
View Full Code Here

    }

    @Override
    void execute(Session sshSession) throws SshException {
        InputStream in = null;
        ChannelExec channel = null;

        if (command == null) {
            throw new IllegalStateException("command attribute of " +
                    this.getClass().getName() + " can't be null.");
        }

        try {
            try {
                channel = this.setUpChannel(sshSession, command);
                in = channel.getInputStream();
                channel.connect();

                streamOutput(channel, in);
            } finally {
                if (in != null)
                    in.close();
                if (channel != null)
                    channel.disconnect();
            }
        } catch (Exception e) {
            throw new SshException(e);
        }
    }
View Full Code Here

        out.flush();
    }

    ChannelExec setUpChannel(Session sshSession, String cmd)
            throws JSchException {
        ChannelExec channel = (ChannelExec) sshSession.openChannel("exec");
        channel.setCommand(cmd);
        return channel;
    }
View Full Code Here

        }

        InputStream in = null;
        OutputStream out = null;
        InputStream fis = null;
        ChannelExec channel = null;

        try {
            try {
                final String cmd = SCP_UPLOAD_COMMAND + this.scpFile.getRemoteDirectory();

                channel = this.setUpChannel(sshSession, cmd);
                fis = new FileInputStream(scpFile.getLocalFile());
                in = channel.getInputStream();
                out = channel.getOutputStream();

                channel.connect();

                if (checkAck(in) != 0) {
                    throw new SshException("Acknowledgement check failed: Initializing "
                            + "session returned a status code other than 0");
                }

                sendFileSizeAndRemotePath(scpFile, out);

                if (checkAck(in) != 0) {
                    throw new SshException("Scp upload failed. Reason: sending filesize "
                            + "and filename returned a status code other than 0.");
                }

                sendPayloadToServer(out, fis);
                sendEOFToServer(out);

                if (checkAck(in) != 0) {
                    throw new SshException("Scp upload failed.  Reason: sending the  "
                            + " file payload resulted a status code other than 0");
                }
            } finally {
                if (out != null)
                    out.close();
                if (in != null)
                    in.close();
                if (fis != null)
                    fis.close();
                if (channel != null)
                    channel.disconnect();
            }
        } catch (Exception e) {
            throw new SshException(e);
        }
    }
View Full Code Here

        log.info("Result of SSH command [" + description + "]: " + result);
        return result;
    }

    private String executeCommand(String command) throws ExecuteException {
        ChannelExec channel = null;
        int exitStatus = -1;

        InputStream is = null;
        InputStream es = null;

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

            is = channel.getInputStream();
            es = channel.getErrStream();

            channel.connect(CONNECTION_TIMEOUT); // connect and execute command

            String out = read(is, channel);
            String err = read(es, channel);

            if (log.isTraceEnabled()) {
                log.trace("SSH command output: " + out);
            }

            if (err.length() > 0) {
                exitStatus = channel.getExitStatus();

                if (log.isTraceEnabled()) {
                    log.trace("SSH command error [" + exitStatus + "]: " + err);
                }

                if (exitStatus != 0) {
                    throw new ExecuteException(exitStatus, err, out);
                } else if (out.length() == 0) {
                    return err;
                }
            } else {
                exitStatus = 0;
            }

            return out;

        } catch (ExecuteException ee) {
            throw ee;
        } catch (Exception e) {
            throw new ExecuteException(exitStatus, e.toString());
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (Exception e) {
                }
            }

            if (es != null) {
                try {
                    es.close();
                } catch (Exception e) {
                }
            }

            if (channel != null) {
                try {
                    channel.disconnect();
                } catch (Exception e) {
                    log.error("Failed to disconnect", e);
                }
            }
        }
View Full Code Here

     * @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))) {
                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

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.