我正在使用 Jsch 并期望它连接到网络设备并更新配置、更改密码等...
我遇到了一个问题,即环回连接保持打开状态,这会阻止创建更多 ssh session 。我读到这是某些版本的 OpenSSH 的问题,解决方案是升级 sshd。不幸的是,这在连接到网络设备时有时不是一个选项。
没有解决办法吗?
编辑 - 这是我的代码 - 我不是手动关闭所有内容吗?
JSch jSch = new JSch();
Session session = jSch.getSession("username", h.hostname);
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.setPassword("password");
session.connect();
Channel channel = session.openChannel("shell");
Expect expect = new ExpectBuilder()
.withOutput(channel.getOutputStream())
.withInputs(channel.getInputStream(), channel.getExtInputStream())
.withEchoOutput(System.out)
.withEchoInput(System.err)
.withExceptionOnFailure()
.build();
channel.connect();
expect.expect(contains("#"));
expect.sendLine("showRules\r");
String response = expect.expect(regexp("#")).getBefore();
System.out.println("---" + response + "----");
expect.sendLine("exit\r");
expect.close();
channel.disconnect();
session.disconnect();
最佳答案
事实证明,未关闭的环回连接是由我的 IDE - IntelliJ IDEA 创建的。当我将这些类部署到 UNIX 机器并运行它时,没有挥之不去的环回连接,也没有用完它们的问题。
关于java - 如何强制关闭未关闭的 Jsch ssh 连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27847599/