Package org.apache.sshd.client.channel

Examples of org.apache.sshd.client.channel.ChannelShell


            throw new IllegalArgumentException("Unsupported channel type " + type);
        }
    }

    public ChannelShell createShellChannel() throws IOException {
        ChannelShell channel = new ChannelShell();
        registerChannel(channel);
        return channel;
    }
View Full Code Here


        client = SshClient.setUpDefaultClient();
        client.start();
        ClientSession s = client.connect("localhost", port).await().getSession();
        s.authPassword("test", "test").await();
        ChannelShell shell = s.createShellChannel();
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        ByteArrayOutputStream err = new ByteArrayOutputStream();
        shell.setOut(out);
        shell.setErr(err);
        shell.open().await();
        int res = s.waitFor(ClientSession.CLOSED, 5000);
        assertEquals("Session should be closed", ClientSession.CLOSED | ClientSession.AUTHED, res);
        assertTrue(latch.await(1, TimeUnit.SECONDS));
        assertTrue(TestEchoShellFactory.TestEchoShell.latch.await(1, TimeUnit.SECONDS));
    }
View Full Code Here

            throw new IllegalArgumentException("Unsupported channel type " + type);
        }
    }

    public ChannelShell createShellChannel() throws IOException {
        ChannelShell channel = new ChannelShell();
        getConnectionService().registerChannel(channel);
        return channel;
    }
View Full Code Here

        client.getProperties().put(SshClient.WINDOW_SIZE, "1024");
        client.start();
        ClientSession session = client.connect("smx", "localhost", port).await().getSession();
        session.addPasswordIdentity("smx");
        session.auth().verify();
        final ChannelShell channel = session.createShellChannel();
        channel.setStreaming(ClientChannel.Streaming.Async);
        channel.open().verify();


        final byte[] message = "0123456789\n".getBytes();
        final int nbMessages = 1000;

        final ByteArrayOutputStream baosOut = new ByteArrayOutputStream();
        final ByteArrayOutputStream baosErr = new ByteArrayOutputStream();
        final AtomicInteger writes = new AtomicInteger(nbMessages);

        channel.getAsyncIn().write(new Buffer(message))
                .addListener(new SshFutureListener<IoWriteFuture>() {
                    public void operationComplete(IoWriteFuture future) {
                        try {
                            if (future.isWritten()) {
                                if (writes.decrementAndGet() > 0) {
                                    channel.getAsyncIn().write(new Buffer(message)).addListener(this);
                                } else {
                                    channel.getAsyncIn().close(false);
                                }
                            } else {
                                throw new SshException("Error writing", future.getException());
                            }
                        } catch (IOException e) {
                            if (!channel.isClosing()) {
                                e.printStackTrace();
                                channel.close(true);
                            }
                        }
                    }
                });
        channel.getAsyncOut().read(new Buffer())
                .addListener(new SshFutureListener<IoReadFuture>() {
                    public void operationComplete(IoReadFuture future) {
                        try {
                            future.verify();
                            Buffer buffer = future.getBuffer();
                            baosOut.write(buffer.array(), buffer.rpos(), buffer.available());
                            buffer.rpos(buffer.rpos() + buffer.available());
                            buffer.compact();
                            channel.getAsyncOut().read(buffer).addListener(this);
                        } catch (IOException e) {
                            if (!channel.isClosing()) {
                                e.printStackTrace();
                                channel.close(true);
                            }
                        }
                    }
                });
        channel.getAsyncErr().read(new Buffer())
                .addListener(new SshFutureListener<IoReadFuture>() {
                    public void operationComplete(IoReadFuture future) {
                        try {
                            future.verify();
                            Buffer buffer = future.getBuffer();
                            baosErr.write(buffer.array(), buffer.rpos(), buffer.available());
                            buffer.rpos(buffer.rpos() + buffer.available());
                            buffer.compact();
                            channel.getAsyncErr().read(buffer).addListener(this);
                        } catch (IOException e) {
                            if (!channel.isClosing()) {
                                e.printStackTrace();
                                channel.close(true);
                            }
                        }
                    }
                });

        channel.waitFor(ClientChannel.CLOSED, 0);

        assertEquals(nbMessages * message.length, baosOut.size());
    }
View Full Code Here

        client.start();
        ClientSession session = client.connect("smx", "localhost", port).await().getSession();
        session.addPasswordIdentity("smx");
        session.auth().verify();

        ChannelShell channel = new ChannelShell();
        session.getService(ConnectionService.class).registerChannel(channel);

        ByteArrayOutputStream sent = new ByteArrayOutputStream();
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        ByteArrayOutputStream err = new ByteArrayOutputStream();
        channel.setOut(out);
        channel.setErr(err);
        channel.open().verify();

        channel.close(false).await();
        client.stop();
    }
