SshConnectionException from SshNet in C#

Viewed 7567

From my script trying to connect to the unix server to download a file but getting below error..

Renci.SshNet.Common.SshConnectionException : Client not connected.

I can connect properly to that server from WinScp by using the same credentials.

Not sure what's going wrong here. Any idea/pointer ?

Code

using (var client = new ScpClient(Config.UnixServer, Config.UnixUsername, Config.UnixPassword))
{
    client.Connect();
    client.Upload(new FileInfo(fileUpload), fileName);
    client.Disconnect();
}

Error

Renci.SshNet.Common.SshConnectionException : Client not connected.
at Renci.SshNet.Session.WaitOnHandle(WaitHandle waitHandle)
at Renci.SshNet.Session.Connect()
at Renci.SshNet.BaseClient.Connect()

WinSCP Session Log

4 Answers

The session log shows that WinSCP is using the sftp protocol (WinSCP supports both scp and sftp protocols). Not all sftp servers will accept scp connections. Switch to the SftpClient class and use the UploadFile method.

I also suspect you meant to call OpenRead() on your FileInfo instance to get a stream.

using (var client = new SftpClient(Config.UnixServer, Config.UnixUsername, Config.UnixPassword))
{
    client.Connect();
    client.UploadFile(new FileInfo(fileUpload).OpenRead(), fileName);
    client.Disconnect();
}

Maybe that helps?

Maybe something to do with unix paths? Check client.RemotePathTransformation = RemotePathTransformation.ShellQuote;

Have quick read here

By default, SSH.NET applies a simple quoting mechanism for remote paths in ScpClient. More precisely, any remote path is enclosed in double quotes before it is passed to the scp command on the remote host.

Perhaps the trust isn't there? In the original logs, OP has this: 2017-09-14 09:10:17.495 Host key matches cached key.

The github page: https://github.com/sshnet/SSH.NET has information about how to establish an expected fingerprint.

The other issue I run into is when a server has a white list of IP addresses and the server is on it, but my test environment is not.

@Lee, the log from the application performing the connection is most helpful in figuring out these kinds of things, as Martin has pointed out.

Update the Renci reference lib will solved the issues.

Related