我正在尝试编写一个Java程序,该程序将通过ssh连接并在工作中的服务器(redhat linux)上做一些事情。我的盒子是窗户。我阅读了有关sshj的文章,并试图使该示例生效。我已经处理了大多数依赖关系,现在在处理公钥/私钥时遇到了一个错误,不幸的是,我在那里也不了解太多(是的,这是新手的完美风暴!)。这是错误:

线程“主”中的异常net.schmizz.sshj.transport.TransportException:[HOST_KEY_NOT_VERIFIABLE]无法为端口22上的ssh-rsa验证具有指纹5f:d6:94:00:9e:ec:7e:34:6d:d0:d3:76:df:5e:dd:3dmyserver主机密钥

这是代码:

import net.schmizz.sshj.SSHClient;
import net.schmizz.sshj.common.IOUtils;
import net.schmizz.sshj.connection.channel.direct.Session;
import net.schmizz.sshj.connection.channel.direct.Session.Command;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

/** This examples demonstrates how a remote command can be executed. */
public class sshBuddy {

    public static void main(String... args)
            throws IOException {
        final SSHClient ssh = new SSHClient();
        ssh.loadKnownHosts();
        //ssh.addHostKeyVerifier("5f:d6:94:00:9e:ec:7e:34:6d:d0:d3:76:df:5e:dd:3d");

        ssh.connect("myserver");
        try {
            ssh.authPublickey(System.getProperty("myusername"));
            final Session session = ssh.startSession();
            try {
                final Command cmd = session.exec("ping -c 1 google.com");
                System.out.println(IOUtils.readFully(cmd.getInputStream()).toString());
                cmd.join(5, TimeUnit.SECONDS);
                System.out.println("\n** exit status: " + cmd.getExitStatus());
            } finally {
                session.close();
            }
        } finally {
            ssh.disconnect();
        }
    }

}


任何帮助,将不胜感激,谢谢!

最佳答案

试试这个

ssh.addHostKeyVerifier(new PromiscuousVerifier());


这应该工作

07-27 23:14