我使用甘尼德,它是一个实现SSH-2协议的库,我希望它连接到其他服务器并复制到其他服务器文件。
问题是,ssh和scp命令需要一个密码(我不能用键来做),而当我这样做的时候,它就不工作了。
下面是一个示例:
public static void main(String[] args)
{
String username = "sabre";
String host = "beta";
String password = "xxxxxxxx";
try
{
Connection conn = new Connection(host);
conn.connect();
boolean isAuthenticated = conn.authenticateWithPassword(username, password);
if (!isAuthenticated)
throw new IOException("Authentication failed.");
Session sess = conn.openSession();
sess.requestPTY("bash");
sess.startShell();
OutputStreamWriter writer = new new OutputStreamWriter(sess.getStdin(), "utf-8");
writer.write("ssh nir@192.168.1.123 \n");
writer.flush();
writer.write("password\n");
writer.flush();
writer.close();
sess.waitForCondition(ChannelCondition.CLOSED | ChannelCondition.EOF | ChannelCondition.EXIT_STATUS, 10000);
sess.close();
conn.close();
} catch (Exception e)
{
e.printStackTrace(System.err);
System.exit(2);
}
}当我调试它时,我看到服务器返回访问被拒绝的结果。
发布于 2013-09-02 19:05:41
我从未使用过Ganymed,但通过查看代码,我怀疑您发送密码太早了。您可能需要处理服务器对您的响应。换句话说,服务器接收的不是您所认为的。
https://stackoverflow.com/questions/18579345
复制相似问题