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); } }
@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 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); } }
/** * 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); } }
channel.open(); channel.waitFor(ClientChannel.CLOSED, 0); } catch (Throwable t) {
channel.setOut(out); channel.setErr(err); channel.open(); channel.waitFor(ClientChannel.CLOSED, 0); System.out.println(out.toString());
channel.open(); channel.waitFor(ClientChannel.CLOSED, 0); } finally {
channel.open(); channel.waitFor(ClientChannel.CLOSED, 0); } finally {