SSH/SFTP Error: Must first connect to the SSH server.
The error shown below is explained in this post:
ChilkatLog:
DownloadFileByName:
DllDate: Apr 25 2018
ChilkatVersion: *
UnlockPrefix: *
Architecture: Little Endian; 64-bit
Language: *
VerboseLogging: 0
SftpVersion: 3
Must first connect to the SSH server.
--DownloadFileByName
--ChilkatLog
This error can occur after a long period of inactivity.
For example, your application may have successfully connected to the SFTP server, authenticated, and completed some work. If the connection then sits idle for too long, the SFTP server may close it due to inactivity. Your application does not necessarily know this happened immediately. It usually discovers the disconnect only when it tries to perform another operation, such as calling DownloadFileByName.
At that point, Chilkat attempts to send the next message over the connection, discovers that the socket is no longer connected, and returns the error shown above.
There are two common ways an application can handle this:
- Prevent the disconnect
Periodically call
sftp.SendIgnoreto keep the connection active and prevent the server from timing it out due to inactivity. - Automatically recover if disconnected
If
DownloadFileByNameor another SFTP method fails, check thesftp.IsConnectedproperty. If it indicates the connection has been lost, your application can automatically:- reconnect to the SSH server,
- re-authenticate,
- call
InitializeSftpagain, - and then retry the original operation.
This second approach is often a good way to make an application more robust when idle timeouts are possible.