View Full Code Here

            throw new IllegalArgumentException("Unsupported channel type " + type);
        }
    }

    public ChannelShell createShellChannel() throws Exception {
        ChannelShell channel = new ChannelShell();
        registerChannel(channel);
        return channel;
    }
View Full Code Here

            throw new IllegalArgumentException("Unsupported channel type " + type);
        }
    }

    public ChannelShell createShellChannel() throws IOException {
        ChannelShell channel = new ChannelShell();
        getConnectionService().registerChannel(channel);
        return channel;
    }
View Full Code Here

        SshClient client1 = SshClient.setUpDefaultClient();
        client1.setAgentFactory(localAgentFactory);
        client1.start();
        ClientSession session1 = client1.connect("localhost", port1).await().getSession();
        assertTrue(session1.authAgent("smx").await().isSuccess());
        ChannelShell channel1 = session1.createShellChannel();
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        ByteArrayOutputStream err = new ByteArrayOutputStream();
        channel1.setOut(out);
        channel1.setErr(err);
        channel1.setAgentForwarding(true);
        channel1.open().await();
        OutputStream pipedIn = channel1.getInvertedIn();

        synchronized (shellFactory.shell) {
            System.out.println("Possibly waiting for remote shell to start");
            if (!shellFactory.shell.started) {
                shellFactory.shell.wait();
            }
        }

        SshClient client2 = SshClient.setUpDefaultClient();
        client2.setAgentFactory(agentFactory);
        client2.getProperties().putAll(shellFactory.shell.getEnvironment().getEnv());
        client2.start();
        ClientSession session2 = client2.connect("localhost", port2).await().getSession();
        assertTrue(session2.authAgent("smx").await().isSuccess());
        ChannelShell channel2 = session2.createShellChannel();
        channel2.setIn(shellFactory.shell.getIn());
        channel2.setOut(shellFactory.shell.getOut());
        channel2.setErr(shellFactory.shell.getErr());
        channel2.setAgentForwarding(true);
        channel2.open().await();

        pipedIn.write("foo\n".getBytes());
        pipedIn.flush();

        Thread.sleep(1000);
View Full Code Here

        client = SshClient.setUpDefaultClient();
        client.start();
        ClientSession s = client.connect("localhost", port).await().getSession();
        s.authPassword("test", "test").await();
        ChannelShell shell = s.createShellChannel();
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        ByteArrayOutputStream err = new ByteArrayOutputStream();
        shell.setOut(out);
        shell.setErr(err);
        shell.open().await();
        int res = s.waitFor(ClientSession.CLOSED, 5000);
        assertEquals("Session should be closed", ClientSession.CLOSED | ClientSession.AUTHED, res);
        assertTrue(latch.await(1, TimeUnit.SECONDS));
        assertTrue(TestEchoShellFactory.TestEchoShell.latch.await(1, TimeUnit.SECONDS));
    }
View Full Code Here

        SshClient client = SshClient.setUpDefaultClient();
        client.start();
        ClientSession session = client.connect("localhost", port).await().getSession();
        session.authPassword("smx", "smx").await();
        ChannelShell channel = session.createShellChannel();

        ByteArrayOutputStream sent = new ByteArrayOutputStream();
        PipedOutputStream pipedIn = new PipedOutputStream();
        channel.setIn(new PipedInputStream(pipedIn));
        OutputStream teeOut = new TeeOutputStream(sent, pipedIn);
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        ByteArrayOutputStream err = new ByteArrayOutputStream();
        channel.setOut(out);
        channel.setErr(err);
        channel.open();

        teeOut.write("this is my command\n".getBytes());
        teeOut.flush();

        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < 10; i++) {
            sb.append("0123456789");
        }
        sb.append("\n");

        for (int i = 0; i < 10; i++) {
            teeOut.write(sb.toString().getBytes());
            teeOut.flush();
            session.reExchangeKeys();
        }
        teeOut.write("exit\n".getBytes());
        teeOut.flush();

        channel.waitFor(ClientChannel.CLOSED, 0);

        channel.close(false);
        client.stop();

        assertArrayEquals(sent.toByteArray(), out.toByteArray());
    }
View Full Code Here

TOP

Related Classes of org.apache.sshd.client.channel.ChannelShell

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.