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

时间:2017-05-22 03:46:51

标签: java ssh inputstream outputstream jsch

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

我的问题是从一个单独的TextField获取String,并将.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

的图片

1 个答案:

答案 0 :(得分:0)

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