gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 11:16:39 25 4
gpt4 key购买 nike

我尝试通过 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 类:

/* 
* 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

我找到了这个答案:
https://stackoverflow.com/a/2003460/2699664

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

另一件我觉得奇怪的事情是,错误的最后一行表明我在插入 0000 时插入了端口 0.000

有谁知道哪里可能出错吗?

最佳答案

默认情况下,JSch 应该能够解析您使用 ssh 设置的 OpenSSH known_hosts 文件中的已知主机 key 。

但是当您使用路径显式调用JSch.setKnownHosts(因此出现FileNotFoundException)时,您会破坏它(因此 >未知主机 key )。

关于java - 使用 JSch 连接到远程服务器时获取 UnknownHostKey,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31748240/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com