javashellsshjsch

JSch create a embed ssh connection but ServerAliveInterval not works


I want use JSch as a common shell interactive tool, it's really useful.

I want create a ssh which is inner JSch channel shell and the shell's InputStream and OutputStream is maintained by myself.

The problem is when I send a command string

ssh -o ServerAliveInterval=5 -o ServerAliveCountMax=2 -o StrictHostKeyChecking=no -o ConnectTimeout=10 -T root@xxx.xxx.xxx.xxx -p 22

to the OutputStream to setting a keep-live by ServerAliveInterval but it not works at all. The new channel will hold 1 min when I close network. This time is more over my config ServerAliveInterval=5 ServerAliveCountMax=2.

Is JSch support inner ssh connection setting to handle keep-alive check, such as ServerAliveInterval?

Some step to describe the question:

  1. create a shell channel use JSch to login host1
  2. use the channel jump to host2 by send a command ssh -o ServerAliveInterval=5 -o ServerAliveCountMax=2 root@xxx.xxx.xxx.xxx -p 22 which is a byte stream. The new ssh is config with ServerAliveInterval to check ssh network status of host2.

JSch can recognize host1 is disconnect if I'm use Session.setServerAliveInterval method and test it with Session.isConnected.
The problem is host2 can't be checked if only host2 network is break even through I'm use ServerAliveInterval config when jump to host2. It will hold a long time (more then 1min on my test) until I get

packet_write_wait: Connection to xxx.xxx.xxx.xxx port 22: Broken pipe

But I expect some error message arrived less than 10 seconds as the ServerAliveInterval=5 ServerAliveCountMax=2 was set.


Solution

  • The shell is a black box for JSCh.

    JSch is not aware of anything that you do there.

    If you want to emulate, what OpenSSH ServerAliveInterval directive does, use Session.setServerAliveInterval.

    See also JSch: How to keep the session alive and up.