com.jcraft.jsch.JSchException: UnknownHostKey [英] com.jcraft.jsch.JSchException: UnknownHostKey

查看:43
本文介绍了com.jcraft.jsch.JSchException: UnknownHostKey的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Jsch 在 Java 中建立 SSH 连接.我的代码产生以下异常:

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

我在 Jsch 文档中找不到如何验证主机密钥.我在下面包含了我的代码.

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

推荐答案

我会:

  1. 尝试从命令行执行 ssh 并接受公钥(主机将被添加到 ~/.ssh/known_hosts,然后一切都应该在 Jsch 中正常工作) -OR-
  2. 使用以下代码将 JSch 配置为不使用StrictHostKeyChecking"(这会引入不安全性,应仅用于测试目的):

  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" (this introduces insecurities and should only be used for testing purposes), using the following code:

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

选项 #1(将主机添加到 ~/.ssh/known_hosts 文件)有我的偏好.

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

这篇关于com.jcraft.jsch.JSchException: UnknownHostKey的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