Search in sources :

Example 6 with ChannelExec

use of com.jcraft.jsch.ChannelExec in project voltdb by VoltDB.

the class SFTPSession method exec.

public String exec(String command, int timeout) {
    ChannelExec channel = null;
    BufferedReader outStrBufRdr = null;
    BufferedReader errStrBufRdr = null;
    StringBuilder result = new StringBuilder(2048);
    try {
        try {
            channel = (ChannelExec) m_session.openChannel("exec");
        } catch (JSchException jex) {
            throw new SSHException("opening ssh exec channel", jex);
        }
        // Direct stdout output of command
        try {
            InputStream out = channel.getInputStream();
            InputStreamReader outStrRdr = new InputStreamReader(out, "UTF-8");
            outStrBufRdr = new BufferedReader(outStrRdr);
        } catch (IOException ioex) {
            throw new SSHException("geting exec channel input stream", ioex);
        }
        // Direct stderr output of command
        try {
            InputStream err = channel.getErrStream();
            InputStreamReader errStrRdr = new InputStreamReader(err, "UTF-8");
            errStrBufRdr = new BufferedReader(errStrRdr);
        } catch (IOException ioex) {
            throw new SSHException("getting exec channel error stream", ioex);
        }
        channel.setCommand(command);
        StringBuffer stdout = new StringBuffer();
        StringBuffer stderr = new StringBuffer();
        try {
            channel.connect(timeout);
            int retries = timeout / 100;
            while (!channel.isClosed() && retries-- > 0) {
                // Read from both streams here so that they are not blocked,
                // if they are blocked because the buffer is full, channel.isClosed() will never
                // be true.
                int ch;
                try {
                    while (outStrBufRdr.ready() && (ch = outStrBufRdr.read()) > -1) {
                        stdout.append((char) ch);
                    }
                } catch (IOException ioex) {
                    throw new SSHException("capturing '" + command + "' output", ioex);
                }
                try {
                    while (errStrBufRdr.ready() && (ch = errStrBufRdr.read()) > -1) {
                        stderr.append((char) ch);
                    }
                } catch (IOException ioex) {
                    throw new SSHException("capturing '" + command + "' error", ioex);
                }
                try {
                    Thread.sleep(100);
                } catch (InterruptedException ignoreIt) {
                }
            }
            if (retries < 0) {
                throw new SSHException("'" + command + "' timed out");
            }
        } catch (JSchException jex) {
            throw new SSHException("executing '" + command + "'", jex);
        }
        // In case there's still some more stuff in the buffers, read them
        int ch;
        try {
            while ((ch = outStrBufRdr.read()) > -1) {
                stdout.append((char) ch);
            }
        } catch (IOException ioex) {
            throw new SSHException("capturing '" + command + "' output", ioex);
        }
        try {
            while ((ch = errStrBufRdr.read()) > -1) {
                stderr.append((char) ch);
            }
        } catch (IOException ioex) {
            throw new SSHException("capturing '" + command + "' error", ioex);
        }
        if (stderr.length() > 0) {
            throw new SSHException(stderr.toString());
        }
        result.append(stdout.toString());
        result.append(stderr.toString());
    } finally {
        if (outStrBufRdr != null)
            try {
                outStrBufRdr.close();
            } catch (Exception ignoreIt) {
            }
        if (errStrBufRdr != null)
            try {
                errStrBufRdr.close();
            } catch (Exception ignoreIt) {
            }
        if (channel != null && channel.isConnected()) {
            // Shutdown the connection
            channel.disconnect();
        }
    }
    return result.toString();
}
Also used : JSchException(com.jcraft.jsch.JSchException) InputStreamReader(java.io.InputStreamReader) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) ChannelExec(com.jcraft.jsch.ChannelExec) SftpException(com.jcraft.jsch.SftpException) IOException(java.io.IOException) JSchException(com.jcraft.jsch.JSchException)

Example 7 with ChannelExec

use of com.jcraft.jsch.ChannelExec in project gerrit by GerritCodeReview.

the class SshSession method exec.

