Com.jcraft.jsch.JSchException: UnknownHostKey
I'm trying to use Jsch to establish an SSH connection in Java. My code produces the following 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 cannot find how to verify the host key in the Jsch documentation. I have included my code below.
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);
}
}
}
Getting jcraft.jsch.JSchException: UnknownHostKey” exception.Depending on what program you use for ssh, the way to get the proper key could vary. Putty (popular with Windows) uses their own format for ssh keys. With most variants of Linux and BSD that I've seen, you just have to look in ~/.ssh/known_hosts. I usually ssh from a Linux machine and then copy this file to a Windows machine. Then I use something similar to
jsch.setKnownHosts("C:\Users\cabbott\known_hosts");
Assuming I have placed the file in C:Userscabbott on my Windows machine. If you don't have access to a Linux machine, try http://www.cygwin.com/
Maybe someone else can suggest another Windows alternative. I find putty's way of handling SSH keys by storing them in the registry in a non-standard format bothersome to extract.