Using keep alive to notice SSH server disconnection

Erez Lidor erez at kashya.com
Sun Oct 10 18:26:16 MEST 2004


Hi all,
 
We are using the MindTerm package to connect to remote machines via SSH.
I'm trying to implement a reconnection mechanism, on top of the MT
driver. I wish to catch a disconnection (due to a remote machine
crashing, rebooting, network problems etc.), and catch it quickly
(preferably in under a minute).
I've set the connection preferences to perform keep-alive every 2
seconds, as follows:
 
 SSH2Preferences prefs = new SSH2Preferences(_props);

 prefs.setPreference(SSH2Preferences.ALIVE, "2");

 byte[] seed = RandomSeed.getSystemStateHash();

 SecureRandomAndPad secureRandom = new SecureRandomAndPad(new
SecureRandom(seed));

_transport = new SSH2Transport(new Socket(_host, 22), prefs,
secureRandom);

_client = new SSH2SimpleClient(_transport, _user, _password);

_console = new SSH2ConsoleRemote(_client.getConnection());

 if( _console.shell(true, "vt100", _ptyLines, 115) ) {

 _stdOut = _console.getStdOut();

 _stdIn = _console.getStdIn();

[I've also set my Linux machine TCP preferences for short keep alive (10
seconds rather then the default 2 hours) but it does not change the
behavior.]
Then I run a "top" command, through SSH, on the remote machine (so that
the command will not return), and shut down the remote machine. MT gets
the notification that the connection was closed after about 16 minutes!
Logging the I/O error and no exception thrown.
I'm not using the SSH2ConsoleRemote.command() API, rather than that I
send the command through the console's stream, as follows:
_stdIn.write(command.getBytes());

_stdIn.flush();

Then I wait for the prompt to return, occasionally checking the return
value of the SSH2Transport.isConnected() method.
This mechanism generally works ok - sends the command and performs it as
expected. The only problem is that identifying a disconnection takes too
long.
When I reboot the remote machine (instead of shutting it down), MT
catches the disconnection, only when the machine goes back up, logs it,
and the SSH2Transport.isConnected() method returns false, as expected.
 
Any help would be appreciated - we chose to use MT precisely because of
its support of keep-alive (unlike some of the competition...).
 
Thanks in advance,
 
Erez Lidor
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://172.23.2.180/pipermail/mindterm-users/attachments/20041010/555a759b/attachment.htm


More information about the Mindterm-users mailing list