需要通过jumphost SSH到目标主机。曾尝试JSch JumpHosts example中提到的相同内容。
Session[] sessions = new Session[2];
Session session = null;
sessions[0] = session = jsch.getSession(getUserName(), "jumphost1.com", 22);
session.setPassword(getHostPassword());
UserInfo userInfo = new UserInfo();
userInfo.setPassword(getHostPassword());
session.setUserInfo(userInfo);
Properties prop = new Properties();
prop.put("StrictHostKeyChecking", "no");
prop.put("PreferredAuthentications", "publickey,keyboard-interactive,password");
session.setConfig(prop);
session.connect();
String host = "host1.com";
int assignedPort = session.setPortForwardingL(0, host, 22);
LOGGER.info("Jump host the {} of agent {} and port forwarding {}", i, host, assignedPort);
sessions[i] = session = jsch.getSession(getUserName(), "127.0.0.1", assignedPort);
session.setPassword(getHostPassword());
userInfo = new UserInfo();
userInfo.setPassword(getHostPassword());
session.setUserInfo(userInfo);
session.setHostKeyAlias(host);
session.connect();
连接目标主机时获得以下异常:
Caused by: com.jcraft.jsch.JSchException: reject HostKey: 127.0.0.1
at com.jcraft.jsch.Session.checkHost(Session.java:799)
at com.jcraft.jsch.Session.connect(Session.java:345)
at com.jcraft.jsch.Session.connect(Session.java:183)
我正尝试通过host1.com
jumphost1.com
jumphost1.com
host1.com
host1
答案 0 :(得分:4)
您通过jumphost连接的代码是正确的。
唯一的问题是您的本地主机密钥存储库包含第二台主机的不同主机密钥,而不是您从真实(第二台)主机接收的主机密钥。
你实际上似乎不关心安全性,因为你为jumphost会话设置了StrictHostKeyChecking=no
(official example正确地做了什么!)。但是你没有为第二个会话做同样的事情,因此错误。
另见How to resolve Java UnknownHostKey, while using JSch SFTP library?