/** * 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); } }
@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); } }
private synchronized void handleSshChanelOpened(final ChannelHandlerContext ctx) { LOG.trace("SSH subsystem channel opened successfully on channel: {}", ctx.channel()); connectPromise.setSuccess(); // TODO we should also read from error stream and at least log from that sshReadAsyncListener = new AsyncSshHandlerReader(new AutoCloseable() { @Override public void close() throws Exception { AsyncSshHandler.this.disconnect(ctx, ctx.newPromise()); } }, new AsyncSshHandlerReader.ReadMsgHandler() { @Override public void onMessageRead(final ByteBuf msg) { ctx.fireChannelRead(msg); } }, channel.toString(), channel.getAsyncOut()); // if readAsyncListener receives immediate close, it will close this handler and closing this handler sets channel variable to null if(channel != null) { sshWriteAsyncHandler = new AsyncSshHandlerWriter(channel.getAsyncIn()); ctx.fireChannelActive(); } }
private synchronized void handleSshAuthenticated(final ClientSession session, final ChannelHandlerContext ctx) { try { LOG.debug("SSH session authenticated on channel: {}, server version: {}", ctx.channel(), session.getServerVersion()); channel = session.createSubsystemChannel(SUBSYSTEM); channel.setStreaming(ClientChannel.Streaming.Async); channel.open().addListener(new SshFutureListener<OpenFuture>() { @Override public void operationComplete(final OpenFuture future) { if(future.isOpened()) { handleSshChanelOpened(ctx); } else { handleSshSetupFailure(ctx, future.getException()); } } }); } catch (final IOException e) { handleSshSetupFailure(ctx, e); } }
private synchronized void handleSshAuthenticated(final ClientSession session, final ChannelHandlerContext ctx) { try { LOG.debug("SSH session authenticated on channel: {}, server version: {}", ctx.channel(), session.getServerVersion()); channel = session.createSubsystemChannel(SUBSYSTEM); channel.setStreaming(ClientChannel.Streaming.Async); channel.open().addListener(new SshFutureListener<OpenFuture>() { @Override public void operationComplete(final OpenFuture future) { if(future.isOpened()) { handleSshChanelOpened(ctx); } else { handleSshSetupFailure(ctx, future.getException()); } } }); } catch (final IOException e) { handleSshSetupFailure(ctx, e); } }
if (sb.length() > 0) { channel = session.createChannel("exec"); channel.setIn(new ByteArrayInputStream(sb.append("\n").toString().getBytes())); } else { terminal = new TerminalFactory().getTerminal(); channel = session.createChannel("shell"); channel.setIn(System.in); ((ChannelShell) channel).setupSensibleDefaultPty(); channel.setOut(AnsiConsole.wrapOutputStream(System.out)); channel.setErr(AnsiConsole.wrapOutputStream(System.err)); channel.open(); channel.waitFor(ClientChannel.CLOSED, 0); } catch (Throwable t) { if (level > 1) {
private synchronized void handleSshChanelOpened(final ChannelHandlerContext ctx) { LOG.trace("SSH subsystem channel opened successfully on channel: {}", ctx.channel()); if(negotiationFuture == null) { connectPromise.setSuccess(); } // TODO we should also read from error stream and at least log from that sshReadAsyncListener = new AsyncSshHandlerReader(new AutoCloseable() { @Override public void close() throws Exception { AsyncSshHandler.this.disconnect(ctx, ctx.newPromise()); } }, new AsyncSshHandlerReader.ReadMsgHandler() { @Override public void onMessageRead(final ByteBuf msg) { ctx.fireChannelRead(msg); } }, channel.toString(), channel.getAsyncOut()); // if readAsyncListener receives immediate close, it will close this handler and closing this handler sets channel variable to null if(channel != null) { sshWriteAsyncHandler = new AsyncSshHandlerWriter(channel.getAsyncIn()); ctx.fireChannelActive(); } }
ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayOutputStream err = new ByteArrayOutputStream(); channel.setIn(new ByteArrayInputStream(new byte[0])); channel.setOut(out); channel.setErr(err); channel.open(); channel.waitFor(ClientChannel.CLOSED, 0); System.out.println(out.toString()); System.err.println(err.toString());
if (cmd != null && cmd.length() > 0) { channel = sshSession.createChannel("exec", cmd); channel.setIn(new ByteArrayInputStream(new byte[0])); } else { channel = sshSession.createChannel("shell"); channel.setIn(new NoCloseInputStream(System.in)); ((ChannelShell) channel).setPtyColumns(ShellUtils.getTermWidth(session)); ((ChannelShell) channel).setupSensibleDefaultPty(); ((ChannelShell) channel).setAgentForwarding(true); channel.setOut(new NoCloseOutputStream(System.out)); channel.setErr(new NoCloseOutputStream(System.err)); channel.open(); channel.waitFor(ClientChannel.CLOSED, 0); } finally { session.put(Console.IGNORE_INTERRUPTS, oldIgnoreInterrupts);
if (cmd != null && cmd.length() > 0) { channel = sshSession.createChannel("exec", cmd); channel.setIn(new ByteArrayInputStream(new byte[0])); } else { channel = sshSession.createChannel("shell"); channel.setIn(new NoCloseInputStream(System.in)); ((ChannelShell) channel).setPtyColumns(ShellUtils.getTermWidth(session)); ((ChannelShell) channel).setupSensibleDefaultPty(); ((ChannelShell) channel).setAgentForwarding(true); channel.setOut(new NoCloseOutputStream(System.out)); channel.setErr(new NoCloseOutputStream(System.err)); channel.open(); channel.waitFor(ClientChannel.CLOSED, 0); } finally { session.put(Console.IGNORE_INTERRUPTS, oldIgnoreInterrupts);