JSchException: UnknownHostKey

I am trying to use Jsch to establish an SSH connection in Java. I set "StrictHostKeyChecking" to "yes." I understand that the host server of the server must be hand-picked and stored in the file of the main page before the first attempt to connect to the server. How can I get a hostkey server. My code throws the following exception:

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

How can I establish a 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();
}
}
+2
source share
4 answers

KnownHostKeys,

jsch.setKnownHosts(new FileInputStream(knownHostsFile));

, .

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

sftp-, , linux unix

ssh-keyscan -t rsa 10.1.1.120
+6

. knownHost ,

:

192.168.0.1 ssh-rsa
AAAAC3NzaC1yc2EAAAADAQABAAABAQCi5b647581SwC0uUDQw1ENjKSz3rhJMRRZEgIjHylvF4fbuAEzj645YoAf9SI
tb51MhetFAJrq98jYsHpedSm3IoMG+aR/P1CjsBz1RtJKlfR2NfYDCZ7Dyx11P8FnJbwbYif
/GeG0xEujekwF1pyL0tNPmf0H4/GPR4mwrv/llGlB3Lo3BzxrGtl4f4X
/oSHDoo7FrQkDwqOfeSM++3vPPHxyVO5zhFJ5u9f7M/uuxUeHS+YS5JWAI7NLXKgbiM9dluGzZU
/6Awo3ux4x5ojL+kf29JEVxK+o6GfW2bIW+LhgIGZNThnN5nHzBVfNNHvQ7KC5ic0h2z2gbVpwJr1h

. RSA, , :

session=null;
com.jcraft.jsch.Channel channel =null;
try{
    ssh=new JSch();
    ssh.setKnownHosts("test");
    session=ssh.getSession(userTextField.getText(),ip,22);
    session.setPassword(passwordField1.getText());

    System.out.println(session.getHostKey());
    session.connect();
    channel=session.openChannel("sftp");
    channel.connect();
    ChannelSftp sftp=(ChannelSftp) channel;
    System.out.println(sftp.getHome());
    for (Object o : sftp.ls(sftp.getHome())) {
        System.out.println(((ChannelSftp.LsEntry)o).getFilename());
    }

    } catch (JSchException e1) {
        e1.printStackTrace();
        addHost(session.getHostKey().getKey());
    } catch (SftpException e1) {
        e1.printStackTrace();
    }
}

private void addHost(String key){
    try {
        FileWriter tmpwriter=new FileWriter("test",true);

            tmpwriter.append(ip + " ssh-rsa " + key+"\n");
            System.out.println(ip + " ssh-rsa " + key);

        tmpwriter.flush();
        tmpwriter.close();

    } catch (IOException e) {
        e.printStackTrace();
    }
}

session.getHostKey().getKey() - , , .

session.connect();, catch.

+1

, , , . SSH .

Eg ssh user@192.168.100.103

. ssh s hosts. , known_host

user/.ssh/known_hosts

windows C:\Users\athakur\.ssh\known_hosts. . , IP-, :

192.168.100.103 SSH-RSA AAAAB3NzaC1yc2EAAAABIwAAAQEA1UsgiLH5hjIScZlqPA4kNhPoXAX00mMv65N8qTvYd1D1M5DwWtTTcxK4w0wGKKVA7RERzWbtjPpSomJwT1LofZr + qafLdiEvhirIXVYHSWZqp6zTJW0jzk3p07ugjoHV3YUWKDzOaiFuOMslt8hD7pZv8nhOYfxBZdsVHdukYRP8MADXC0ZgOD5ZYZ0EglaQJYPG7n73PSMZWZT/oafUcx6bFiRF4QsXaguWuu6umX9gaV7VvoyMJg + kxPAKnGDFY7If61AG7vAchUUhlQ44SB1FFr25y + qeUg2NGqAxH/Z/ZAfvZ + pDv3Cd9s + KCnEIqxyxY/sPQ2zCvwf0Z9fTeQ ==

. SSH ( , /etc/ssh/ssh_host_rsa_key.pub) . SSH . MIM ( ). , .

+1

Perhaps this is no more relevant, but in my case a similar problem occurred with docker-compose.yml in a container that was created from a spring-boot application that 100% worked locally

config-service:
    image: <host>/<bla>-config-service:<version>
    hostname: config-service
    ports:
        - 3000:3000
    depends_on:
        - discovery
    environment:
        - CONSUL_HOST=discovery
        - CONSUL_PORT=<port> 
        - CONFIG_GIT_URI=git@<host>:<group>/<repository>.git
        - CONFIG_GIT_BRANCH=development
    volumes:
        - ~/.ssh/:/root/.ssh/:ro

The solution was to hack the ~ / .ssh folder.

chcon -Rt svirt_sandbox_file_t ~/.ssh

After that, I believe that the volumes are correctly mapped between the docker container and the local machine, and the described exception has passed.

0
source

Source: https://habr.com/ru/post/1621820/


All Articles