I have an SSH Client with which I have a log in both the BeforeConnect and AfterConnect (as well as BeforeDisconnect and AfterDisconnect) so I can see what is happening during some connection testing.
I have seen a few odd logs where there is a Connecting message (from the BeforeConnect) but no following Connected message (from the AfterConnect). I have the connection attempt in a try except so if the connection fails I also get a log of the failure as in the except I log any exception.
So I should see a Connecting then a Connected or a Connecting then a Failure. However what I see is the Connecting message and then nothing else until I manually restart the connection. It is as if the connection just hangs and never moves on. What could cause this?
Just to be clear this happens maybe once in a 72 hour period and it is very likely it is happening when there is a connection problem on the network (a link drop or similar), in fact the last one that presented this behaviour was definately during a network connection problem and when I got back on to the server I saw just the Connecting with no subsequent Failure message.
I should also add that after the Connect line in Delphi I have an If statement to check that the SSHClient.Connected is true and if not then I repeat until there is a connection. In the repeat loop I log the failure, sleep for 5 minutes, connect again. Nothing from this loop happened in over 15 minutes, so it really looks like the Connecting happened then the program completely hung at this point, with no exception (at least none logged) and I am not sure what could cause it to get in to this state.
To give you some code;
Code: Select all
with SSHClient do begin KeyStorage := keyStore; Options.ServerAliveCountMax := 3; Options.ServerAliveInterval := 18; Options.TCPKeepAlive := true; Timeout := 90; OnServerKeyValidate := ServerKeyValidate; BeforeConnect := SSHConnectorBeforeConnect; BeforeDisconnect := SSHConnectorBeforeDisconnect; AfterConnect := SSHConnectorAfterConnect; AfterDisconnect := SSHConnectorAfterDisconnect; end; procedure SSHClientStart; begin try with SSHClient do begin Hostname := SSHHostname; Port := SSHPort; User := SSHUsername; if SSHType = 0 then Authentication := atPassword; if SSHType = 1 then Authentication := atPublicKey; Password := SSHPasswd; end; with SSHChannel do begin Client := SSHClient; DestHost := ChannelHost; DestPort := ChannelDPort; SourcePort := ChannelSPort; end; SSHClient.Connect; // The program just seems to hang here. if SSHClient.Connected then begin SSHChannel.Connect; end else begin repeat Log('Connection Failure'); // The Log procedure writes a line to a local MySQL database table. I never see this log. sleep(300000); SSHClient.Connect; until (SSHClient.Connected); SSHChannel.Connect; end; except on E: Exception do Log(e.ClassName+e.Message); // I never see this log. end; end; procedure SSHConnectorBeforeConnect(Sender: TObject); begin Log(' Connecting...'); // When this issue occurs I do not see multiple of this log, so the repeat until loop has not been entered. end; procedure SSHConnectorAfterConnect(Sender: TObject); begin Log(' Connected.'); // When this issue occurs I do not see this log. end;