@SuppressWarnings("resource")
public String exec(String command, InputStream opt) throws JSchException, IOException {
    ChannelExec channel = (ChannelExec) getSession().openChannel("exec");
    try {
        channel.setCommand(command);
        channel.setInputStream(opt);
        InputStream in = channel.getInputStream();
        InputStream err = channel.getErrStream();
        channel.connect();
        Scanner s = new Scanner(err).useDelimiter("\\A");
        error = s.hasNext() ? s.next() : null;
        s = new Scanner(in).useDelimiter("\\A");
        return s.hasNext() ? s.next() : "";
    } finally {
        channel.disconnect();
    }
}
Also used : Scanner(java.util.Scanner) InputStream(java.io.InputStream) ChannelExec(com.jcraft.jsch.ChannelExec)

Example 8 with ChannelExec

use of com.jcraft.jsch.ChannelExec in project gerrit by GerritCodeReview.

the class SshSession method exec2.

public InputStream exec2(String command, InputStream opt) throws JSchException, IOException {
    ChannelExec channel = (ChannelExec) getSession().openChannel("exec");
    channel.setCommand(command);
    channel.setInputStream(opt);
    InputStream in = channel.getInputStream();
    channel.connect();
    return in;
}
Also used : InputStream(java.io.InputStream) ChannelExec(com.jcraft.jsch.ChannelExec)

Example 9 with ChannelExec

use of com.jcraft.jsch.ChannelExec in project che by eclipse.

the class JschSshClient method execAndGetOutput.

private String execAndGetOutput(String command) throws JSchException, MachineException, IOException {
    ChannelExec exec = (ChannelExec) session.openChannel("exec");
    exec.setCommand(command);
    try (BufferedReader reader = new BufferedReader(new InputStreamReader(exec.getInputStream()));
        InputStream erStream = exec.getErrStream()) {
        exec.connect(connectionTimeout);
        ListLineConsumer listLineConsumer = new ListLineConsumer();
        String line;
        while ((line = reader.readLine()) != null) {
            listLineConsumer.writeLine(line);
        }
        // read stream to wait until command finishes its work
        IoUtil.readStream(erStream);
        if (exec.getExitStatus() != 0) {
            throw new MachineException(format("Error code: %s. Error: %s", exec.getExitStatus(), IoUtil.readAndCloseQuietly(exec.getErrStream())));
        }
        return listLineConsumer.getText();
    } finally {
        exec.disconnect();
    }
}
Also used : ListLineConsumer(org.eclipse.che.api.core.util.ListLineConsumer) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) MachineException(org.eclipse.che.api.machine.server.exception.MachineException) BufferedReader(java.io.BufferedReader) ChannelExec(com.jcraft.jsch.ChannelExec)

Example 10 with ChannelExec

use of com.jcraft.jsch.ChannelExec in project che by eclipse.

the class JschSshClient method execAndGetCode.

private int execAndGetCode(String command) throws JSchException, IOException {
    ChannelExec exec = (ChannelExec) session.openChannel("exec");
    exec.setCommand(command);
    try (InputStream inStream = exec.getInputStream();
        InputStream erStream = exec.getErrStream()) {
        exec.connect(connectionTimeout);
        // read streams to wait until command finishes its work
        IoUtil.readStream(inStream);
        IoUtil.readStream(erStream);
    } finally {
        exec.disconnect();
    }
    return exec.getExitStatus();
}
Also used : InputStream(java.io.InputStream) ChannelExec(com.jcraft.jsch.ChannelExec)

Aggregations

ChannelExec (com.jcraft.jsch.ChannelExec)16 InputStream (java.io.InputStream)11 JSch (com.jcraft.jsch.JSch)7 Session (com.jcraft.jsch.Session)7 IOException (java.io.IOException)7 Channel (com.jcraft.jsch.Channel)6 JSchException (com.jcraft.jsch.JSchException)6 BufferedReader (java.io.BufferedReader)6 InputStreamReader (java.io.InputStreamReader)6 FileInputStream (java.io.FileInputStream)5 OutputStream (java.io.OutputStream)5 UserInfo (com.jcraft.jsch.UserInfo)3 File (java.io.File)3 SftpException (com.jcraft.jsch.SftpException)2 FileOutputStream (java.io.FileOutputStream)2 MachineException (org.eclipse.che.api.machine.server.exception.MachineException)2 VirtualMachine (com.microsoft.azure.management.compute.VirtualMachine)1 PublicIPAddress (com.microsoft.azure.management.network.PublicIPAddress)1 UserInfoPrompted (edu.umass.cs.aws.networktools.UserInfoPrompted)1 HeadlessException (java.awt.HeadlessException)1