简体   繁体   English

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

[英]Ganymed - how to execute ssh/scp that requires a password

I use Ganymed , which is a library that implements the SSH-2 protocol, and I want it to connect to other sever and copy to other sever files. 我使用Ganymed ,这是一个实现SSH-2协议的库,并且我希望它连接到其他服务器并复制到其他服务器文件。

The problem is that the ssh and scp commands requires a password (I cant do it with keys) and as I do it - it doesnt work. 问题是sshscp命令需要一个密码(我不能使用键来做到这一点),而且在执行时-它不起作用。

Here is an example: 这是一个例子:

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

When I debug this I see that the server return that the access is denied. 调试时,我看到服务器返回拒绝访问的信息。

I've never used Ganymed but just by looking at the code I suspect that you send password too early. 我从未使用过Ganymed但只是通过查看代码,我怀疑您发送密码的时间过早。 You probably need to handle what server responds to you. 您可能需要处理服务器响应您的内容。 In other words server receives not what you think it receives. 换句话说,服务器没有收到您认为它收到的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM