JframeベースのSSHクライアントを構築しようとしています。クライアントを接続し、出力ストリームをJTextAreaにパイプします。JSch Channel InputStreamをJTextFieldボックスに接続すると、コマンドが送信されない
問題はユーザー入力にあり、JTextAreaの下のJTextFieldから取得し、ユーザーがEnterキーを押してサーバーに送信したときに読み取られます。
public class InputArea extends InputStream {
byte[] contents;
int pointer = 0;
public InputArea(JTextField inputarea) {
TextPrompt consoleTp = new TextPrompt(">> ", inputarea);
consoleTp.setShow(TextPrompt.Show.FOCUS_LOST);
inputarea.setPreferredSize(new Dimension(560, 40));
inputarea.setVisible(true);
// Fire listener for input area when enter key is pressed .
inputarea.addKeyListener(new KeyAdapter() {
@Override
public void keyReleased(KeyEvent e) {
if (e.getKeyChar() == '\n') {
contents = inputarea.getText().getBytes();
pointer = 0;
inputarea.setText("");
}
super.keyReleased(e);
}
});
}
@Override
public int read() throws IOException {
if (pointer >= contents.length) return -1;
return this.contents[pointer++];
}
}
このクラスは入力ストリームを拡張し、Enterキーを押下リッスン。そこからJTextFieldの内容が読み込まれ、(うまくいけば)これが 'contents'に設定されます。これは空でないときにInputStreamによって読み込まれます。
そこから、コンソールウィンドウのメインJFrameクラスにあります。
//
// Configure input area
//
JTextField inputTextField = new JTextField();
in = new InputArea(inputTextField);
add(inputTextField);
次に、SSHサーバーに接続する方法で、
セッションセッション= jsch.getSession(user、host、port);
// Disable key checking
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.setPassword(String.valueOf(password));
System.out.println("Attempting to connect to " + host + " on port " + port + " ....");
session.connect(3000);
Channel channel = session.openChannel("shell");
channel.setInputStream(in);
channel.setOutputStream(out);
channel.connect(3000);
System.out.println("Connected.");
細かいサーバ機能の出力ストリーム(下のスクリーンショットを参照)、コマンドを入力し、リターンキーを押したときに、テキストフィールドにその内容をリセットします。ただし、このコマンドはサーバーに送信されません(mkdir Testing ectを実行してテストされます)。
アドバイスをいただければ幸いです。完全なコードサンプルが必要な場合はhttps://github.com/ElliotAlexander/SSH-Connect-GUIにあります。彼らは改行文字で終了されていないため、リモートサーバに送信
:
最後に、あなたのコマンドは、出力ストリームを書きますサーバーに到達していません。 –
解決策を更新しました。 –