如何在Windows机器中生成known_host文件

Kar*_*hik 5 ssh jsch ssh-keys

我正在使用 Jsch(Jcraft) 库与 SSH 服务器建立 SSH 连接,如下所示:

        JSch jsch = new JSch();
        String user = "****";
        String host = "****";
        int port = 22;
        String privateKey = "***.ppk";//Path to private key(The file is in .ppk format)
        try 
        {
            jsch.addIdentity(privateKey);
            Session session = jsch.getSession(user, host, port);
            java.util.Properties config = new java.util.Properties(); 
            config.put("StrictHostKeyChecking", "no");
            session.setConfig(config);
            session.connect();
            ChannelSftp sftpChannel = (ChannelSftp) session.openChannel("sftp");
            sftpChannel.connect();
            /*file transfer code*/
            sftpChannel.disconnect();
            session.disconnect();
        }
        catch (Exception e) 
        {
            e.printStackTrace();
        }
Run Code Online (Sandbox Code Playgroud)

由于“StrictHostKeyChecking”被禁用,SSH 连接已成功建立。如果启用它,我会收到以下错误:

com.jcraft.jsch.JSchException: UnknownHostKey: ******. RSA key f
 is *************
        at com.jcraft.jsch.Session.checkHost(Session.java:805)
        at com.jcraft.jsch.Session.connect(Session.java:345)
        at com.jcraft.jsch.Session.connect(Session.java:183)
Run Code Online (Sandbox Code Playgroud)

我知道我们需要在代码中设置已知的主机文件,如下所示:

jsch.setKnownHosts(knownHostsFileName); 
Run Code Online (Sandbox Code Playgroud)

我无法通过以下命令生成known_hosts 文件:

ssh-keyscan <HOST> > known_hosts 
Run Code Online (Sandbox Code Playgroud)

它抛出以下错误:

'ssh-keyscan' is not recognized as an internal or external command,
operable program or batch file.
Run Code Online (Sandbox Code Playgroud)

我只有 .ppk 格式的公钥和私钥。我没有known_host 文件。
我们如何创建known_host文件?
为什么ssh-keyscan命令会抛出错误 - 未识别为内部/外部命令?

Con*_* Ma 1

该文件通常由ssh-keyscanSSH 主机创建,或者由用户连接到 SSH 主机创建。

您可以简单地通过命令创建它

ssh-keyscan [host]
Run Code Online (Sandbox Code Playgroud)

并保存输出。该-H选项启用散列输出,但我不知道相关库是否可以使用它。

请注意,known_hosts如果没有首先验证主机指纹,该文件本身并不是很有用。以下警告直接来自ssh-keyscan手册页:

SECURITY
     If an ssh_known_hosts file is constructed using ssh-keyscan without veri-
     fying the keys, users will be vulnerable to man in the middle attacks.
     On the other hand, if the security model allows such a risk, ssh-keyscan
     can help in the detection of tampered keyfiles or man in the middle
     attacks which have begun after the ssh_known_hosts file was created
Run Code Online (Sandbox Code Playgroud)