JSchException:UnknownHostKey [英] JSchException: UnknownHostKey

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

问题描述

我正在尝试使用Jsch在Java中建立SSH连接. 我已经将"StrictHostKeyChecking"设置为是.我了解必须先获取服务器的主机密钥并将其存储在主机密钥文件中 在第一次尝试连接到服务器之前.如何获取服务器的HostKey.我的代码产生以下异常:

I'm trying to use Jsch to establish an SSH connection in Java. I have set "StrictHostKeyChecking" to yes. I understand that the hostkey of the server has to be obtained before hand and store in the hostkey file before the first attempt to connect to the server. How can I get the HostKey of the server. My code produces the following exception:

com.jcraft.jsch.JSchException:UnknownHostKey:ASY-PC RSA密钥指纹为22:fb:ee:fe:18:cd:aa:9a:9c:78:89:9f:b4:78:75:b4

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

如何与StrictHostKeyChecking建立连接是. 这是我的代码.

How can I make connection with StrictHostKeyChecking Yes. Here is my code.

package sshexample;

import com.jcraft.jsch.*;
import java.io.*;

public class SSHexample 
{
public static void main(String[] args) 
{
    String user = "user";
    String password = "password";
    String host = "192.168.100.103";
    int port=22;
    try
    {
        JSch jsch = new JSch();
        Session session = jsch.getSession(user, host, port);
        session.setPassword(password);
        session.setConfig("StrictHostKeyChecking", "yes");
        System.out.println("Establishing Connection...");
        session.connect();
        System.out.println("Connection established.");
        System.out.println("Crating SFTP Channel.");
        ChannelSftp sftpChannel = (ChannelSftp) session.openChannel("sftp");
        sftpChannel.connect();
}catch(Exception e) {
e.printStackTrace();
}
}

推荐答案

您必须通过调用以下函数

You have to supply a KnownHostKeys file by calling following function

jsch.setKnownHosts(new FileInputStream(knownHostsFile));

此文件应具有所有已知主机的指纹,并用换行符分隔.

this file should have all the the known hosts' fingerprints separated by new lines.

例如

hostname,10.1.1.120, ssh-rsa AAAAC3NzaC1yc2EAAAADAQABAAABAQCi5b647581SwC0uUDQw1ENjKSz3rhJMRRZEgIjHylvF4fbuAEzj645YoAf9SItb51MhetFAJrq98jYsHpedSm3IoMG+aR/P1CjsBz1RtJKlfR2NfYDCZ7Dyx11P8FnJbwbYif/GeG0xEujekwF1pyL0tNPmf0H4/GPR4mwrv/llGlB3Lo3BzxrGtl4f4X/oSHDoo7FrQkDwqOfeSM++3vPPHxyVO5zhFJ5u9f7M/uuxUeHS+YS5JWAI7NLXKgbiM9dluGzZU/6Awo3ux4x5ojL+kf29JEVxK+o6GfW2bIW+LhgIGZNThnN5nHzBVfNNHvQ7KC5ic0h2z2gbVpwJr1h

您可以使用任何sftp客户端从服务器获取此密钥,但是如果您使用的是Linux或UNIX,则以下命令可能会有所帮助

you can obtain this key from server by using any sftp client however following command may help if you are using linux or unix

ssh-keyscan -t rsa 10.1.1.120

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

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