@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); } }
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();
return; ClientChannel channel = session.createChannel("exec", script); ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayOutputStream err = new ByteArrayOutputStream();
@Test public void testClientWithHeartBeat() throws Exception { SshClient client = SshClient.setUpDefaultClient(); client.getProperties().put(ClientFactoryManager.HEARTBEAT_INTERVAL, "500"); client.start(); ClientSession session = client.connect("localhost", port).await().getSession(); session.authPassword("smx", "smx").await().isSuccess(); ClientChannel channel = session.createChannel(ClientChannel.CHANNEL_SHELL); int state = channel.waitFor(ClientChannel.CLOSED, 2000); assertTrue((state & ClientChannel.CLOSED) == 0); channel.close(false); client.stop(); }
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));
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));