【发布时间】:2016-09-02 14:48:32
【问题描述】:
我正在使用 Ganymede ssh2 从我的 Java 应用程序连接到服务器并在那里做一些工作。
它运行良好,但问题是请求批准的 ssh 命令,例如。
一个命令
stop someService
返回
Are you sure (y/n)?
在适当的击键 (y/n) 之后,它会继续前进。
目前我正在使用 ssh2 ganymed 示例给出的实现,就像这样:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.Session;
import ch.ethz.ssh2.StreamGobbler;
public class ConnectUtil(String hostname, String username, String password, String command)
{
try
{
Connection conn = new Connection(hostname);
conn.connect();
boolean isAuthenticated = conn.authenticateWithPassword(username, password);
if (isAuthenticated == false)
throw new IOException("Authentication failed.");
Session sess = conn.openSession();
sess.execCommand(command);
InputStream stdout = new StreamGobbler(sess.getStdout());
BufferedReader br = new BufferedReader(new InputStreamReader(stdout));
while (true)
{
String line = br.readLine();
if (line == null)
break;
System.out.println(line);
}
sess.close();
conn.close();
}
catch (IOException e)
{
e.printStackTrace(System.err);
}
}
用上面提到的命令('stop someService')调用这个util类后,它卡在了
String line = br.readLine();
服务器超时后一切都会中断。
非常欢迎任何有关如何解决此问题的想法。
非常感谢, 米洛斯。
【问题讨论】: