protected ChannelExec openCommandChannel(ClientSession session, String cmd) throws IOException { long waitTimeout = PropertyResolverUtils.getLongProperty( session, SCP_EXEC_CHANNEL_OPEN_TIMEOUT, DEFAULT_EXEC_CHANNEL_OPEN_TIMEOUT); ChannelExec channel = session.createExecChannel(cmd); long startTime = System.nanoTime(); try { channel.open().verify(waitTimeout); long endTime = System.nanoTime(); long nanosWait = endTime - startTime; if (log.isTraceEnabled()) { log.trace( "openCommandChannel(" + session + ")[" + cmd + "]" + " completed after " + nanosWait + " nanos out of " + TimeUnit.MILLISECONDS.toNanos(waitTimeout)); } return channel; } catch (IOException | RuntimeException e) { long endTime = System.nanoTime(); long nanosWait = endTime - startTime; if (log.isTraceEnabled()) { log.trace( "openCommandChannel(" + session + ")[" + cmd + "]" + " failed (" + e.getClass().getSimpleName() + ")" + " to complete after " + nanosWait + " nanos out of " + TimeUnit.MILLISECONDS.toNanos(waitTimeout) + ": " + e.getMessage()); } channel.close(false); throw e; } }
private void execute(ClientSession session, String command) throws Exception { try (ChannelExec channel = session.createExecChannel(command)) { channel.setOut(System.out); channel.setErr(System.err); channel.open().verify(11L, TimeUnit.SECONDS); Collection<ClientChannelEvent> result = channel.waitFor(EnumSet.of(ClientChannelEvent.CLOSED), TimeUnit.MINUTES.toMillis(1L)); assertTrue( "Command '" + command + "'not completed on time: " + result, result.contains(ClientChannelEvent.CLOSED)); Integer status = channel.getExitStatus(); if (status != null) { assertEquals("Failed (" + status + ") " + command, 0, status.intValue()); } } }