如何强制关闭未关闭的Jsch ssh连接 [英] how to force unclosed Jsch ssh connections to close

查看:683
本文介绍了如何强制关闭未关闭的Jsch ssh连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Jsch,希望能连接到网络设备并更新配置,更改密码等...

I'm using Jsch and expectit to connect to network devices and update configs, change passwords, etc...

我遇到一个问题,即环回连接保持打开状态,这阻止了创建更多的ssh会话.我读到这是某些版本的OpenSSH的问题,解决方案是升级sshd.不幸的是,在连接到网络设备时,有时这不是一个选择.

I am having a problem where loopback connections remain open which prevents more ssh sessions from being created. I read that this is a problem with certain versions of OpenSSH and that the solution is to upgrade the sshd. Unfortunately this sometimes isn't an option when connecting to network appliances.

没有解决方法吗?

编辑-这是我的代码-我不是手动关闭所有内容吗?

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计算机并运行它时,没有回荡的环回连接,也没有耗尽它们的问题.

It turns out the loopback connections that weren't closing were being created by my IDE - IntelliJ IDEA. When I deployed the classes to a UNIX machine and ran it, there were no lingering loopback connections and no issues with running out of them.

这篇关于如何强制关闭未关闭的Jsch ssh连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