[Feature Request] Add IsConnected() method to check session status
The existing IsConnected property does not always reliably reflect the actual state of the SSH session. For example, it may still return true even if the connection has silently dropped due to network issues or server-side timeouts.
It would be helpful to have a method like IsConnected() that actively attempts to communicate with the server (e.g., by sending a keep-alive packet or executing a lightweight command) to verify whether the session is still alive. This would improve robustness in long-running applications and allow developers to handle disconnections more gracefully.
Have you considered using Client.KeepAliveInterval?
Yes, setting this does simplify things, but there’s still a problem. For example, if the server’s timeout is 10 minutes and KeepAliveInterval is set to 20 minutes, IsConnected will still return true even after 15 minutes, even though the server has already closed the session.
I have this helper right now
private async Task<bool> IsConnectedAsync(CancellationToken cancellationToken)
{
if (!_sftpClient.IsConnected)
return false;
try
{
await _sftpClient.ExistsAsync(".", cancellationToken);
return true;
}
catch (Exception ex) when (ex is SshConnectionException or SocketException)
{
// SshConnectionException - Client not connected
// SshConnectionException - An established connection was aborted by the server
// SocketException - An existing connection was forcibly closed by the remote host
_sftpClient.Disconnect();
return false;
}
}
probably there is more effective way to do that using BaseClient methods (low level)