1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
|
public int executeCommand(String command) {
int exitCode = -1;
ChannelExec channel = null;
BufferedReader stdoutReader = null;
BufferedReader stderrReader = null;
String line ;
String line_error="" ;
try {
channel = (ChannelExec) oSession.openChannel("exec");
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));
//while (stdoutReader.read() != -1) {
while ((line = stdoutReader.readLine()) != null ||(line_error = stderrReader.readLine()) != null) {
//System.out.println(stdoutReader.readLine());
//System.out.println(line) ;
if(line_error.length() > 0)
{
textarea.append(line_error+"\n") ;
line_error="";
}
textarea.append(line+"\n") ;
textarea.repaint() ;
}
//&& (line_error = stderrReader.readLine()) != null
exitCode = channel.getExitStatus();
return exitCode;
} catch (IOException e) {
System.out.println("Cannot execute remote command: " + command
+ " : " + e.getMessage());
} catch (Exception e) {
System.out.println("Cannot execute remote command: " + command
+ " : " + e.getMessage());
} finally {
try {
stdoutReader.close();
stderrReader.close();
} catch (Exception e) {
System.out.println(e.getMessage());
}
if (channel != null) {
channel.disconnect();
}
}
return exitCode;
} |
Partager