简体   繁体   English

如何强制未关闭的Jsch ssh连接关闭

[英]how to force unclosed Jsch ssh connections to close

I'm using Jsch and expectit to connect to network devices and update configs, change passwords, etc... 我正在使用Jsch并希望连接到网络设备并更新配置,更改密码等...

I am having a problem where loopback connections remain open which prevents more ssh sessions from being created. 我遇到一个问题,即环回连接保持打开状态,这阻止了创建更多的ssh会话。 I read that this is a problem with certain versions of OpenSSH and that the solution is to upgrade the sshd. 我读到这是某些版本的OpenSSH的问题,解决方案是升级sshd。 Unfortunately this sometimes isn't an option when connecting to network appliances. 不幸的是,有时在连接到网络设备时这不是一个选择。

Is there no workaround? 有没有解决方法?

EDIT - Here's my code - aren't I closing everything manually? 编辑-这是我的代码-我不是手动关闭所有内容吗?

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();

Here is my response to the same question asked here. 这是我对这里提出的同样问题的回答

The channel does not close itself when there is no input left. 没有输入时,通道不会自行关闭。 Try closing it yourself after you have read all data. 阅读完所有数据后,请尝试自行关闭它。

while (true) {
    while (inputStream.available() > 0) {
        int i = inputStream.read(buffer, 0, 1024);
        if (i < 0) {
            break;
        }
        //It is printing the response to console
        System.out.print(new String(buffer, 0, i));
    }
    System.out.println("done");

    channel.close();  // this closes the jsch channel

    if (channel.isClosed()) {
        System.out.println("exit-status: " + channel.getExitStatus());
        break;
    }
    try{Thread.sleep(1000);}catch(Exception ee){}
}

The only time you are going to use a loop that doesnt manually close the channel is when you have interactive keyboard input from the user. 唯一要使用不会手动关闭通道的循环是当您从用户那里获得交互式键盘输入时。 Then when the user does an 'exit' that will change the channel's 'getExitStatus'. 然后,当用户执行“退出”操作时,该操作将更改通道的“ getExitStatus”。 If your loop is while(channel.getExitStatus() == -1) then the loop will exit when the user has exited. 如果您的循环为while(channel.getExitStatus()== -1),则当用户退出时循环将退出。 You still need to disconnect the channel and session yourself after you detect an Exit Status. 在检测到退出状态后,您仍然需要断开频道并自己进行会话。

It is not listed on their example page, but JSCH hosts an interactive keyboard demo on their site. 它没有在其示例页面上列出,但是JSCH在其站点上托管了一个交互式键盘演示。 http://www.jcraft.com/jsch/examples/UserAuthKI.java http://www.jcraft.com/jsch/examples/UserAuthKI.java

Even their demo, which I used to connect to an AIX system without changing any of their code... does not close when you exit the shell! 即使是我的演示程序,我用来连接到AIX系统而不更改任何代码的演示程序,在退出Shell时也不会关闭!

I had to add the following code to get it to exit properly after I had typed 'exit' in my remote session: 在远程会话中键入“退出”后,我必须添加以下代码才能使其正确退出:

         channel.connect();

         // My added code begins here
         while (channel.getExitStatus() == -1){
            try{Thread.sleep(1000);}catch(Exception e){System.out.println(e);}
         }

         channel.disconnect();
         session.disconnect();
         // My Added code ends here

       }

   catch(Exception e){
     System.out.println(e);
   }
}

It turns out the loopback connections that weren't closing were being created by my IDE - IntelliJ IDEA. 事实证明,尚未关闭的环回连接是由我的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. 当我将类部署到UNIX计算机并运行它时,没有回荡的环回连接,也没有耗尽它们的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM