0 votes
ago by (620 points)

I am getting the exception "connection closed by the server .,make sure you are connecting to an SSH or SFTP server" while connecting to SFTP. It was working few days back without any issue. We are using Rebex 6.0.8372.0. Publc and Private Key pair was created and public key was shared to CB for whuitelisted. Afterwards with Username, pasword, private key path and password we are connecting to CB. please advise on the below asap
Below is th code we are using

string SFTPHost = Convert.ToString(ConfigurationSettings.AppSettings["SFTPHost"]);
string SFTPPort = Convert.ToString(ConfigurationSettings.AppSettings["SFTPPort"]);
string SFTPUserName = Convert.ToString(ConfigurationSettings.AppSettings["SFTPUserName"]);
string SFTPPassword = Convert.ToString(ConfigurationSettings.AppSettings["SFTPPassword"]);
string SFTPPvtKeyPath = Convert.ToString(ConfigurationSettings.AppSettings["SFTPPvtKeyPath"]);
string SFTPPvtKeyPwd = Convert.ToString(ConfigurationSettings.AppSettings["SFTPPvtKeyPwd"]);
//Updated by Alex on 31.01.2022 for SFTP Authentication with Private Key without loading PFX File End

IsActive = objNewSFTP.GetConnectionState();             

WriteLog("ConnectSftp()-SFTP Connection State:" + objNewSFTP.GetConnectionState().Connected, false);
if (IsActive.Connected == false)
{

                WriteLog("ConnectSftp()-SFTP Connectivity Attempted with SFTPHost:" + Convert.ToString(SFTPHost) + Environment.NewLine + "Port:" + Convert.ToInt32(SFTPPort) + "at " + DateTime.Now.ToString(), false);   //Added by Alex on 31.1.2022

                objNewSFTP.Connect(SFTPHost, Convert.ToInt32(SFTPPort));

                WriteLog("ConnectSftp()-SFTP Connectivity Done Successfully at " + DateTime.Now.ToString(), false);   //Added by Alex on 31.1.2022

                //Added  by Alex on 31.01.2022 for SFTP Authentication with Private Key without loading PFX File Start
                WriteLog("ConnectSftp()-Private Key Details before loading-Private Key Path:" + Convert.ToString(SFTPPvtKeyPath) + Environment.NewLine + "at " + DateTime.Now.ToString(), false);

                SshPrivateKey privateKey = new SshPrivateKey(Convert.ToString(SFTPPvtKeyPath), Convert.ToString(SFTPPvtKeyPwd));

                WriteLog("ConnectSftp()-Private Key Details loaded successfully at " + DateTime.Now.ToString(), false);

                WriteLog("ConnectSftp()-SFTP Authentication Details before Login-UserName:" + Convert.ToString(SFTPUserName) + Environment.NewLine + "at " + DateTime.Now.ToString(), false);
                objNewSFTP.Login(Convert.ToString(SFTPUserName), Convert.ToString(SFTPPassword), privateKey);
                //Added  by Alex on 31.01.2022 for SFTP Authentication with Private Key without loading PFX File End

                WriteLog("ConnectSftp()-Completed SFTP Authentication Process at " + DateTime.Now.ToString(), false);
}
Applies to: Rebex SFTP

1 Answer

0 votes
ago by (149k points)

The "Connection was closed by the Server. Make sure you are connecting to SSH or SFTP Server." error occurs in Rebex SFTP's Connect method when the server does not respond with a proper SSH server identification string that starts with "SSH-2.0-". Typically, this happens when trying to connect to a server that is not using SFTP/SSH. However, it can also occur if the server accepts the connection but then does not start SSH negotiation and closes the connection instead. It's quite possible this is caused by misconfigured whitelisting.

What message do you receive when you connect to the server from the command line using the 'telnet' command? Does it start with "SSH-2.0-"? Make sure you are connecting from the same machine where Rebex SFTP runs.

...