Created
December 18, 2012 20:44
-
-
Save carlohamalainen/4331785 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Somewhere earlier: | |
session = jsch.getSession(cvlUsername, cvlHost, 22); | |
session.setPassword(cvlPassword); | |
session.setConfig("StrictHostKeyChecking", "no"); | |
session.connect(); | |
session.setServerAliveInterval(1000); | |
session.setServerAliveCountMax(30); | |
commandOutput = sendCommand(session, "uptime", true, launcherLogWindowTextArea); | |
/** | |
* This method sets up some buffers for reading STDOUT and STDERR | |
* from a remote command, and then waits for the command to | |
* finish before reading the STDOUT and STDERR from the buffers. | |
* This method is not be suitable for long-running commands where | |
* the Launcher needs to parse the output before the command has | |
* completed. The remoteCommand object passed into this method | |
* is updated with the resulting STDOUT and STDERR strings. | |
* The method returns the STDOUT string. | |
*/ | |
private String sendCommand(Session session, RemoteCommand remoteCommand, boolean showCommand, JTextArea launcherLogWindowTextArea) | |
{ | |
StringBuilder commandStderrBuffer = new StringBuilder(); | |
StringBuilder commandStdoutBuffer = new StringBuilder(); | |
try | |
{ | |
Channel channel = session.openChannel("exec"); | |
if (showCommand) | |
writeToLogWindow(launcherLogWindowTextArea, remoteCommand.getCommand() + "\n"); | |
((ChannelExec) channel).setCommand(remoteCommand.getCommand()); | |
channel.setInputStream(null); | |
//((ChannelExec) channel).setErrStream(System.err); | |
// Direct stderr output of command | |
InputStream fromChannelStderrStream = ((ChannelExec)channel).getErrStream(); | |
InputStreamReader fromChannelStderrStreamReader = new InputStreamReader(fromChannelStderrStream, "UTF-8"); | |
BufferedReader fromChannelStderrBufferedReader = new BufferedReader(fromChannelStderrStreamReader); | |
// Direct stdout output of command | |
InputStream fromChannelStdoutStream = channel.getInputStream(); | |
InputStreamReader fromChannelStdoutStreamReader = new InputStreamReader(fromChannelStdoutStream, "UTF-8"); | |
BufferedReader fromChannelStdoutBufferedReader = new BufferedReader(fromChannelStdoutStreamReader); | |
channel.connect(); | |
byte[] tmp = new byte[1024]; | |
int ch; | |
while (true) | |
{ | |
if (channel.isClosed()) | |
{ | |
remoteCommand.setExitCode(channel.getExitStatus()); | |
break; | |
} | |
try | |
{ | |
Thread.sleep(1000); | |
} | |
catch (Exception ee) | |
{ | |
throw new JSchException("Cannot execute remote command: " + remoteCommand.getCommand() + " : " + ee.getMessage()); | |
} | |
} | |
channel.disconnect(); | |
while ((ch = fromChannelStderrBufferedReader.read()) > -1) | |
commandStderrBuffer.append((char)ch); | |
while ((ch = fromChannelStdoutBufferedReader.read()) > -1) | |
commandStdoutBuffer.append((char)ch); | |
} | |
catch (Exception e) | |
{ | |
System.out.println(e); | |
} | |
remoteCommand.setStderr(commandStderrBuffer.toString()); | |
remoteCommand.setStdout(commandStdoutBuffer.toString()); | |
return remoteCommand.getStdout(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment