4

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

14
  • Are youe Config settings set correctly? Commented Sep 13, 2017 at 5:26
  • Yes, I debug the code and getting the values (server, username, password) correctly. Commented Sep 13, 2017 at 5:28
  • 1
    winscp.net/eng/docs/ui_pref_logging Commented Sep 13, 2017 at 6:47
  • 1
    exactly the same problem. No new topic needed. Commented Feb 21, 2019 at 9:01
  • 1
    So do you actually know that the answer is "SFTP", but you just want to get this posted as an answer? Commented Feb 28, 2019 at 6:42

4 Answers 4

3

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();
}
Sign up to request clarification or add additional context in comments.

Comments

0

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.

1 Comment

The problems happens while connecting, long before any paths are involved.
0

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.

1 Comment

SSH.NET does does bother with hostkey verification.
0

Update the Renci reference lib will solved the issues.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.