Reputation: 569
I've seen other threads about this error, but I am having this error randomly. Out of 30 connects, 12 got this error. Trying to understand why this is, and what possible solutions are.
using (SftpClient client = new SftpClient(sftpHost, sftpPort, sftpUser, sftpPassword))
{
client.Connect();
}
throws this exception:
Renci.SshNet.Common.SshConnectionException: Server response does not contain SSH protocol identification
Upvotes: 12
Views: 15899
Reputation: 1033
I know this is an old thread, but to help anyone else who stumbles on it Like I did, I had this problem when connecting to Ubiquiti radios. The problem turned out to be that I had many ssh sessions to the radio running at once due to a bug in my code. Either the client or server was resource starved I think. Fixing the bug in my code made the error go away.
Upvotes: 1
Reputation: 33437
This might sound trivial, but I have tried the Renci.SshNet.Async package and it worked with out any issue. The following could be a reason/s for the problem with solution or alternative solution.
Here examples provided by WinSCP https://winscp.net/eng/docs/library_examples.
My Test example is:
This is just playground example to see if things are running, when used in production, please modify it. Further more thanks to @MartinPrikry adding this note: If you have set
SshHostKeyFingerprint
correctly, then do not setGiveUpSecurityAndAcceptAnySshHostKey
.
public static int WinSCPListDirectory()
{
try
{
var sessionOptions = new SessionOptions
{
Protocol = Protocol.Sftp,
HostName = "xxx.xxx.xxx.xxx",
UserName = "username",
Password = "password",
SshHostKeyFingerprint = "ssh-rsa 2048 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx",
GiveUpSecurityAndAcceptAnySshHostKey = true
};
using (var session = new Session())
{
session.Open(sessionOptions);
var directory = session.ListDirectory("/var/www");
foreach (RemoteFileInfo fileInfo in directory.Files)
{
Console.WriteLine(
"{0} with size {1}, permissions {2} and last modification at {3}",
fileInfo.Name, fileInfo.Length, fileInfo.FilePermissions,
fileInfo.LastWriteTime);
}
}
return 0;
}
catch (Exception e)
{
Console.WriteLine("Error: {0}", e);
return 1;
}
}
This example should return a list of directories
Related links:
Upvotes: 10
Reputation: 10927
No one knows the exact cause for this behavior to be honest.
As a possible fix, Some people suggest creating a counter loop for retrying to connect, suggesting it solved their problem:
int attempts = 0;
do
{
try
{
client.Connect();
}
catch (Renci.SshNet.Common.SshConnectionException e)
{
attempts++;
}
} while (attempts < _connectiontRetryAttempts && !client.IsConnected);
Another suggestion is to add the IP address into the hosts.allow
file on the server, so you could check in that direction as well.
As I said, there is no possible explanation for the behavior altogether.
Upvotes: 1