/** * * {@link org.apache.commons.vfs2.provider.sftp.SftpFileSystem#executeCommand(java.lang.String, java.lang.StringBuilder) } */ private int executeCommand( String command, StringBuilder output ) throws JSchException, IOException { this.ensureSession(); ChannelExec channel = (ChannelExec) this.session.openChannel( "exec" ); channel.setCommand( command ); channel.setInputStream( (InputStream) null ); InputStreamReader stream = new InputStreamReader( channel.getInputStream() ); channel.setErrStream( System.err, true ); channel.connect(); char[] buffer = new char[128]; int read; while ( ( read = stream.read( buffer, 0, buffer.length ) ) >= 0 ) { output.append( buffer, 0, read ); } stream.close(); while ( !channel.isClosed() ) { try { Thread.sleep( 100L ); } catch ( Exception exc ) { log.logMinimal( "Warning: Error session closing. " + exc.getMessage() ); } } channel.disconnect(); return channel.getExitStatus(); }
/** * Execute a command through the ssh session, pumping its * stderr and stdout to our own logs. */ private int execCommand(Session session, String cmd) throws JSchException, InterruptedException, IOException { LOG.debug("Running cmd: " + cmd); ChannelExec exec = null; try { exec = (ChannelExec)session.openChannel("exec"); exec.setCommand(cmd); exec.setInputStream(null); exec.connect(); // Pump stdout of the command to our WARN logs StreamPumper outPumper = new StreamPumper(LOG, cmd + " via ssh", exec.getInputStream(), StreamPumper.StreamType.STDOUT); outPumper.start(); // Pump stderr of the command to our WARN logs StreamPumper errPumper = new StreamPumper(LOG, cmd + " via ssh", exec.getErrStream(), StreamPumper.StreamType.STDERR); errPumper.start(); outPumper.join(); errPumper.join(); return exec.getExitStatus(); } finally { cleanup(exec); } }
@Override public int exitValue() { if (isRunning()) throw new IllegalStateException(); return channel.getExitStatus(); }
private boolean isRunning() { return channel.getExitStatus() < 0 && channel.isConnected(); }
int exitStatus = channel.getExitStatus(); sshResponse.setStatusCodeInt(exitStatus); sshResponse.setStatusCode(Integer.toString(exitStatus));
@Override public Integer get() { int exitStatus = executor.getExitStatus(); return exitStatus != -1 ? exitStatus : null; }
@Override public Integer get() { int exitStatus = executor.getExitStatus(); return exitStatus != -1 ? exitStatus : null; }
@Override public Integer get() { int exitStatus = executor.getExitStatus(); return exitStatus != -1 ? exitStatus : null; }
@Override public int exitValue() { if (isRunning()) throw new IllegalStateException(); return channel.getExitStatus(); }
private boolean isRunning() { return channel.getExitStatus() < 0 && channel.isConnected(); }
@Override public int exitValue() { if (isRunning()) throw new IllegalStateException(); return channel.getExitStatus(); }
private boolean isRunning() { return channel.getExitStatus() < 0 && channel.isConnected(); }
@Override public void close() { // prior to closing, attempt to get the exit status (if we can) if (exitStatus == null &&!channel.isClosed()) { exitStatus = channel.getExitStatus(); } channel.disconnect(); session.disconnect(); }
@Override public void close() { // prior to closing, attempt to get the exit status (if we can) if (exitStatus == null &&!channel.isClosed()) { exitStatus = channel.getExitStatus(); } channel.disconnect(); session.disconnect(); }
@Override public int exitValue() throws IllegalThreadStateException { int exitStatus = channelExec.getExitStatus(); if (exitStatus == -1) { if (!channelExec.isConnected()) { // exit status for SIGHUP return 129; } throw new IllegalThreadStateException("Process not terminated"); } return exitStatus; }
/** * * @return the exit code of the executed command */ public int getCommandExitCode() { if (execChannel != null && isStandardOutputFullyRead() && isErrorOutputFullyRead()) { return execChannel.getExitStatus(); } return -1; }
@Override public int exitValue() throws IllegalThreadStateException { int exitStatus = channelExec.getExitStatus(); if (exitStatus == -1) { if (!channelExec.isConnected()) { // exit status for SIGHUP return 129; } throw new IllegalThreadStateException("Process not terminated"); } return exitStatus; }
public void copyFile(Session session, String sourceFile, String destinationFile) { ChannelExec channel = (ChannelExec) session.openChannel("exec"); channel.setCommand("cp " + sourceFile + " " + destinationFile); channel.connect(); while(channel.isConnected()) { Thread.sleep(20); } int status = channel.getExitStatus(); if(status != 0) throw new CopyException("copy failed, exit status is " + status); }
public void disconnect() { if (this.outReader != null) { this.outReader.stop(); } if (this.errReader != null) { this.errReader.stop(); } if (this.channel != null) { this.exitCode = this.channel.getExitStatus(); this.channel.disconnect(); } if (this.session != null) { this.session.disconnect(); } }
public void updateInterval(Session s, String filename) { String checkfortimeupdate = "fgrep '-send' \"public_html/final/" + filename + "\""; ChannelExec channel = (ChannelExec)session.openChannel("exec"); channel.setCommand(checkfortimeupdate); channel.setInputStream(null); channel.setErrStream(System.err); BufferedReader in = new BufferedReader(new InputStreamReader(channel.getInputStream(), encoding)); channel.connect(); eatOutput(in); if (channel.isClosed()) { System.out.println("UpdateInterval Closed exit-status: " + channel.getExitStatus()); break; } channel.disconnect(); }