Ganymed - 如何执行需要密码的ssh / scp

时间:2013-09-02 19:00:04

标签: java

我使用Ganymed,这是一个实现SSH-2协议的库,我希望它连接到其他服务器并复制到其他服务器文件。

问题是sshscp命令需要密码(我不能用密钥),而且我这样做 - 它不起作用。

以下是一个例子:

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);
    }
}

当我调试这个时,我看到服务器返回拒绝访问。

1 个答案:

答案 0 :(得分:0)

我从未使用Ganymed但只是通过查看我怀疑您过早发送密码的代码。您可能需要处理服务器响应您的问题。换句话说,服务器不会收到您认为收到的内容。