【问题标题】:Jsch java ssh client is not getting disconnected after command gets executed执行命令后,Jsch java ssh客户端没有断开连接
【发布时间】:2022-05-04 11:00:34
【问题描述】:

我正在使用 java ssh 客户端 (http://www.jcraft.com/jsch/) 连接到远程机器并执行命令。 代码工作正常,直到我连接到远程机器并执行命令。但是,问题是,即使命令执行成功,通道和会话也没有断开。

我也调用了 session.disconnect 和 channel.disconnect,但问题仍然存在。

这是我的代码:

    JSch jsch = new JSch();
    String host = null;
    host = "192.168.102.211";
    String privateKey = "C:\\test\\key";
    String cmd = "a";
    String command = "b";

    jsch.addIdentity(privateKey);
    Session session = jsch.getSession("user1", host, 22);
    Channel channel = session.openChannel("shell");

    UserInfo ui = new MyUserInfo() {
        public boolean promptYesNo(String message) {
            return true;
        }
    };
    session.setUserInfo(ui);
    session.connect(30000);

    OutputStream ops = channel.getOutputStream();
    PrintStream ps = new PrintStream(ops, true);

    channel.connect();
    ps.println(cmd);
    ps.println(command);
    ps.close();

    InputStream in = channel.getInputStream();
    byte[] bt = new byte[1024];
        while (in.available() > 0) {
        //  int i = in.read(bt, 0, 1024);
            for (int i = in.read(); i >=0; i--)
                    {
            String str = new String(bt, 0, i);
            System.out.print(str);
                    }
            break;

    }

 if (channel != null) {
        channel.disconnect();
        session.disconnect();
        System.out.println(channel.isConnected());
    }
}

请推荐

【问题讨论】:

    标签: java ssh


    【解决方案1】:

    使用相关代码更新您的问题。 只是一个提示,你应该做类似的事情。

    } finally {
        if (channel != null) {
            Session session = channel.getSession();
            channel.disconnect();
            session.disconnect();
            System.out.println(channel.isConnected());
        }
    }
    

    【讨论】:

    • 添加 finally 块后,它仍然无法正常工作。我不知道为什么 while 循环没有被终止 ....
    • 我遇到了同样的问题,即使会话断开连接,程序也会继续运行。有什么想法吗?
    • 检查 channel.isClosed() 状态。如果通道关闭,则可以断开通道。我在回答中添加了示例代码,请检查。
    【解决方案2】:

    我有类似的错误,我将代码更改为那个
    首先我声明了 sessionchannel

    static Session session = null;
    static Channel channel = null;
    

    这是连接代码

    public static OutputStream puttyConnection(String user, String hostName, String password)
            throws JSchException, IOException {
        session = new JSch().getSession(user, hostName, 22);
        session.setPassword(password);
        java.util.Properties config = new java.util.Properties();
        config.put("StrictHostKeyChecking", "no");
        session.setConfig(config);
        session.connect();
        channel = session.openChannel("shell");
        OutputStream ops = channel.getOutputStream();
        channel.connect();
        return ops;
    }
    

    在运行代码的最后,我会像这样断开连接

      if (channel != null) {
                    channel.disconnect();
                    session.disconnect();
                    System.out.println(channel.isConnected());
            }
    

    【讨论】:

      【解决方案3】:

      我遇到了类似的问题,意识到只有关闭通道才能断开连接。所以我使用while循环检查通道是否关闭,然后我可以毫无问题地断开通道。

             while (true){
                  if(channel.isClosed()){
                      channel.disconnect();
                      break;
                  }
                  try {
                      Thread.sleep(2000);
                  } catch (InterruptedException e) {
                      throw new RuntimeException(e);
                  }
              }
      

      【讨论】:

        猜你喜欢
        • 2022-11-15
        • 2015-06-29
        • 2018-07-12
        • 1970-01-01
        • 2021-01-29
        • 2017-05-23
        • 1970-01-01
        • 2014-09-02
        • 2010-11-01
        相关资源
        最近更新 更多