我正在尝试实现一个基于 MINA sshd 通过 ssh 跳转与终端服务器通信的 ssh 客户端。
根据文档(https://github.com/apache/mina-sshd/blob/master/docs/internals.md#ssh-jumps)和互联网上找到的其他信息,我的代码看起来像这样。
SshClient client = SshClient.setUpDefaultClient();
client.setHostConfigEntryResolver(
HostConfigEntry.toHostConfigEntryResolver(
Arrays.asList(
new HostConfigEntry("server", "end-server", 22, "user1", "proxy"),
new HostConfigEntry("proxy", "proxy-server", 22, "user2"))));
client.start();
try (ClientSession session = client.connect("server").verify(defaultTimeout).getSession()) {
session.addPasswordIdentity("password1"); // password for the end-server
session.auth().verify(defaultTimeout);
try (ByteArrayOutputStream responseStream = new ByteArrayOutputStream();
ByteArrayOutputStream errorStream = new ByteArrayOutputStream();
ClientChannel channel = session.createExecChannel("pwd"))
{
channel.setOut(responseStream);
channel.setErr(errorStream);
try {
channel.open().verify(defaultTimeout);
try (OutputStream pipedIn = channel.getInvertedIn()) {
pipedIn.write("dir".getBytes());
pipedIn.flush();
}
channel.waitFor(EnumSet.of(ClientChannelEvent.CLOSED), defaultTimeout);
String error = errorStream.toString();
if (!error.isEmpty()) {
throw new Exception(error);
}
System.out.println(responseStream);
} finally {
channel.close(false);
}
}
}
如果对代理的身份验证是通过密钥身份验证并且终端服务器使用密码身份验证,则此实现工作正常。
问题是,实际上我使用的跳转服务器仅提供密码身份验证,而我找不到提供其凭据的方法。
如何提供跳转服务器凭据?
谢谢!