5
votes

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

3
Show us WinSCP session log file.Martin Prikryl
Did you solve your original question? If you did please post the solution.Martin Prikryl
For your new problem, ask a new question. This is Q&A site, not a discussion forum.Martin Prikryl
@MartinPrikryl - please see this winscp.net/forum/viewtopic.php?t=8388 also winscp.net/eng/docs/faq_passphrase, my doubt is for the solution given in the 2nd link.Pranay Deep
@MartinPrikryl - please upvote my answer if you find it correct. thank you.Pranay Deep

3 Answers

1
votes

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.

1
votes

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"
};
1
votes

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.