/** * Handles the SSH connection in case of timeout or exit. If the session ends with a timeout * condition, it throws an exception; if the channel reaches an end of file condition, but it * does not have an exit status, it returns true to break the loop; otherwise, it returns * false. */ protected static boolean canEndTheSshConnection(int waitResultTimeoutInMs, com.trilead.ssh2.Session sess, int conditions) throws SshException { if (isChannelConditionEof(conditions)) { int newConditions = sess.waitForCondition(ChannelCondition.EXIT_STATUS, waitResultTimeoutInMs); throwSshExceptionIfConditionsTimeout(newConditions); if ((newConditions & ChannelCondition.EXIT_STATUS) != 0) { return true; } } return false; }
int conditions = sshSession.waitForCondition(ChannelCondition.STDOUT_DATA | ChannelCondition.STDERR_DATA | ChannelCondition.EOF | ChannelCondition.EXIT_STATUS, 120000);
int conditions = sess.waitForCondition(ChannelCondition.STDOUT_DATA | ChannelCondition.STDERR_DATA | ChannelCondition.EOF | ChannelCondition.EXIT_STATUS, waitResultTimeoutInMs);
@Test public void canEndTheSshConnectionTest() throws Exception { PowerMockito.spy(SshHelper.class); Session mockedSession = Mockito.mock(Session.class); PowerMockito.doReturn(true).when(SshHelper.class, "isChannelConditionEof", Mockito.anyInt()); Mockito.when(mockedSession.waitForCondition(ChannelCondition.EXIT_STATUS, 1l)).thenReturn(0); PowerMockito.doNothing().when(SshHelper.class, "throwSshExceptionIfConditionsTimeout", Mockito.anyInt()); SshHelper.canEndTheSshConnection(1, mockedSession, 0); PowerMockito.verifyStatic(); SshHelper.isChannelConditionEof(Mockito.anyInt()); SshHelper.throwSshExceptionIfConditionsTimeout(Mockito.anyInt()); Mockito.verify(mockedSession).waitForCondition(ChannelCondition.EXIT_STATUS, 1l); }
public void waitForCondition(int code, long timeout) { mySession.waitForCondition(code, timeout); }
@Override public int waitFor() throws InterruptedException { ssh.waitForCondition(ChannelCondition.EXIT_STATUS,0); return ssh.getExitStatus(); }
@Override public int waitFor() throws InterruptedException { int r = s.waitForCondition(EXIT_STATUS, timeout * 1000); if ((r&EXIT_STATUS)!=0) return exitValue(); // not sure what exception jgit expects throw new InterruptedException("Timed out: "+r); }
/** * Try reading some output from stdout. * @param timeout timeout * @return String * @throws IOException in case of error */ protected String readOutput(int timeout) throws IOException { final int BUFF_SIZE = 8192; byte buffer[] = new byte[BUFF_SIZE]; int offset = 0; int conditions = session.waitForCondition(ChannelCondition.STDOUT_DATA | ChannelCondition.EOF, timeout); if (0 != (conditions & ChannelCondition.EOF)) { throw new EOFException("Connection to console lost."); } while (stdout.available() > 0 && offset < BUFF_SIZE) { int maxLen = BUFF_SIZE - offset; int result = stdout.read(buffer, offset, maxLen); if (result > 0) { offset += result; } } return new String(buffer, 0, offset); } }
protected byte[] getOutput(int timeout) throws EOFException, IOException { int conditions = ChannelCondition.EOF; final int BUFF_SIZE = 8192; byte buffer[] = new byte[BUFF_SIZE]; int offset = 0; do { conditions = session.waitForCondition(ChannelCondition.STDOUT_DATA | ChannelCondition.EOF, timeout); while (stdout.available() > 0) { int maxLen = BUFF_SIZE - offset; int result = stdout.read(buffer, offset, maxLen); // if we read chars then increase offset if (result > 0) { offset += result; } } } while ((conditions & (ChannelCondition.TIMEOUT | ChannelCondition.EOF)) == 0); // keep // looping // till // timeout // or // EOF if (0 != (conditions & ChannelCondition.EOF)) { logger.error("EOF (response if any): " + (new String(buffer))); throw new EOFException("Connection to console lost."); } return buffer; }
public int join() throws IOException, InterruptedException { try { t1.join(); t2.join(); t3.join(); session.waitForCondition(ChannelCondition.EXIT_STATUS,0); Integer r = session.getExitStatus(); if(r!=null) return r; return -1; } finally { session.close(); } }
public int join() throws IOException, InterruptedException { try { t1.join(); t2.join(); t3.join(); session.waitForCondition(ChannelCondition.EXIT_STATUS,0); Integer r = session.getExitStatus(); if(r!=null) return r; return -1; } finally { session.close(); } }
public String execute(String command) { out.println(command); StringBuilder sb = new StringBuilder(); try { char[] buffer = new char[2048]; int stdOutConditions = session.waitForCondition( ChannelCondition.STDOUT_DATA | ChannelCondition.STDERR_DATA | ChannelCondition.EXIT_STATUS | ChannelCondition.EOF |ChannelCondition.EXIT_SIGNAL |ChannelCondition.TIMEOUT, 3000); if ((stdOutConditions & ChannelCondition.STDOUT_DATA) != 0) { int len = stdoutReader.read(buffer); if (len > 0) { sb.append(buffer, 0, len); } }else if ((stdOutConditions & ChannelCondition.STDERR_DATA) != 0) { int errLen = stderrReader.read(buffer); if (errLen > 0) { sb.append(buffer, 0, errLen); } } String response = sb.toString(); if (logger.isInfoEnabled()) { logger.info("\n" + response); } return response; } catch (Exception e) { throw new RuntimeException("execute comand [" + command + "] error !", e); } }
/** * Find the exit code or exit status, which are differentiated in SSH protocol. */ private String getSessionOutcomeMessage(Session session, boolean isConnectionLost) throws InterruptedException { session.waitForCondition(ChannelCondition.EXIT_STATUS | ChannelCondition.EXIT_SIGNAL, 3000); Integer exitCode = session.getExitStatus(); if (exitCode != null) return "Slave JVM has terminated. Exit code=" + exitCode; String sig = session.getExitSignal(); if (sig != null) return "Slave JVM has terminated. Exit signal=" + sig; if (isConnectionLost) return "Slave JVM has not reported exit code before the socket was lost"; return "Slave JVM has not reported exit code. Is it still running?"; }
/** * Find the exit code or exit status, which are differentiated in SSH protocol. */ private String getSessionOutcomeMessage(Session session, boolean isConnectionLost) throws InterruptedException { session.waitForCondition(ChannelCondition.EXIT_STATUS | ChannelCondition.EXIT_SIGNAL, 3000); Integer exitCode = session.getExitStatus(); if (exitCode != null) return "Slave JVM has terminated. Exit code=" + exitCode; String sig = session.getExitSignal(); if (sig != null) return "Slave JVM has terminated. Exit signal=" + sig; if (isConnectionLost) return "Slave JVM has not reported exit code before the socket was lost"; return "Slave JVM has not reported exit code. Is it still running?"; }
private static void putDir(Connection conn, String localDirectory, String remoteTargetDirectory, String mode) throws IOException { final String[] fileList = curDir.list(); for (String file : fileList) { final String fullFileName = localDirectory + "/" + file; if (new File(fullFileName).isDirectory()) { final String subDir = remoteTargetDirectory + "/" + file; Session sess = conn.openSession(); sess.execCommand("mkdir " + subDir); sess.waitForCondition(ChannelCondition.EOF, 0); putDir(conn, fullFileName, subDir, mode); } else { SCPClient scpc = conn.createSCPClient(); scpc.put(fullFileName, remoteTargetDirectory, mode); } } }
/** * Handles the SSH connection in case of timeout or exit. If the session ends with a timeout * condition, it throws an exception; if the channel reaches an end of file condition, but it * does not have an exit status, it returns true to break the loop; otherwise, it returns * false. */ protected static boolean canEndTheSshConnection(final int waitResultTimeoutInMs, final com.trilead.ssh2.Session sess, final int conditions) throws SshException { if (isChannelConditionEof(conditions)) { final int newConditions = sess.waitForCondition(ChannelCondition.EXIT_STATUS, waitResultTimeoutInMs); throwSshExceptionIfConditionsTimeout(newConditions); if ((newConditions & ChannelCondition.EXIT_STATUS) != 0) { return true; } } return false; }
public boolean closeSession(Session session) { lock(Thread.currentThread()); try { if (session == null) { return false; } try { session.close(); session.waitForCondition(ChannelCondition.CLOSED, 0); } finally { mySessionCount--; SVNDebugLog.getDefaultLog().logFine(SVNLogType.NETWORK, ourRequestor + ": SESSION CLOSED: " + this + "." + mySessionCount); } if (mySessionCount <= 0) { mySessionCount = 0; } return mySessionCount <= 0; } finally { unlock(); } }
@Test public void canEndTheSshConnectionTest() throws Exception { PowerMockito.spy(SshHelper.class); final Session mockedSession = Mockito.mock(Session.class); PowerMockito.doReturn(true).when(SshHelper.class, "isChannelConditionEof", Mockito.anyInt()); Mockito.when(mockedSession.waitForCondition(ChannelCondition.EXIT_STATUS, 1l)).thenReturn(0); PowerMockito.doNothing().when(SshHelper.class, "throwSshExceptionIfConditionsTimeout", Mockito.anyInt()); SshHelper.canEndTheSshConnection(1, mockedSession, 0); PowerMockito.verifyStatic(); SshHelper.isChannelConditionEof(Mockito.anyInt()); SshHelper.throwSshExceptionIfConditionsTimeout(Mockito.anyInt()); Mockito.verify(mockedSession).waitForCondition(ChannelCondition.EXIT_STATUS, 1l); }
public boolean closeSession(Session session) throws SVNException { synchronized (SVNSSHSession.class) { if (session == null) { return false; } try { session.close(); session.waitForCondition(ChannelCondition.CLOSED, 0); dummy(); } catch (InterruptedException e) { SVNErrorMessage err = SVNErrorMessage.create(SVNErrorCode.RA_SVN_IO_ERROR, "SSH session failed to shutdown normally and interrupted" ); SVNErrorManager.error(err, e, SVNLogType.NETWORK); } finally { mySessionCount--; SVNDebugLog.getDefaultLog().logFine(SVNLogType.NETWORK, ourRequestor + ": SESSION CLOSED: " + this + "." + mySessionCount); } if (mySessionCount <= 0) { mySessionCount = 0; } return mySessionCount <= 0; } }
/** * Executes a process remotely and blocks until its completion. * * @param command the command * @param output The stdout/stderr will be sent to this stream. * @return the int * @throws IOException the io exception * @throws InterruptedException the interrupted exception */ public int exec(String command, OutputStream output) throws IOException, InterruptedException { Session session = openSession(); try { session.execCommand(command); PumpThread t1 = new PumpThread(session.getStdout(), output); t1.start(); PumpThread t2 = new PumpThread(session.getStderr(), output); t2.start(); session.getStdin().close(); t1.join(); t2.join(); // wait for some time since the delivery of the exit status often gets delayed session.waitForCondition(ChannelCondition.EXIT_STATUS,3000); Integer r = session.getExitStatus(); if(r!=null) return r.intValue(); return -1; } finally { session.close(); } }