@Override protected void executeInternal(String command) throws Exception { commandResult = null; // executing new command, reset previous result final ClientChannel channel = session.createChannel("exec", command.concat(NEW_LINE)); try ( InputStream in = new ByteArrayInputStream(new byte[0]); ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayOutputStream err = new ByteArrayOutputStream() ) { channel.setIn(in); channel.setOut(AnsiConsole.wrapOutputStream(out)); channel.setErr(AnsiConsole.wrapOutputStream(err)); channel.open(); channel.waitFor(ClientChannel.CLOSED, 0); out.writeTo(System.out); err.writeTo(System.err); commandResult = out.toString(); final boolean isError = (channel.getExitStatus() != null && channel.getExitStatus() != 0); if (isError) { log.error(commandResult); throw new IllegalArgumentException(String.format("Operation '%s' failed", command)); } } finally { channel.close(true); } }
/** * Constructor that creates creates the terminal and * connects the I/O streams to the server * @throws IOException */ public SessionTerminal() throws IOException { try { this.terminal = new Terminal(TERM_WIDTH, TERM_HEIGHT); in = new NoClosePipedOutputStream(); out = new NoClosePipedInputStream(); NoClosePipedOutputStream pipedOut = new NoClosePipedOutputStream(out); NoClosePipedInputStream pipedIn = new NoClosePipedInputStream(in); channel = session.createChannel(ClientChannel.CHANNEL_SHELL); channel.setIn(pipedIn); channel.setOut(pipedOut); channel.setErr(pipedOut); new Thread(this).start(); channel.open(); } catch (Exception e) { LoggerFactory.getLogger(getClass()).error(e.getMessage(), e); } }
((ChannelShell) channel).setupSensibleDefaultPty(); channel.setOut(AnsiConsole.wrapOutputStream(System.out)); channel.setErr(AnsiConsole.wrapOutputStream(System.err)); channel.open();
ByteArrayOutputStream err = new ByteArrayOutputStream(); channel.setIn(new ByteArrayInputStream(new byte[0])); channel.setOut(out); channel.setErr(err); channel.open();
((ChannelShell) channel).setAgentForwarding(true); channel.setOut(new NoCloseOutputStream(System.out)); channel.setErr(new NoCloseOutputStream(System.err)); channel.open();
((ChannelShell) channel).setAgentForwarding(true); channel.setOut(new NoCloseOutputStream(System.out)); channel.setErr(new NoCloseOutputStream(System.err)); channel.open();