使用 JSch 连接到远程服务器时获取 UnknownHostKey

Getting UnknownHostKey when connecting to remote server with JSch

我正在尝试通过 JSch 连接到 Java 中的远程服务器,但出现错误。

我有这个代码:

public static void myCode() {
    String command = "ls /home";
    String userName = "username";
    String password = "pass";
    String connectionIP = "XXX.XXX.XXX.XXX";
    int port = 0000; // example value
    SSHManager instance = new SSHManager(userName, password, connectionIP, "", port);
    String errorMessage = instance.connect();

    if (errorMessage != null) {
        System.out.println(errorMessage);
    }

    String result = instance.sendCommand(command);
    instance.close();
}

SSHManager class:

/* 
 * SSHManager
 * 
 * @author cabbott
 * @version 1.0
 */

import com.jcraft.jsch.*;
import java.io.IOException;
import java.io.InputStream;
import java.util.logging.Level;
import java.util.logging.Logger;

public class SSHManager {

    private static final Logger LOGGER
            = Logger.getLogger(SSHManager.class.getName());
    private JSch jschSSHChannel;
    private String strUserName;
    private String strConnectionIP;
    private int intConnectionPort;
    private String strPassword;
    private Session sesConnection;
    private int intTimeOut;

    private void doCommonConstructorActions(String userName,
            String password, String connectionIP, String knownHostsFileName) {
        jschSSHChannel = new JSch();

        try {
            jschSSHChannel.setKnownHosts(knownHostsFileName);
        } catch (JSchException jschX) {
            logError(jschX.getMessage());
        }

        strUserName = userName;
        strPassword = password;
        strConnectionIP = connectionIP;
    }

    public SSHManager(String userName, String password,
            String connectionIP, String knownHostsFileName) {
        doCommonConstructorActions(userName, password,
                connectionIP, knownHostsFileName);
        intConnectionPort = 22;
        intTimeOut = 60000;
    }

    public SSHManager(String userName, String password, String connectionIP,
            String knownHostsFileName, int connectionPort) {
        doCommonConstructorActions(userName, password, connectionIP,
                knownHostsFileName);
        intConnectionPort = connectionPort;
        intTimeOut = 60000;
    }

    public SSHManager(String userName, String password, String connectionIP,
            String knownHostsFileName, int connectionPort, int timeOutMilliseconds) {
        doCommonConstructorActions(userName, password, connectionIP,
                knownHostsFileName);
        intConnectionPort = connectionPort;
        intTimeOut = timeOutMilliseconds;
    }

    public String connect() {
        String errorMessage = null;

        try {
            sesConnection = jschSSHChannel.getSession(strUserName,
                    strConnectionIP, intConnectionPort);
            sesConnection.setPassword(strPassword);
        // UNCOMMENT THIS FOR TESTING PURPOSES, BUT DO NOT USE IN PRODUCTION
            // sesConnection.setConfig("StrictHostKeyChecking", "no");
            sesConnection.connect(intTimeOut);
        } catch (JSchException jschX) {
            errorMessage = jschX.getMessage();
        }

        return errorMessage;
    }

    private String logError(String errorMessage) {
        if (errorMessage != null) {
            LOGGER.log(Level.SEVERE, "{0}:{1} - {2}",
                    new Object[]{strConnectionIP, intConnectionPort, errorMessage});
        }

        return errorMessage;
    }

    private String logWarning(String warnMessage) {
        if (warnMessage != null) {
            LOGGER.log(Level.WARNING, "{0}:{1} - {2}",
                    new Object[]{strConnectionIP, intConnectionPort, warnMessage});
        }

        return warnMessage;
    }

    public String sendCommand(String command) {
        StringBuilder outputBuffer = new StringBuilder();

        try {
            Channel channel = sesConnection.openChannel("exec");
            ((ChannelExec) channel).setCommand(command);
            InputStream commandOutput = channel.getInputStream();
            channel.connect();
            int readByte = commandOutput.read();

            while (readByte != 0xffffffff) {
                outputBuffer.append((char) readByte);
                readByte = commandOutput.read();
            }

            channel.disconnect();
        } catch (IOException ioX) {
            logWarning(ioX.getMessage());
            return null;
        } catch (JSchException jschX) {
            logWarning(jschX.getMessage());
            return null;
        }

        return outputBuffer.toString();
    }

    public void close() {
        sesConnection.disconnect();
    }

}

当我 运行 它时,我得到这个错误:

Jul 31, 2015 2:56:44 PM javaapplication2.SSHManager logError
SEVERE: null:0 - java.io.FileNotFoundException
UnknownHostKey: XXX.XXX.XXX.XXX. RSA key fingerprint is xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx
Jul 31, 2015 2:56:45 PM javaapplication2.SSHManager logWarning
WARNING: XXX.XXX.XXX.XXX:0.000 - session is down

我找到了这个答案:

看完后我决定删除~/.ssh/known_hosts,然后在终端通过ssh登录服务器。它告诉我已知的主机文件已更新,但此代码出现相同的错误。

另一件我觉得很奇怪的事情是错误的最后一行说我在插入 0000[=35 时插入了端口 0.000 =].

有谁知道哪里出错了?

默认情况下,JSch 应该能够解析来自您使用 ssh.

设置的 OpenSSH known_hosts 文件的已知主机密钥

但是当您使用 路径(因此 FileNotFoundException)显式调用 JSch.setKnownHosts 时,您破坏了它(因此 UnknownHostKey ).