@Test(expected = JschServiceException.class) public void testRunCommandUsingChannelShellAndTimesOut() throws Exception { when(mockChannelShell.getInputStream()).thenReturn(new ByteArrayInputStream("output without eof".getBytes())); when(mockChannelShell.getOutputStream()).thenReturn(mockOut); when(mockChannelShell.isConnected()).thenReturn(true); when(Config.getMockPool().borrowObject(any(ChannelSessionKey.class))).thenReturn(mockChannelShell); final RemoteCommandReturnInfo result = jschService.runShellCommand(mockRemoteSystemConnection, "scp", 500); verify(mockOut, times(6)).write(any(byte[].class)); verify(mockOut).flush(); assertEquals("", result.standardOuput); }
@Override public RemoteCommandReturnInfo runShellCommand(RemoteSystemConnection remoteSystemConnection, String command, long timeout) { final ChannelSessionKey channelSessionKey = new ChannelSessionKey(remoteSystemConnection, ChannelType.SHELL); LOGGER.debug("channel session key = {}", channelSessionKey); Channel channel = null; try { channel = getChannelShell(channelSessionKey); final InputStream in = channel.getInputStream(); final OutputStream out = channel.getOutputStream(); LOGGER.debug("Executing command \"{}\"", command); out.write(command.getBytes(StandardCharsets.UTF_8)); out.write(CRLF.getBytes(StandardCharsets.UTF_8)); out.write("echo 'EXIT_CODE='$?***".getBytes(StandardCharsets.UTF_8)); out.write(CRLF.getBytes(StandardCharsets.UTF_8)); out.write("echo -n -e '\\xff'".getBytes(StandardCharsets.UTF_8)); out.write(CRLF.getBytes(StandardCharsets.UTF_8)); out.flush(); return getShellRemoteCommandReturnInfo(command, in, timeout); } catch (final Exception e) { final String errMsg = MessageFormat.format("Failed to run the following command: {0}", command); LOGGER.error(errMsg, e); throw new JschServiceException(errMsg, e); } finally { if (channel != null) { channelPool.returnObject(channelSessionKey, channel); LOGGER.debug("channel {} returned", channel.getId()); } } }
JschServiceImpl implements JschService { @Override public RemoteCommandReturnInfo runShellCommand(RemoteSystemConnection remoteSystemConnection, String command, long timeout) { final ChannelSessionKey channelSessionKey = new ChannelSessionKey(remoteSystemConnection, ChannelType.SHELL); LOGGER.debug("channel session key = {}", channelSessionKey); Channel channel = null; try { channel = getChannelShell(channelSessionKey); final InputStream in = channel.getInputStream(); final OutputStream out = channel.getOutputStream(); LOGGER.debug("Executing command \"{}\"", command); out.write(command.getBytes(StandardCharsets.UTF_8)); out.write(CRLF.getBytes(StandardCharsets.UTF_8)); out.write("echo 'EXIT_CODE='$?***".getBytes(StandardCharsets.UTF_8)); out.write(CRLF.getBytes(StandardCharsets.UTF_8)); out.write("echo -n -e '\\xff'".getBytes(StandardCharsets.UTF_8)); out.write(CRLF.getBytes(StandardCharsets.UTF_8)); out.flush(); return getShellRemoteCommandReturnInfo(command, in, timeout); } catch (final Exception e) { final String errMsg = MessageFormat.format("Failed to run the following command: {0}", command); LOGGER.error(errMsg, e); throw new JschServiceException(errMsg, e); } finally { if (channel != null) { channelPool.returnObject(channelSessionKey, channel); LOGGER.debug("channel {} returned", channel.getId()); } } } }
JschServiceImpl implements JschService { @Override public RemoteCommandReturnInfo runShellCommand(RemoteSystemConnection remoteSystemConnection, String command, long timeout) { final ChannelSessionKey channelSessionKey = new ChannelSessionKey(remoteSystemConnection, ChannelType.SHELL); LOGGER.debug("channel session key = {}", channelSessionKey); Channel channel = null; try { channel = getChannelShell(channelSessionKey); final InputStream in = channel.getInputStream(); final OutputStream out = channel.getOutputStream(); LOGGER.debug("Executing command \"{}\"", command); out.write(command.getBytes(StandardCharsets.UTF_8)); out.write(CRLF.getBytes(StandardCharsets.UTF_8)); out.write("echo 'EXIT_CODE='$?***".getBytes(StandardCharsets.UTF_8)); out.write(CRLF.getBytes(StandardCharsets.UTF_8)); out.write("echo -n -e '\\xff'".getBytes(StandardCharsets.UTF_8)); out.write(CRLF.getBytes(StandardCharsets.UTF_8)); out.flush(); return getShellRemoteCommandReturnInfo(command, in, timeout); } catch (final Exception e) { final String errMsg = MessageFormat.format("Failed to run the following command: {0}", command); LOGGER.error(errMsg, e); throw new JschServiceException(errMsg, e); } finally { if (channel != null) { channelPool.returnObject(channelSessionKey, channel); LOGGER.debug("channel {} returned", channel.getId()); } } }  }
JschServiceImpl implements JschService { @Override public RemoteCommandReturnInfo runShellCommand(RemoteSystemConnection remoteSystemConnection, String command, long timeout) { final ChannelSessionKey channelSessionKey = new ChannelSessionKey(remoteSystemConnection, ChannelType.SHELL); LOGGER.debug("channel session key = {}", channelSessionKey); Channel channel = null; try { channel = getChannelShell(channelSessionKey); final InputStream in = channel.getInputStream(); final OutputStream out = channel.getOutputStream(); LOGGER.debug("Executing command \"{}\"", command); out.write(command.getBytes(StandardCharsets.UTF_8)); out.write(CRLF.getBytes(StandardCharsets.UTF_8)); out.write("echo 'EXIT_CODE='$?***".getBytes(StandardCharsets.UTF_8)); out.write(CRLF.getBytes(StandardCharsets.UTF_8)); out.write("echo -n -e '\\xff'".getBytes(StandardCharsets.UTF_8)); out.write(CRLF.getBytes(StandardCharsets.UTF_8)); out.flush(); return getShellRemoteCommandReturnInfo(command, in, timeout); } catch (final Exception e) { final String errMsg = MessageFormat.format("Failed to run the following command: {0}", command); LOGGER.error(errMsg, e); throw new JschServiceException(errMsg, e); } finally { if (channel != null) { channelPool.returnObject(channelSessionKey, channel); LOGGER.debug("channel {} returned", channel.getId()); } } }  @Override RemoteCommandReturnInfo runShellCommand(RemoteSystemConnection remoteSystemConnection, String command, long timeout); @Override RemoteCommandReturnInfo runExecCommand(RemoteSystemConnection remoteSystemConnection, String command, long timeout); }
JschServiceImpl implements JschService { @Override public RemoteCommandReturnInfo runShellCommand(RemoteSystemConnection remoteSystemConnection, String command, long timeout) { final ChannelSessionKey channelSessionKey = new ChannelSessionKey(remoteSystemConnection, ChannelType.SHELL); LOGGER.debug("channel session key = {}", channelSessionKey); Channel channel = null; try { channel = getChannelShell(channelSessionKey); final InputStream in = channel.getInputStream(); final OutputStream out = channel.getOutputStream(); LOGGER.debug("Executing command \"{}\"", command); out.write(command.getBytes(StandardCharsets.UTF_8)); out.write(CRLF.getBytes(StandardCharsets.UTF_8)); out.write("echo 'EXIT_CODE='$?***".getBytes(StandardCharsets.UTF_8)); out.write(CRLF.getBytes(StandardCharsets.UTF_8)); out.write("echo -n -e '\\xff'".getBytes(StandardCharsets.UTF_8)); out.write(CRLF.getBytes(StandardCharsets.UTF_8)); out.flush(); return getShellRemoteCommandReturnInfo(command, in, timeout); } catch (final Exception e) { final String errMsg = MessageFormat.format("Failed to run the following command: {0}", command); LOGGER.error(errMsg, e); throw new JschServiceException(errMsg, e); } finally { if (channel != null) { channelPool.returnObject(channelSessionKey, channel); LOGGER.debug("channel {} returned", channel.getId()); } } }  @Override RemoteCommandReturnInfo runShellCommand(RemoteSystemConnection remoteSystemConnection, String command, long timeout); @Override RemoteCommandReturnInfo runExecCommand(RemoteSystemConnection remoteSystemConnection, String command, long timeout);  }