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();
}
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();
}
}
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;
}
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();
}
}
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();
}
Aggregations