@Test public void testRunShellCommandInvalidEncoding() throws Exception { SSHService sshService = new SSHServiceImpl(sessionMock, channelExecMock); exception.expect(RuntimeException.class); sshService.runShellCommand("", "test", true, CONNECT_TIMEOUT, COMMAND_TIMEOUT, AGENT_FORWARDING_TRUE); }
@Override public CommandResult runShellCommand( String command, String characterSet, boolean usePseudoTerminal, int connectTimeout, int commandTimeout, boolean agentForwarding) { try { if (!isConnected()) { session.connect(connectTimeout); } ChannelExec channel = (ChannelExec) session.openChannel(EXEC_CHANNEL); channel.setCommand(command.getBytes(characterSet)); channel.setPty(usePseudoTerminal); channel.setAgentForwarding(agentForwarding); OutputStream out = new ByteArrayOutputStream(); channel.setOutputStream(out); OutputStream err = new ByteArrayOutputStream(); channel.setErrStream(err); channel.connect(connectTimeout); long currentTime = System.currentTimeMillis(); long timeLimit = currentTime + commandTimeout; while (!channel.isClosed() && currentTime < timeLimit) { try { Thread.sleep(POLLING_INTERVAL); } catch (InterruptedException ignore) { } currentTime = System.currentTimeMillis(); } boolean timedOut = !channel.isClosed(); CommandResult result = new CommandResult(); result.setStandardOutput(((ByteArrayOutputStream) out).toString(characterSet)); if (usePseudoTerminal && channel.getExitStatus() != 0) { result.setStandardError(((ByteArrayOutputStream) out).toString(characterSet)); } else { result.setStandardError(((ByteArrayOutputStream) err).toString(characterSet)); } channel.disconnect(); result.setExitCode(channel.getExitStatus()); if (timedOut) { throw new TimeoutException(String.valueOf(result)); } return result; } catch (JSchException | UnsupportedEncodingException | TimeoutException e) { throw new RuntimeException(e); } }
SSHServiceImpl implements SSHService { @Override public CommandResult runShellCommand( String command, String characterSet, boolean usePseudoTerminal, int connectTimeout, int commandTimeout, boolean agentForwarding) { try { if (!isConnected()) { session.connect(connectTimeout); } ChannelExec channel = (ChannelExec) session.openChannel(EXEC_CHANNEL); channel.setCommand(command.getBytes(characterSet)); channel.setPty(usePseudoTerminal); channel.setAgentForwarding(agentForwarding); OutputStream out = new ByteArrayOutputStream(); channel.setOutputStream(out); OutputStream err = new ByteArrayOutputStream(); channel.setErrStream(err); channel.connect(connectTimeout); long currentTime = System.currentTimeMillis(); long timeLimit = currentTime + commandTimeout; while (!channel.isClosed() && currentTime < timeLimit) { try { Thread.sleep(POLLING_INTERVAL); } catch (InterruptedException ignore) { } currentTime = System.currentTimeMillis(); } boolean timedOut = !channel.isClosed(); CommandResult result = new CommandResult(); result.setStandardOutput(((ByteArrayOutputStream) out).toString(characterSet)); if (usePseudoTerminal && channel.getExitStatus() != 0) { result.setStandardError(((ByteArrayOutputStream) out).toString(characterSet)); } else { result.setStandardError(((ByteArrayOutputStream) err).toString(characterSet)); } channel.disconnect(); result.setExitCode(channel.getExitStatus()); if (timedOut) { throw new TimeoutException(String.valueOf(result)); } return result; } catch (JSchException | UnsupportedEncodingException | TimeoutException e) { throw new RuntimeException(e); } } }
SSHServiceImpl implements SSHService { @Override public CommandResult runShellCommand( String command, String characterSet, boolean usePseudoTerminal, int connectTimeout, int commandTimeout, boolean agentForwarding) { try { if (!isConnected()) { session.connect(connectTimeout); } ChannelExec channel = (ChannelExec) session.openChannel(EXEC_CHANNEL); channel.setCommand(command.getBytes(characterSet)); channel.setPty(usePseudoTerminal); channel.setAgentForwarding(agentForwarding); OutputStream out = new ByteArrayOutputStream(); channel.setOutputStream(out); OutputStream err = new ByteArrayOutputStream(); channel.setErrStream(err); channel.connect(connectTimeout); long currentTime = System.currentTimeMillis(); long timeLimit = currentTime + commandTimeout; while (!channel.isClosed() && currentTime < timeLimit) { try { Thread.sleep(POLLING_INTERVAL); } catch (InterruptedException ignore) { } currentTime = System.currentTimeMillis(); } boolean timedOut = !channel.isClosed(); CommandResult result = new CommandResult(); result.setStandardOutput(((ByteArrayOutputStream) out).toString(characterSet)); if (usePseudoTerminal && channel.getExitStatus() != 0) { result.setStandardError(((ByteArrayOutputStream) out).toString(characterSet)); } else { result.setStandardError(((ByteArrayOutputStream) err).toString(characterSet)); } channel.disconnect(); result.setExitCode(channel.getExitStatus()); if (timedOut) { throw new TimeoutException(String.valueOf(result)); } return result; } catch (JSchException | UnsupportedEncodingException | TimeoutException e) { throw new RuntimeException(e); } } SSHServiceImpl(Session session, Channel channel);  SSHServiceImpl(ConnectionDetails details, IdentityKey identityKey, KnownHostsFile knownHostsFile,
                          int connectTimeout, boolean keepContextForExpectCommand, ProxyHTTP proxyHTTP, String allowedCiphers); }
