channel.setIn(new NoCloseInputStream(System.in)); channel.setOut(new NoCloseOutputStream(System.out)); channel.setErr(new NoCloseOutputStream(System.err)); WaitableFuture wf = channel.open(); wf.await(); Set<ClientChannelEvent> waitMask = channel.waitFor(Collections.singletonList(ClientChannelEvent.CLOSED), 0L); Integer exitStatus = channel.getExitStatus(); return exitStatus;
ByteArrayOutputStream err = new ByteArrayOutputStream(); ClientChannel channel = session.createChannel(Channel.CHANNEL_SHELL)) { channel.setOut(out); channel.setErr(err); channel.open().verify(9L, TimeUnit.SECONDS); try (OutputStream pipedIn = channel.getInvertedIn()) { msg += "\nexit\n"; pipedIn.write(msg.getBytes(StandardCharsets.UTF_8)); channel.waitFor(EnumSet.of(ClientChannelEvent.CLOSED), TimeUnit.SECONDS.toMillis(15L)); assertFalse("Timeout while waiting for channel closure", result.contains(ClientChannelEvent.TIMEOUT)); } finally { channel.close(false);
OutputStream channelOut = (stdout == null) ? new NullOutputStream() : new NoCloseOutputStream(stdout); ClientChannel channel = createExecChannel(command)) { channel.setOut(channelOut); channel.setErr(channelErr); channel.open().await(); // TODO use verify and a configurable timeout Collection<ClientChannelEvent> waitMask = channel.waitFor(REMOTE_COMMAND_WAIT_EVENTS, 0L); if (waitMask.contains(ClientChannelEvent.TIMEOUT)) { throw new SocketTimeoutException("Failed to retrieve command result in time: " + command); Integer exitStatus = channel.getExitStatus(); ClientChannel.validateCommandExitStatusCode(command, exitStatus);
@Test public void testIdleClient() throws Exception { SshClient client = setupTestClient(); client.start(); try (ClientSession session = client.connect(getCurrentTestName(), TEST_LOCALHOST, port).verify(7L, TimeUnit.SECONDS).getSession()) { session.addPasswordIdentity(getCurrentTestName()); session.auth().verify(5L, TimeUnit.SECONDS); try (ClientChannel channel = session.createChannel(Channel.CHANNEL_SHELL)) { Collection<ClientChannelEvent> result = channel.waitFor(EnumSet.of(ClientChannelEvent.CLOSED), WAIT); assertTrue("Wrong channel state: " + result, result.containsAll(EnumSet.of(ClientChannelEvent.CLOSED))); } } finally { client.stop(); } }
long timeout = channel.getLongProperty(SCP_EXEC_CHANNEL_EXIT_STATUS_TIMEOUT, DEFAULT_EXEC_CHANNEL_EXIT_STATUS_TIMEOUT); if (timeout <= 0L) { handleCommandExitStatus(cmd, (Integer) null); Collection<ClientChannelEvent> events = channel.waitFor(COMMAND_WAIT_EVENTS, timeout); long waitEnd = System.nanoTime(); if (log.isDebugEnabled()) { handleCommandExitStatus(cmd, channel.getExitStatus());
@Override public void write(int b) throws IOException { if (channel.isClosed() || channel.isClosing()) { throw new IOException("Channel (" + channel + ") is closing / closed on write single byte"); } if (b == (eofSignal & 0xff)) { log.info("Closing channel (" + channel + ") due to single byte EOF"); channel.close(true); } }
@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(); }
@Test public void testSwitchToNoneCipher() throws Exception { setUp(0L, 0L, 0L); sshd.getCipherFactories().add(BuiltinCiphers.none); try (SshClient client = setupTestClient()) { client.getCipherFactories().add(BuiltinCiphers.none); client.start(); try (ClientSession session = client.connect(getCurrentTestName(), TEST_LOCALHOST, port).verify(7L, TimeUnit.SECONDS).getSession()) { session.addPasswordIdentity(getCurrentTestName()); session.auth().verify(5L, TimeUnit.SECONDS); outputDebugMessage("Request switch to none cipher for %s", session); KeyExchangeFuture switchFuture = session.switchToNoneCipher(); switchFuture.verify(5L, TimeUnit.SECONDS); try (ClientChannel channel = session.createSubsystemChannel(SftpConstants.SFTP_SUBSYSTEM_NAME)) { channel.open().verify(5L, TimeUnit.SECONDS); } } finally { client.stop(); } } }
ByteArrayOutputStream err = new ByteArrayOutputStream(); ClientChannel channel = session.createChannel(Channel.CHANNEL_SHELL)) { channel.setOut(out); channel.setErr(err); channel.open().verify(9L, TimeUnit.SECONDS); try (OutputStream pipedIn = channel.getInvertedIn()) { msg += "\nexit\n"; pipedIn.write(msg.getBytes(StandardCharsets.UTF_8)); channel.waitFor(EnumSet.of(ClientChannelEvent.CLOSED), TimeUnit.SECONDS.toMillis(15L)); assertFalse("Timeout while waiting for channel closure", result.contains(ClientChannelEvent.TIMEOUT)); } finally { channel.close(false);
OutputStream channelOut = (stdout == null) ? new NullOutputStream() : new NoCloseOutputStream(stdout); ClientChannel channel = createExecChannel(command)) { channel.setOut(channelOut); channel.setErr(channelErr); channel.open().await(); // TODO use verify and a configurable timeout Collection<ClientChannelEvent> waitMask = channel.waitFor(REMOTE_COMMAND_WAIT_EVENTS, 0L); if (waitMask.contains(ClientChannelEvent.TIMEOUT)) { throw new SocketTimeoutException("Failed to retrieve command result in time: " + command); Integer exitStatus = channel.getExitStatus(); ClientChannel.validateCommandExitStatusCode(command, exitStatus);
@Test public void testIdleClient() throws Exception { SshClient client = setupTestClient(); client.start(); try (ClientSession session = client.connect(getCurrentTestName(), TEST_LOCALHOST, port).verify(7L, TimeUnit.SECONDS).getSession()) { session.addPasswordIdentity(getCurrentTestName()); session.auth().verify(5L, TimeUnit.SECONDS); try (ClientChannel channel = session.createChannel(Channel.CHANNEL_SHELL)) { Collection<ClientChannelEvent> result = channel.waitFor(EnumSet.of(ClientChannelEvent.CLOSED), WAIT); assertTrue("Wrong channel state: " + result, result.containsAll(EnumSet.of(ClientChannelEvent.CLOSED))); } } finally { client.stop(); } }
@Override public void write(int b) throws IOException { if (channel.isClosed() || channel.isClosing()) { throw new IOException("Channel (" + channel + ") is closing / closed on write single byte"); } if (b == (eofSignal & 0xff)) { log.info("Closing channel (" + channel + ") due to single byte EOF"); channel.close(true); } }
@Test public void testSwitchToNoneCipher() throws Exception { setUp(0L, 0L, 0L); sshd.getCipherFactories().add(BuiltinCiphers.none); try (SshClient client = setupTestClient()) { client.getCipherFactories().add(BuiltinCiphers.none); client.start(); try (ClientSession session = client.connect(getCurrentTestName(), TEST_LOCALHOST, port).verify(7L, TimeUnit.SECONDS).getSession()) { session.addPasswordIdentity(getCurrentTestName()); session.auth().verify(5L, TimeUnit.SECONDS); outputDebugMessage("Request switch to none cipher for %s", session); KeyExchangeFuture switchFuture = session.switchToNoneCipher(); switchFuture.verify(5L, TimeUnit.SECONDS); try (ClientChannel channel = session.createSubsystemChannel(SftpConstants.SFTP_SUBSYSTEM_NAME)) { channel.open().verify(5L, TimeUnit.SECONDS); } } finally { client.stop(); } } }
channel.setIn(new ByteArrayInputStream(new byte[0])); final ByteArrayOutputStream sout = new ByteArrayOutputStream(); final ByteArrayOutputStream serr = new ByteArrayOutputStream(); channel.setOut( AnsiConsole.wrapOutputStream(sout)); channel.setErr( AnsiConsole.wrapOutputStream(serr)); channel.open(); channel.waitFor(EnumSet.of(ClientChannelEvent.CLOSED), 0); final boolean isError = (channel.getExitStatus() != null && channel.getExitStatus().intValue() != 0); if (isError) { final String errorMarker = Ansi.ansi().fg(Color.RED).toString();
ByteArrayOutputStream err = new ByteArrayOutputStream(); ClientChannel channel = session.createChannel(Channel.CHANNEL_SHELL)) { channel.setOut(out); channel.setErr(err); channel.open().verify(9L, TimeUnit.SECONDS); try (OutputStream pipedIn = channel.getInvertedIn()) { msg += "\nexit\n"; pipedIn.write(msg.getBytes(StandardCharsets.UTF_8)); channel.waitFor(EnumSet.of(ClientChannelEvent.CLOSED), TimeUnit.SECONDS.toMillis(15L)); assertFalse("Timeout while waiting for channel closure", result.contains(ClientChannelEvent.TIMEOUT)); } finally { channel.close(false);
@Test public void testIdleClient() throws Exception { SshClient client = setupTestClient(); client.start(); try (ClientSession session = client.connect(getCurrentTestName(), TEST_LOCALHOST, port).verify(7L, TimeUnit.SECONDS).getSession()) { session.addPasswordIdentity(getCurrentTestName()); session.auth().verify(5L, TimeUnit.SECONDS); try (ClientChannel channel = session.createChannel(Channel.CHANNEL_SHELL)) { Collection<ClientChannelEvent> result = channel.waitFor(EnumSet.of(ClientChannelEvent.CLOSED), WAIT); assertTrue("Wrong channel state: " + result, result.containsAll(EnumSet.of(ClientChannelEvent.CLOSED))); } } finally { client.stop(); } }
@Override public void write(int b) throws IOException { if (channel.isClosed() || channel.isClosing()) { throw new IOException("Channel (" + channel + ") is closing / closed on write single byte"); } if (b == (eofSignal & 0xff)) { log.info("Closing channel (" + channel + ") due to single byte EOF"); channel.close(true); } }
@Test public void testSwitchToNoneCipher() throws Exception { setUp(0L, 0L, 0L); sshd.getCipherFactories().add(BuiltinCiphers.none); try (SshClient client = setupTestClient()) { client.getCipherFactories().add(BuiltinCiphers.none); client.start(); try (ClientSession session = client.connect(getCurrentTestName(), TEST_LOCALHOST, port).verify(7L, TimeUnit.SECONDS).getSession()) { session.addPasswordIdentity(getCurrentTestName()); session.auth().verify(5L, TimeUnit.SECONDS); outputDebugMessage("Request switch to none cipher for %s", session); KeyExchangeFuture switchFuture = session.switchToNoneCipher(); switchFuture.verify(5L, TimeUnit.SECONDS); try (ClientChannel channel = session.createSubsystemChannel(SftpConstants.SFTP_SUBSYSTEM_NAME)) { channel.open().verify(5L, TimeUnit.SECONDS); } } finally { client.stop(); } } }
final ProgressOutputStream ierr = new ProgressOutputStream(err)) { channel = session.createExecChannel(command); channel.setIn(iin); channel.setOut(iout); channel.setErr(ierr); channel.open(); boolean activity; do { stat = channel.waitFor( EnumSet.of( ClientChannelEvent.CLOSED, stat = channel.waitFor( EnumSet.of( ClientChannelEvent.CLOSED, if (stat.contains(ClientChannelEvent.EXIT_STATUS) && channel.getExitStatus() != 0) { throw new IOException( String.format( "Command returned failure code %2$d during SSH session '%1$s'", this.getDisplayHost(), channel.getExitStatus())); } finally { if (channel != null) { Set<ClientChannelEvent> stat = channel.waitFor( EnumSet.of( ClientChannelEvent.CLOSED,
@Test public void testClientWithHeartBeat() throws Exception { SshClient client = setupTestClient(); PropertyResolverUtils.updateProperty(client, ClientFactoryManager.HEARTBEAT_INTERVAL, HEARTBEAT); client.start(); try (ClientSession session = client.connect(getCurrentTestName(), TEST_LOCALHOST, port).verify(7L, TimeUnit.SECONDS).getSession()) { session.addPasswordIdentity(getCurrentTestName()); session.auth().verify(5L, TimeUnit.SECONDS); try (ClientChannel channel = session.createChannel(Channel.CHANNEL_SHELL)) { Collection<ClientChannelEvent> result = channel.waitFor(EnumSet.of(ClientChannelEvent.CLOSED), WAIT); assertTrue("Wrong channel state: " + result, result.contains(ClientChannelEvent.TIMEOUT)); } } finally { client.stop(); } }