現在、Ganymed-SSH2-Library for Javaを使用して、sshサーバーでいくつかのコマンドを実行しようとしています。また、perlスクリプトを呼び出す必要があるためSession.execCommand();
、「通常の」unixコマンドを実行するだけなので使用できません(またはこれは間違っていますか?)。代わりに、疑似端末を使用する必要があります。
次のコードを実行すると、ハングします。
session = con.openSession();
session.requestPTY("vt220");
session.execCommand("/bin/bash");
session.getStdin().write("echo test".getBytes());
int b = 1;
InputStream is = new StreamGobbler(session.getStdout());
while ((b = is.read()) != -1) {
System.out.print((char)b);
}
System.out.println("finished");
結果は次のようになります(打ち切られたUSERNAMEとHOST)。
echo testUSERNAME@HOST:~
# echo test
しかし、コードはこの時点でハングし、到達することはありませんSystem.out.println("finished");
接続構築は完全に機能します。次のコードを実行すると、機能します。
session = con.openSession();
session.execCommand("ls");
BufferedReader reader = new BufferedReader(new InputStreamReader(new StreamGobbler(session.getStdout())));
String line = reader.readLine();
while (line != null) {
line = reader.readLine();
System.out.println(line);
}
System.out.println("finished");
問題がどこにあるか誰もが知っていますか?少し前に、Jsch(Java用の別のSSH-Lib)でこれを試しましたが、同じ問題があります。
前もって感謝します :)