SSHServiceImpl implements SSHService { @Override public CommandResult runShellCommand( String command, String characterSet, boolean usePseudoTerminal, int connectTimeout, int commandTimeout, boolean agentForwarding) { try { if (!isConnected()) { session.connect(connectTimeout); } ChannelExec channel = (ChannelExec) session.openChannel(EXEC_CHANNEL); channel.setCommand(command.getBytes(characterSet)); channel.setPty(usePseudoTerminal); channel.setAgentForwarding(agentForwarding); OutputStream out = new ByteArrayOutputStream(); channel.setOutputStream(out); OutputStream err = new ByteArrayOutputStream(); channel.setErrStream(err); channel.connect(connectTimeout); long currentTime = System.currentTimeMillis(); long timeLimit = currentTime + commandTimeout; while (!channel.isClosed() && currentTime < timeLimit) { try { Thread.sleep(POLLING_INTERVAL); } catch (InterruptedException ignore) { } currentTime = System.currentTimeMillis(); } boolean timedOut = !channel.isClosed(); CommandResult result = new CommandResult(); result.setStandardOutput(((ByteArrayOutputStream) out).toString(characterSet)); if (usePseudoTerminal && channel.getExitStatus() != 0) { result.setStandardError(((ByteArrayOutputStream) out).toString(characterSet)); } else { result.setStandardError(((ByteArrayOutputStream) err).toString(characterSet)); } channel.disconnect(); result.setExitCode(channel.getExitStatus()); if (timedOut) { throw new TimeoutException(String.valueOf(result)); } return result; } catch (JSchException | UnsupportedEncodingException | TimeoutException e) { throw new RuntimeException(e); } } SSHServiceImpl(Session session, Channel channel);  SSHServiceImpl(ConnectionDetails details, IdentityKey identityKey, KnownHostsFile knownHostsFile,
                          int connectTimeout, boolean keepContextForExpectCommand, ProxyHTTP proxyHTTP, String allowedCiphers); @Override CommandResult runShell(
            final String command,
            final String characterSet,
            boolean usePseudoTerminal,
            int connectTimeout,
            int commandTimeout,
            boolean agentForwarding); @Override CommandResult runShellCommand(
            String command,
            String characterSet,
            boolean usePseudoTerminal,
            int connectTimeout,
            int commandTimeout,
            boolean agentForwarding); @Override void createLocalTunnel(int localPort, String remoteHost, int remotePort); @Override boolean isConnected(); @Override void close(); @Override boolean saveToCache(GlobalSessionObject<Map<String, SSHConnection>> sessionParam, String sessionId); @Override void removeFromCache(GlobalSessionObject<Map<String, SSHConnection>> sessionParam, String sessionId); @Override Session getSSHSession(); @Override Channel getExecChannel(); }
SSHServiceImpl implements SSHService { @Override public CommandResult runShellCommand( String command, String characterSet, boolean usePseudoTerminal, int connectTimeout, int commandTimeout, boolean agentForwarding) { try { if (!isConnected()) { session.connect(connectTimeout); } ChannelExec channel = (ChannelExec) session.openChannel(EXEC_CHANNEL); channel.setCommand(command.getBytes(characterSet)); channel.setPty(usePseudoTerminal); channel.setAgentForwarding(agentForwarding); OutputStream out = new ByteArrayOutputStream(); channel.setOutputStream(out); OutputStream err = new ByteArrayOutputStream(); channel.setErrStream(err); channel.connect(connectTimeout); long currentTime = System.currentTimeMillis(); long timeLimit = currentTime + commandTimeout; while (!channel.isClosed() && currentTime < timeLimit) { try { Thread.sleep(POLLING_INTERVAL); } catch (InterruptedException ignore) { } currentTime = System.currentTimeMillis(); } boolean timedOut = !channel.isClosed(); CommandResult result = new CommandResult(); result.setStandardOutput(((ByteArrayOutputStream) out).toString(characterSet)); if (usePseudoTerminal && channel.getExitStatus() != 0) { result.setStandardError(((ByteArrayOutputStream) out).toString(characterSet)); } else { result.setStandardError(((ByteArrayOutputStream) err).toString(characterSet)); } channel.disconnect(); result.setExitCode(channel.getExitStatus()); if (timedOut) { throw new TimeoutException(String.valueOf(result)); } return result; } catch (JSchException | UnsupportedEncodingException | TimeoutException e) { throw new RuntimeException(e); } } SSHServiceImpl(Session session, Channel channel);  SSHServiceImpl(ConnectionDetails details, IdentityKey identityKey, KnownHostsFile knownHostsFile,
                          int connectTimeout, boolean keepContextForExpectCommand, ProxyHTTP proxyHTTP, String allowedCiphers); @Override CommandResult runShell(
            final String command,
            final String characterSet,
            boolean usePseudoTerminal,
            int connectTimeout,
            int commandTimeout,
            boolean agentForwarding); @Override CommandResult runShellCommand(
            String command,
            String characterSet,
            boolean usePseudoTerminal,
            int connectTimeout,
            int commandTimeout,
            boolean agentForwarding); @Override void createLocalTunnel(int localPort, String remoteHost, int remotePort); @Override boolean isConnected(); @Override void close(); @Override boolean saveToCache(GlobalSessionObject<Map<String, SSHConnection>> sessionParam, String sessionId); @Override void removeFromCache(GlobalSessionObject<Map<String, SSHConnection>> sessionParam, String sessionId); @Override Session getSSHSession(); @Override Channel getExecChannel(); static final String EXIT_COMMAND; }