You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@mina.apache.org by Lolita <10...@qq.com.INVALID> on 2022/06/24 03:15:47 UTC

I want to use sshd to achieve interactive commands with the server, that is, shell mode

Can provide detailed code, I have been inquiring for a long time but there is no example, I try to write it myself, but it is abnormal,


Exception in thread "channelShell-in" Exception in thread "channelShell-err" java.lang.RuntimeException: java.io.IOException: Stream closed
	at com.example.core.shell.ShellService$ChannelShellModify.lambda$receiveMsg$0(ShellService.java:131)
	at java.base/java.lang.Thread.run(Thread.java:833)
Caused by: java.io.IOException: Stream closed





public static ClientSession openSession(String host, int port, String user, String pwd, int timeout) throws IOException {
    client = SshClient.setUpDefaultClient();
    client.start();
    ClientSession session = client.connect(user, host, port).verify(10 * 1000).getSession();
    session.addPasswordIdentity(pwd);
    session.auth().verify(TimeUnit.SECONDS.toMillis(timeout));
    return session;
}public void initialConnection(Host host, String key) throws Exception {

    ClientSession session = SshSessionPoll.INSTANCE.getSession(String.valueOf(host.id), host.getIp(), host.getPort(), host.getUsername(), host.getPwd(), 3000);

    ChannelShell shellChannel = SshUtil.createShellChannel(session);

    shellChannel.open();
    ChannelShellModify channelShellModify = new ChannelShellModify(shellChannel);
    channelShellModify.receiveMsg((result) -&gt; {
        ShellSocket.sendMsg(ShellSocketHandler.SendShellMessage.builder().hostId(host.id).channelId(key).message(result).build());
        System.out.println(result);
    });

    CHANNEL_POOL.put(key, channelShellModify);
    shellChannel.wait();
}
class ChannelShellModify {
    private ChannelShell channelShell;
    private InputStream inputStream;
    private OutputStream outputStream;


    public ChannelShellModify(ChannelShell channelShell) {
        this.channelShell = channelShell;
        this.outputStream = channelShell.getOut();

    }

    public void sendMsg(String cmd) throws Exception {
        outputStream.write(cmd.getBytes());
        outputStream.flush();
    }

    public void receiveMsg(Consumer<String&gt; msgHandle) {
        new Thread(() -&gt; {
            this.inputStream = channelShell.getIn();
            byte[] buffer = new byte[1024];
            BufferedInputStream bufferedInputStream = new BufferedInputStream(this.inputStream);
            int i = 0;

            try {
                //改用nio中的buff更好操作
                while (true) {
                    if ((i = bufferedInputStream.read(buffer)) == -1) break;
                    msgHandle.accept(StrUtil.getUtf8String(new String(buffer, 0, i)));
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }, "channelShell-in").start();

        new Thread(() -&gt; {
            byte[] buffer = new byte[1024];
            BufferedInputStream bufferedInputStream = new BufferedInputStream(channelShell.getInvertedErr());
            int i = 0;
            try {
                //改用nio中的buff更好操作
                while (true) {
                    if ((i = bufferedInputStream.read(buffer)) == -1) break;
                    msgHandle.accept(StrUtil.getUtf8String(new String(buffer, 0, i)));
                    System.out.println("errMsg");
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }, "channelShell-err").start();
    }


}