• <dd id="3gzlp"></dd>

    <li id="3gzlp"><acronym id="3gzlp"></acronym></li>

    <span id="3gzlp"></span>

    apche mina sshd shell交互

    OSC_ofJBjn 發布于 06/13 16:02
    閱讀 21
    收藏 0

    面對海量數據,你能否從容應對?>>>

    最近使用apache mina ssh shell 交互,但是拿到channel的 in 和out 輸入 輸出沒反應  ,這方面資料也太少了,在這里求大神指教一下

    try (SshClient client = SshClient.setUpDefaultClient()) {
                //PropertyResolverUtils.updateProperty(client, FactoryManager.IDLE_TIMEOUT, 30000L);
                client.start();
                ConnectFuture connect = client.connect("root", "127.0.0.1", 22);
                if (connect.await(5000L)) {
                    try (ClientSession session = connect.getSession()) {
                        session.addPasswordIdentity("123");
                        session.auth().verify(5000L);
                        try (final ChannelShell channel = (ChannelShell) session.createChannel(ClientChannel.CHANNEL_SHELL)) {
                            channel.setAgentForwarding(true);
                            channel.setPtyType("xterm");
                            channel.setPtyColumns(160);
                            channel.setPtyLines(50);
                            channel.setPtyWidth(0);
                            channel.setPtyHeight(0);
                            Map<PtyMode, Integer> ptyModes = new HashMap<>();
                            ptyModes.put(PtyMode.TTY_OP_OSPEED, 38400);
                            ptyModes.put(PtyMode.TTY_OP_ISPEED, 38400);
                            channel.setPtyModes(ptyModes);
    
    
                          ChannelOutputStream channelOutputStream    = new ChannelOutputStream(channel, channel.getRemoteWindow(), logger, (byte)94, true);
                            InputStream inputStream= new NoCloseInputStream(new ByteArrayInputStream("\r".getBytes()));
                            channel.setIn(inputStream);
                            channel.setOut(new NoCloseOutputStream(new ByteArrayOutputStream()));
                            channel.setErr(new NoCloseOutputStream(System.err));
                            InputStream inputStream=  channel.getInvertedOut();
                            try {
                                channel.open();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
    
                   
                            try {
                                //循環讀取
                                byte[] buffer = new byte[1024];
                                int i = 0;
                                //如果沒有數據來,線程會一直阻塞在這個地方等待數據。
                                while ((i = inputStream.read(buffer)) != -1) {
                                    sendMessage(webSocketSession, Arrays.copyOfRange(buffer, 0, i));
                                }
    
                            } finally {
                                //斷開連接后關閉會話
                                session.close();
                                channel.close();
                                if (inputStream != null) {
                                    inputStream.close();
                                }
                            }
    
                            channel.waitFor(Arrays.asList(ClientChannelEvent.CLOSED), 0);
                                                    //設置channel
    
    
    
    
                        } finally {
                            session.close(false);
                        }
                    } finally {
                        client.stop();
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
    加載中
    返回頂部
    頂部
    聚看影院