【问题标题】:JSch interactive SSH console in a Swing GUISwing GUI 中的 JSch 交互式 SSH 控制台
【发布时间】:2017-10-21 14:36:58
【问题描述】:

构建一个 GUI 以通过 SSH 与机器人远程交互。 我正在使用 JSch 创建一个 SSH 会话,然后将输出通过 PrintStream 打印到 TextArea 到 System.setOut 和 System.setErr。

我的问题是使用 .getText() 将字符串从单独的 TextField 中获取到控制台,然后从远程 pc 接收数据流并在实时会话中显示。

我在底部附上了当前项目的图片,下面是正在发生的事情的简化代码。

import com.jcraft.jsch.*;
import javax.swing.*;
import java.io.PrintStream;
import java.util.Properties;
import java.awt.ComponentOrientation;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class RPiClient {

private final JButton sendButton = new JButton("Send CMD");
private JTextField sendField;

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                new RPiClient();
            } catch (Exception e) {
                System.out.print("failed to run");
                e.printStackTrace();
            }
        }
    });
}

public RPiClient() {
    buildGUI(); 
    connectSSH();
    actionListeners();
}

public void buildGUI(){

    JFrame mainFrame = new JFrame();
    mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    mainFrame.setBounds(100, 100, 900, 900);
    mainFrame.getContentPane().setLayout(null);
    JPanel panel = new JPanel();
    panel.setBounds(10, 11, 864, 699);
    mainFrame.getContentPane().add(panel);
    mainFrame.setVisible(true);
    panel.setLayout(null);
    sendField = new JTextField();
    sendField.setText("Enter Command Then Click Send");
    sendField.setBounds(565, 261, 299, 169);
    panel.add(sendField);
    sendField.setColumns(10);
    panel.add(sendField);
    panel.add(sendButton);
    sendButton.setBounds(565, 441, 81, 23);
    JTextArea ta = new JTextArea(50,50);
    TextAreaOutputStream taos = new TextAreaOutputStream( ta, 60 );

    JScrollPane scroll = new JScrollPane(ta);
    panel.add(scroll);
    scroll.setBounds(132, 5, 423, 906);
    scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
    scroll.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);

    PrintStream ps = new PrintStream( taos );
    System.setOut( ps );
    System.setErr( ps );

}

public void connectSSH(){
    try{

        JSch jsch=new JSch();  

        String host="192.168.0.x";
        String user="root";
        String passText = "top secret";

        Session session=jsch.getSession(user, host, 22);
        session.setPassword(passText);

        Properties config = new Properties();
        config.put("StrictHostKeyChecking", "no");
        session.setConfig(config);
        session.connect();

        Channel channel=session.openChannel("shell");
        channel.setInputStream(System.in);
        channel.setOutputStream(System.out);

        channel.connect();
        }
        catch(Exception e){
          System.out.println(e);
        }
      }

      public void actionListeners()
        {
            sendButton.addActionListener(new ActionListener() 
            {
                public void actionPerformed(ActionEvent connectn2ServerE)
                    {

                    }
                }); 
      }

SSH客户端GUI图片sshClient

【问题讨论】:

  • 也许你应该使用自己的流,而不是使用System.inSystem.out,这样你可以更好地控制
  • 我是否使用 PipedIn/OutStreamByteArrayInputStreamBypyteArrayOutputStream,以及如何将信息路由到这些流?大部分教程都显示了 i/O 到 txt。任何人都可以向我指出一个实现,以便我可以练习和/或做进一步的研究?

标签: java ssh inputstream outputstream jsch


【解决方案1】:

找到了有关如何创建 PipedOutputStream 以及如何将 TextField 打印到 PipedStream 并显示输出的答案。 JSCH Channel Shell, Input / Output by @Christopher Lewis

【讨论】:

    猜你喜欢
    • 2015-10-29
    • 2014-11-30
    • 2014-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-24
    • 2013-05-05
    • 2013-09-28
    相关资源
    最近更新 更多