Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/oracle/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Ganymed-如何执行需要密码的ssh/scp_Java - Fatal编程技术网

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

Java Ganymed-如何执行需要密码的ssh/scp,java,Java,我使用,这是一个实现SSH-2协议的库,我希望它连接到其他服务器并复制到其他服务器文件 问题是ssh和scp命令需要一个密码(我不能用键来做),而当我这样做时,它就不起作用了 以下是一个例子: public static void main(String[] args) { String username = "sabre"; String host = "beta"; String password = "xxxxxxxx"; try {

我使用,这是一个实现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);
    }
}

当我调试时,我看到服务器返回拒绝访问的消息。

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

我可以等到服务器等待密码,然后输入
密码。这就是你的意思吗?或者你说可能有不同的方法向服务器发送响应?@nir-Yep。尝试等待响应,然后发送密码。