我在尝试使用java中的Ganymed库进行连接测试时有点问题,2周前它工作正常,没有问题,但现在我收到以下错误消息:尝试执行命令时抛出的错误
The execute request failed.
这与Ganymed的“execCommand()”方法有关。当我使用WinSCP进行连接时,一切正常,但尝试使用java连接会给我带来错误。我也在想,如果防火墙也可能是原因,只是一个想法。
我使用以执行tail命令的代码如下:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.Session;
import ch.ethz.ssh2.StreamGobbler;
public class SSHTesting {
public static void main(String[] args) {
try{ Connection conn = new Connection("eappdev101.momentum.co.za");
conn.connect();
boolean isAuthenticated = conn.authenticateWithPassword("username", "password");
if (isAuthenticated == false) {
System.out.println("Credentials are wrong.");
}
Session sess = conn.openSession();
sess.execCommand("tail -f /logs/SystemOut.log");
InputStream stdout = new StreamGobbler(sess.getStdout());
BufferedReader br = new BufferedReader(new InputStreamReader(stdout));
sess.close();
conn.close();
System.out.println("Done");
}
catch(Exception ie){
System.out.println(ie.getMessage());
}
}
}
光的任何棚屋在高度赞赏。先谢谢你。
代码看起来很好,所以这可能是因为SSH服务器的身份验证组态。 –