tags:

views:

2056

answers:

4

I don't know how SSH works and I think that's a simple question. How do I fix that exception:

com.jcraft.jsch.JSchException: UnknownHostKey: mywebsite.com. 
RSA key fingerprint is 22:fb:ee:fe:18:cd:aa:9a:9c:78:89:9f:b4:78:75:b4

I know I should verify that key or something, but there is like zero documentation for Jsch. Here is my code it's really straightforward:

import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;

public class ssh{
  public static void main(String[] arg){

    try{
       JSch jsch = new JSch();

       //create SSH connection
       String host = "mywebsite.com";
       String user = "username";
       String password = "123456";

       Session session = jsch.getSession(user, host, 22);
       session.setPassword(password);
       session.connect();

     }
     catch(Exception e){
       System.out.println(e);
    } 
  }
}
A: 

Add that host to ~/.ssh/known_hosts.

bmargulies
+3  A: 

I would either:

  1. Try to ssh from the command line and accept the public key (the host will be added to ~/.ssh/known_hosts and everything should then work fine from JSch) -OR-
  2. Configure JSch to not use "StrictHostKeyChecking" using the following code:

    java.util.Properties config = new java.util.Properties(); 
    config.put("StrictHostKeyChecking", "no");
    session.setConfig(config);
    

Option #1 (adding the host to the ~/.ssh/known_hosts file) has my preference.

Pascal Thivent
A: 

Hi, Me too trying the same activity with unix server from a java code. I was also getting the Exception previously -> com.jcraft.jsch.JSchException: UnknownHostKey:

And i tried with one of the options before calling the session.connect();... Configure JSch to not use "StrictHostKeyChecking" using the following code: java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no"); session.setConfig(config);

But, i am getting the following exception... com.jcraft.jsch.JSchException: Auth cancel at com.jcraft.jsch.Session.connect(Session.java:451) at com.jcraft.jsch.Session.connect(Session.java:150) at com.jpmc.test.TestConnect.main(TestConnect.java:34)

Can anybody help me on resolving this.

vijith
that qualifies as a different Question
Tshepang
A: 

Try to shut down sshd on your *nix host, and start a single thread in the foreground: /usr/sbin/sshd -d

This will give you a lot of debugging info from the sshd side.

Mark