JSch 创建嵌入 ssh 连接但 ServerAliveInterval 不起作用

JSch create a embed ssh connection but ServerAliveInterval not works

我想把 JSch 作为一个常用的 shell 交互工具来使用,它真的很有用。

我想创建一个内部 JSch 通道 shell 和 shell 的 InputStream 和 OutStream 由我自己维护的 ssh。

问题是当我发送命令字符串 ssh -o ServerAliveInterval=5 -o ServerAliveCountMax=2 -o StrictHostKeyChecking=no -o ConnectTimeout=10 -T root@xxx.xxx.xxx.xxx -p 22OutputStream 以通过 ServerAliveInterval 设置保持活动但它在 all.the new 时不起作用当我关闭 network.This 时,通道将保持 1 分钟,时间超过我的配置 ServerAliveInterval=5 ServerAliveCountMax=2

JSch 是否支持内部 ssh 连接设置来处理 keep-alive 检查,例如 ServerAliveInterval

更新

描述问题的一些步骤:
1. 创建一个 shell 频道使用 JSch 登录 host1
2. 使用通道跳转至host2发送命令ssh -o ServerAliveInterval=5 -o ServerAliveCountMax=2 root@xxx.xxx.xxx.xxx -p 22,这是一个字节流。新的 ssh 配置为 ServerAliveInterval 以检查 host2.

的 ssh 网络状态

如果我使用 Session.setServerAliveInterval 方法并用 Session.isConnected 测试它,JSch 可以识别 host1 断开连接。
问题是 host2 如果只有 host2 网络收支平衡,则无法检查 我在跳转到 host2 时使用 ServerAliveInterval 配置。它会保持很长时间(在我的测试中超过 1 分钟)直到得到 packet_write_wait: Connection to xxx.xxx.xxx.xxx port 22: Broken pipe。但我希望在设置 ServerAliveInterval=5 ServerAliveCountMax=2 后不到 10 秒就会收到一些错误消息。

shell 是 JSCh 的黑盒子。

JSch 不知道你在那里做了什么。

如果您想模拟 OpenSSH ServerAliveInterval 指令的功能,请使用 Session.setServerAliveInterval.

另见 JSch: How to keep the session alive and up