Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

struggling with sshj example ('exec') : Could not verify `ssh-rsa` host key with fingerprint

I'm trying to write a java program that will connect via ssh and do some stuff on a server at work (redhat linux). My box is windows. I read about sshj and I'm trying to get the example to work. I've worked through most of the dependencies and now I have an error dealing with public/private keys and unfortunately I don't know much there either (yes, it's a perfect storm of newbie-ness!). Here's the error:

Exception in thread "main" net.schmizz.sshj.transport.TransportException: [HOST_KEY_NOT_VERIFIABLE] Could not verify ssh-rsa host key with fingerprint 5f:d6:94:00:9e:ec:7e:34:6d:d0:d3:76:df:5e:dd:3d for myserver on port 22

Here's the code:

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

}

Any help would be appreciated, thanks!

like image 857
hatrickpatrick Avatar asked Jun 06 '12 21:06

hatrickpatrick


People also ask

How do I remove host key verification?

Using Config File You can also define the strings to disable host key checking in the configuration file. You need to create a ~/. ssh/config file and disable strict host key checking by adding the content. This will disable host checking for all hosts you connect to.

What is ssh host key validation?

In host key checking, ssh automatically maintains and checks a database containing identification for all hosts it has ever been used with. Host keys are stored in ~/. ssh/known_hosts in the user's home directory. Additionally, the /etc/ssh/ssh_known_hosts file is automatically checked for known hosts.


1 Answers

just try this

ssh.addHostKeyVerifier(new PromiscuousVerifier());

this should work

like image 85
lennon_liang Avatar answered Sep 30 '22 14:09

lennon_liang