@Test public void testRunCommandUsingChannelExec() throws IOException, JSchException { when(mockChannelExec.getInputStream()).thenReturn(in); when(mockChannelExec.getExitStatus()).thenReturn(0); when(Config.mockJsch.getSession(anyString(), anyString(), anyInt())).thenReturn(mockSession); when(mockSession.openChannel(eq(ChannelType.EXEC.getChannelType()))).thenReturn(mockChannelExec); when(mockChannelExec.isClosed()).thenReturn(true); jschService.runExecCommand(mockRemoteSystemConnection, "scp", 0); verify(mockChannelExec).setCommand(any(byte[].class)); verify(mockChannelExec).connect(anyInt()); }
@Override public RemoteCommandReturnInfo runExecCommand(RemoteSystemConnection remoteSystemConnection, String command, long timeout) { Session session = null; ChannelExec channel = null; try { LOGGER.debug("preparing session..."); session = prepareSession(remoteSystemConnection); session.connect(); LOGGER.debug("session connected"); channel = (ChannelExec) session.openChannel(ChannelType.EXEC.getChannelType()); LOGGER.debug("Executing command \"{}\"", scrubberService.scrub(command)); channel.setCommand(command.getBytes(StandardCharsets.UTF_8)); LOGGER.debug("channel {} connecting...", channel.getId()); channel.connect(CHANNEL_CONNECT_TIMEOUT); LOGGER.debug("channel {} connected!", channel.getId()); return getExecRemoteCommandReturnInfo(channel, timeout); } catch (final Exception e) { if (e.getCause() instanceof ConnectException) { throw new JschServiceException( MessageFormat.format("SSH connection to {0}:{1} for user {2} failed. Check your host, port, user and password.", remoteSystemConnection.getHost(), remoteSystemConnection.getPort(), remoteSystemConnection.getUser()), 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 && channel.isConnected()) { channel.disconnect(); LOGGER.debug("Channel {} disconnected!", channel.getId()); } if (session != null && session.isConnected()) { session.disconnect(); LOGGER.debug("session disconnected"); } } }
JschServiceImpl implements JschService { @Override public RemoteCommandReturnInfo runExecCommand(RemoteSystemConnection remoteSystemConnection, String command, long timeout) { Session session = null; ChannelExec channel = null; try { LOGGER.debug("preparing session..."); session = prepareSession(remoteSystemConnection); session.connect(); LOGGER.debug("session connected"); channel = (ChannelExec) session.openChannel(ChannelType.EXEC.getChannelType()); LOGGER.debug("Executing command \"{}\"", scrubberService.scrub(command)); channel.setCommand(command.getBytes(StandardCharsets.UTF_8)); LOGGER.debug("channel {} connecting...", channel.getId()); channel.connect(CHANNEL_CONNECT_TIMEOUT); LOGGER.debug("channel {} connected!", channel.getId()); return getExecRemoteCommandReturnInfo(channel, timeout); } catch (final Exception e) { if (e.getCause() instanceof ConnectException) { throw new JschServiceException( MessageFormat.format("SSH connection to {0}:{1} for user {2} failed. Check your host, port, user and password.", remoteSystemConnection.getHost(), remoteSystemConnection.getPort(), remoteSystemConnection.getUser()), 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 && channel.isConnected()) { channel.disconnect(); LOGGER.debug("Channel {} disconnected!", channel.getId()); } if (session != null && session.isConnected()) { session.disconnect(); LOGGER.debug("session disconnected"); } } } }
JschServiceImpl implements JschService { @Override public RemoteCommandReturnInfo runExecCommand(RemoteSystemConnection remoteSystemConnection, String command, long timeout) { Session session = null; ChannelExec channel = null; try { LOGGER.debug("preparing session..."); session = prepareSession(remoteSystemConnection); session.connect(); LOGGER.debug("session connected"); channel = (ChannelExec) session.openChannel(ChannelType.EXEC.getChannelType()); LOGGER.debug("Executing command \"{}\"", scrubberService.scrub(command)); channel.setCommand(command.getBytes(StandardCharsets.UTF_8)); LOGGER.debug("channel {} connecting...", channel.getId()); channel.connect(CHANNEL_CONNECT_TIMEOUT); LOGGER.debug("channel {} connected!", channel.getId()); return getExecRemoteCommandReturnInfo(channel, timeout); } catch (final Exception e) { if (e.getCause() instanceof ConnectException) { throw new JschServiceException( MessageFormat.format("SSH connection to {0}:{1} for user {2} failed. Check your host, port, user and password.", remoteSystemConnection.getHost(), remoteSystemConnection.getPort(), remoteSystemConnection.getUser()), 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 && channel.isConnected()) { channel.disconnect(); LOGGER.debug("Channel {} disconnected!", channel.getId()); } if (session != null && session.isConnected()) { session.disconnect(); LOGGER.debug("session disconnected"); } } }  }
JschServiceImpl implements JschService { @Override public RemoteCommandReturnInfo runExecCommand(RemoteSystemConnection remoteSystemConnection, String command, long timeout) { Session session = null; ChannelExec channel = null; try { LOGGER.debug("preparing session..."); session = prepareSession(remoteSystemConnection); session.connect(); LOGGER.debug("session connected"); channel = (ChannelExec) session.openChannel(ChannelType.EXEC.getChannelType()); LOGGER.debug("Executing command \"{}\"", scrubberService.scrub(command)); channel.setCommand(command.getBytes(StandardCharsets.UTF_8)); LOGGER.debug("channel {} connecting...", channel.getId()); channel.connect(CHANNEL_CONNECT_TIMEOUT); LOGGER.debug("channel {} connected!", channel.getId()); return getExecRemoteCommandReturnInfo(channel, timeout); } catch (final Exception e) { if (e.getCause() instanceof ConnectException) { throw new JschServiceException( MessageFormat.format("SSH connection to {0}:{1} for user {2} failed. Check your host, port, user and password.", remoteSystemConnection.getHost(), remoteSystemConnection.getPort(), remoteSystemConnection.getUser()), 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 && channel.isConnected()) { channel.disconnect(); LOGGER.debug("Channel {} disconnected!", channel.getId()); } if (session != null && session.isConnected()) { session.disconnect(); LOGGER.debug("session disconnected"); } } }  @Override RemoteCommandReturnInfo runShellCommand(RemoteSystemConnection remoteSystemConnection, String command, long timeout); @Override RemoteCommandReturnInfo runExecCommand(RemoteSystemConnection remoteSystemConnection, String command, long timeout); }
JschServiceImpl implements JschService { @Override public RemoteCommandReturnInfo runExecCommand(RemoteSystemConnection remoteSystemConnection, String command, long timeout) { Session session = null; ChannelExec channel = null; try { LOGGER.debug("preparing session..."); session = prepareSession(remoteSystemConnection); session.connect(); LOGGER.debug("session connected"); channel = (ChannelExec) session.openChannel(ChannelType.EXEC.getChannelType()); LOGGER.debug("Executing command \"{}\"", scrubberService.scrub(command)); channel.setCommand(command.getBytes(StandardCharsets.UTF_8)); LOGGER.debug("channel {} connecting...", channel.getId()); channel.connect(CHANNEL_CONNECT_TIMEOUT); LOGGER.debug("channel {} connected!", channel.getId()); return getExecRemoteCommandReturnInfo(channel, timeout); } catch (final Exception e) { if (e.getCause() instanceof ConnectException) { throw new JschServiceException( MessageFormat.format("SSH connection to {0}:{1} for user {2} failed. Check your host, port, user and password.", remoteSystemConnection.getHost(), remoteSystemConnection.getPort(), remoteSystemConnection.getUser()), 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 && channel.isConnected()) { channel.disconnect(); LOGGER.debug("Channel {} disconnected!", channel.getId()); } if (session != null && session.isConnected()) { session.disconnect(); LOGGER.debug("session disconnected"); } } }  @Override RemoteCommandReturnInfo runShellCommand(RemoteSystemConnection remoteSystemConnection, String command, long timeout); @Override RemoteCommandReturnInfo runExecCommand(RemoteSystemConnection remoteSystemConnection, String command, long timeout);  }