gpt4 book ai didi

java - 从模拟的 SSH 服务器(Apache MINA)收集命令响应

转载 作者:行者123 更新时间:2023-11-30 06:19:10 26 4
gpt4 key购买 nike

所以我想做的是创建单元测试来检查调用的命令(通过 ssh 连接在 shell 上)是否有正确的响应。问题是我无法阅读这些回复。关于 Apache MINA 的教程并不多,所以我想也许你们中的一些人可以帮助我。这是一个代码

@Before
public void setUpSSHd() {

sshd=SshServer.setUpDefaultServer();
sshd.setPort(22999);
sshd.setKeyPairProvider(new SimpleGeneratorHostKeyProvider("hostkey.ser"));

sshd.setPasswordAuthenticator(new PasswordAuthenticator() {

public boolean authenticate(String username, String password, ServerSession session) {
// TODO Auto-generated method stub
return true;
}

});

List<NamedFactory<KeyExchange>> keyExchangeFactories;
keyExchangeFactories = sshd.getKeyExchangeFactories();

sshd.setKeyExchangeFactories(keyExchangeFactories);

try {
sshd.start();
} catch (Exception e) {
e.printStackTrace();
}

}

@After
public void teardown() throws Exception { sshd.stop(); }

@Test
public void testCommands() throws Exception {

SshClient client = SshClient.setUpDefaultClient();
client.start();
ClientSession session = null;
try {

session = client.connect("localhost", 22999).await().getSession();
session.authPassword("none", "none").await().isSuccess();

System.out.println("Connection established");

final ClientChannel channel = session.createChannel(ClientChannel.CHANNEL_SHELL);
ByteArrayOutputStream sent = new ByteArrayOutputStream();
PipedOutputStream pipedIn = new TeePipedOutputStream(sent);
channel.setIn(new PipedInputStream(pipedIn));
ByteArrayOutputStream out = new ByteArrayOutputStream();
ByteArrayOutputStream err = new ByteArrayOutputStream();
channel.setOut(out);
channel.setErr(err);
channel.open();

pipedIn.write("dir\r\n".getBytes());
pipedIn.flush();

channel.waitFor(ClientChannel.CLOSED, 0);
channel.close(false);
client.stop();

System.out.println(out.toString());

} catch (Exception e) {

e.printStackTrace();
fail("Cannot establish a connection");

} finally {

if (session != null)
session.close(true);

}

}

现在我只是尝试打印收集到的响应。然而,每次我尝试这样做时,我都会得到空字符串。我认为 ssh 服务器配置可能有问题(它应该使用什么 shell?)。最好的情况是,如果我可以在服务器端定义自己的命令和响应,然后仅在客户端检查它

编辑:我尝试手动连接到这个模拟的 ssh 服务器,但我已经无法与::1 端口 22999 协商:未找到匹配的 key 交换方法。他们的报价:diffie-hellman-group1-sha1错误消息。

最佳答案

我建议您更新 Apache SSH。基于source repository 0.5.0 版本已有 7 年历史。

将您发布的代码与默认 JCE 提供程序和 Apache SSH 结合使用

<dependency>
<groupId>org.apache.sshd</groupId>
<artifactId>sshd-core</artifactId>
<version>0.5.0</version>
<dependency>

与 ssh 客户端的连接失败

Their offer: diffie-hellman-group1-sha1

使用更新的 Apache SSH 版本

<dependency>
<groupId>org.apache.sshd</groupId>
<artifactId>sshd-core</artifactId>
<version>1.4.0</version>
<dependency>

连接成功

关于java - 从模拟的 SSH 服务器(Apache MINA)收集命令响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48545486/

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