Java Swing GUI中的JSch交互式SSH控制台
构建GUI以通过SSH远程与机器人交互。 我使用JSch创建一个SSH会话,然后通过一个到System.setOut和System.setErr的打印流将输出打印到TextArea 我的问题是使用.getText()从单独的文本字段获取字符串,将字符串导入控制台,从远程pc接收数据流并在实时会话中显示 我在下面附上了一张当前项目的图片,以及下面正在发生的事情的简化代码Java Swing GUI中的JSch交互式SSH控制台,java,ssh,inputstream,outputstream,jsch,Java,Ssh,Inputstream,Outputstream,Jsch,构建GUI以通过SSH远程与机器人交互。 我使用JSch创建一个SSH会话,然后通过一个到System.setOut和System.setErr的打印流将输出打印到TextArea 我的问题是使用.getText()从单独的文本字段获取字符串,将字符串导入控制台,从远程pc接收数据流并在实时会话中显示 我在下面附上了一张当前项目的图片,以及下面正在发生的事情的简化代码 import com.jcraft.jsch.*; import javax.swing.*; import java.io.P
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的图片找到了如何创建PipedOutStream以及如何将文本字段打印到PipedStream并显示输出的答案 也许您不应该使用
System.in
和System.out
,而应该使用您自己的流,这样您可以更好地控制我使用的是PipedIn/OutStream、ByteArrayInputStream和BypyteArrayOutputStream,以及如何将信息路由到这些流?本教程的大部分内容都显示了txt的i/O。有没有人能给我指一个实现,这样我就可以练习和/或做进一步的研究?