Swing GUI 中的 JSch 交互式 SSH 控制台



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

我的问题是使用 .getText(( 从单独的文本字段获取字符串,将字符串通过管道传输到控制台,并从远程 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 的图片

找到了有关如何创建 PipedOutputStream 以及将 TextField 打印到 PipedStream 并显示输出的答案。JSCH 通道外壳,输入/输出由 @Christopher Lewis

最新更新