5

While accessing my remote server, from SFTP, I am constantly getting this error

Connection has been unexpectedly closed. Server sent command exit status 0

I have filled same credentials in WinSCP, it is working fine. Where am I lacking? Also, instead of .ppk file I am using "ssh-rsa 1024 #######################" in my keyfile column in my project's UI.

Thank You,
Pranay

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
Pranay Deep
  • 1,371
  • 4
  • 24
  • 44

3 Answers3

1

For the problem above, I found that while attempting to "open the session" i.e session.Open(sessionOptions), it will through the exception as the server was not authenticating it.

As my task was to "password less winscp login", i.e i must have to provide:- 1.".ppk" file that is "puTTy private key". 2. Its "ssh key fingerprint".

After all day debugging, finally found that my version of winsscp.dll was old thus was not providing me the [metadata] inbuild properties as 1. SshPrivateKeyPath -> location of our ".ppk" file. 2. SshHostKeyFingerprint 3. passphrase -> only for one time login.

By updating the new version and above now I am able to open session, without any error.

Thank you.

Pranay Deep
  • 1,371
  • 4
  • 24
  • 44
1

I've also faced the same problem , when i first tried to execute the code mentioned at the below link.

https://winscp.net/eng/docs/library#example

Googled it a lot but i couldn't find any solution regarding the same.

Finally the following i relalized that i'm missing the portnumber in the example

Adding the port number field to my code solved the issue.

SessionOptions sessionOptions = new SessionOptions
{
    Protocol = Protocol.Sftp,
    HostName = "11.22.33.44",
    PortNumber = 2222, /* This is cause of the issue i was facing*/
    UserName = "abcdef",
    Password = "ghijklmnop",
    SshHostKeyFingerprint = "ssh-rsa 2048 aa:bb:cc:dd:ee:ff:gg:hh:ii:jj:kk:ll:mm:nn:oo:pp"
};
davmos
  • 9,324
  • 4
  • 40
  • 43
Venkataramana Madugula
  • 13,199
  • 2
  • 18
  • 22
1

If you can connect with WinSCP GUI, but not with .NET assembly, when running both in the same environment (computer), you have most probably missed some settings (like a port number or a host key fingerprint, as the other answers show).

The easiest solution is to have WinSCP GUI generate a code template for you based on its working settings.

enter image description here


If you are running the code on a different machine, check WinSCP FAQ Why does WinSCP not work in a new environment (operating system, machine, user account, network), when it works for me in a different environment already?

While the article is not explicitly about .NET assembly, but about WinSCP in general, it covers issues that you can face with the assembly too.

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992