目前我正在尝试使用 Java 的 Ganymed-SSH2-Library 在 ssh 服务器上运行一些命令。因为我还需要调用 perl 脚本,所以我不能使用Session.execCommand();
,因为它只会运行“正常”的 unix-commands(或者这是错误的吗?)。相反,我需要使用伪终端。
如果我运行以下代码,它会挂起:
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)试过这个,但它有同样的问题。
提前致谢 :)