/** * * {@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(); }
if (channel.isClosed()) { if (in.available() > 0) continue;
public boolean isTerminated() { return channel.isClosed(); }
public boolean isTerminated() { return channel.isClosed(); }
private static void waitForChannelClosure(ChannelExec ce, long maxwaitMs) { log.info("waitForChannelClosure >>>"); final long until = System.currentTimeMillis() + maxwaitMs; try { while (!ce.isClosed() && System.currentTimeMillis() < until) { log.info("SFTP channel not closed .. waiting"); Thread.sleep(250); } } catch (InterruptedException e) { throw new RuntimeException("Interrupted", e); } if (!ce.isClosed()) { throw new RuntimeException("Channel not closed in timely manner!"); } };
protected void waitForClosed(ChannelExec exec, long millis) { while (!exec.isClosed()) { Threads.sleep(millis); } }
protected void waitForClosed(ChannelExec exec, long millis) { while (!exec.isClosed()) { Threads.sleep(millis); } }
protected void waitForClosed(ChannelExec exec, long millis) { while (!exec.isClosed()) { sleep(millis); } }
public void run() { while (!channel.isClosed()) { try { sleep(RETRY_INTERVAL); } catch (InterruptedException e) { // ignored } } } };
public void run() { while (!channel.isClosed()) { if (thread == null) { return; } try { sleep(RETRY_INTERVAL); } catch (Exception e) { // ignored } } } };
@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 read() throws IOException { if (channel.isClosed() && channel.getExitStatus() != 0) { throw new IOException(StreamUtils.readToString(channelErrorStream)); } while (!(channel.isClosed() && channelInputStream.available() == 0)) { if (channelInputStream.available() > 0) { int data = channelInputStream.read(); if (data == -1) { continue; } return data; } try { Thread.sleep(WAIT_DELAY); } catch (InterruptedException e) { break; } } return -1; }
@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(); }
/** * Wait until it's done and return its code. * @param exec The channel * @return The exit code * @throws IOException If some IO problem inside */ @SuppressWarnings("PMD.AvoidCatchingGenericException") private int code(final ChannelExec exec) throws IOException { while (!exec.isClosed()) { try { this.session.sendKeepAliveMsg(); // @checkstyle IllegalCatch (1 line) } catch (final Exception ex) { throw new IOException(ex); } try { TimeUnit.SECONDS.sleep(1L); } catch (final InterruptedException ex) { Thread.currentThread().interrupt(); throw new IOException(ex); } } return exec.getExitStatus(); } }
private void runCmds() throws JSchException, IOException, InterruptedException, ExecutionException { ChannelExec channel = ssh.run(cmds); channel.getOutputStream().close(); Future<String> output = sshHelper.getOutputString(channel); Future<String> error = sshHelper.getErrorString(channel); String outputText = output.get(); String errorText = error.get(); while (true) { if (channel.isClosed()) { int exitCode = channel.getExitStatus(); if (exitCode != 0) { throw new IOException("Command failed with code " + exitCode + " : " + errorText); } break; } Thread.sleep(1000); } log.debug("Command out: " + outputText); log.debug("Command err: " + errorText); }
//Start EXEC ChannelExec ce = (ChannelExec) session.openChannel("exec"); ce.setCommand("screen -S " + name + " -X stuff 'quit\n'"); ce.setInputStream(null); ce.setErrStream(System.err); InputStream in=ce.getInputStream(); ce.connect(); byte[] tmp=new byte[1024]; while(true){ while(in.available()>0){ int i=in.read(tmp, 0, 1024); if(i<0)break; //System.out.print(new String(tmp, 0, i)); } if(ce.isClosed()){ if(in.available()>0) continue; //System.out.println("exit-status: "+ce.getExitStatus()); break; } try{Thread.sleep(1000);}catch(Exception ee){} } ce.disconnect(); //STOP EXEC
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(); }
java.util.Properties config = new java.util.Properties(); config.put("StrictHostKeyChecking", "no"); Session session = null; ChannelExec channel = null; try { JSch jSch = new JSch(); jSch.addIdentity("/tmp/privatekey"); session = jSch.getSession("testuser", address, 22); session.setConfig(config); session.connect(); channel = (ChannelExec) session.openChannel("exec"); BufferedReader in = new BufferedReader(new InputStreamReader(channel.getInputStream())); channel.setCommand(command); channel.connect(); if (channel.getExitStatus() == 0 || channel.isClosed() || channel.isEOF()) { logger.info("SSH connection is successful!"); } in.close(); } catch (JSchException jsche) { logger.error("Trying to SSH to host: {} but got exception {}", address, jsche); } finally { if (channel != null) channel.disconnect(); if (session != null) session.disconnect(); }
/** * Waits for termination. * * @param timeout CAUTION: <= 0 immediately times out */ public void waitFor(long timeout) throws JSchException, ExitCode, InterruptedException { long deadline; try { deadline = System.currentTimeMillis() + timeout; while (!channel.isClosed()) { if (System.currentTimeMillis() >= deadline) { throw new TimeoutException(this); } Thread.sleep(100); // throws InterruptedException } if (channel.getExitStatus() != 0) { throw new ExitCode(new Launcher(command), channel.getExitStatus()); } } finally { channel.disconnect(); } }
/** * Waits for termination. * * @param timeout CAUTION: <= 0 immediately times out */ public void waitFor(long timeout) throws JSchException, ExitCode, InterruptedException { long deadline; try { deadline = System.currentTimeMillis() + timeout; while (!channel.isClosed()) { if (System.currentTimeMillis() >= deadline) { throw new TimeoutException(this); } Thread.sleep(100); // throws InterruptedException } if (channel.getExitStatus() != 0) { throw new ExitCode(new Launcher(command), channel.getExitStatus()); } } finally { channel.disconnect(); } }