Connection has been unexpectedly closed. Server sent command



Connection has been unexpectedly closed. Server sent command

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

Hi Friends,

I am able to connect to a FTP server using WinSCP GUI, but same is not working through C# code.
I am using "Server name", "Username" and "PrivateKeyFile" (Note: No password)

But when trying to connect through C# script throwing an error message stated above.
my script something like:

SessionOptions sessionOptions = new SessionOptions
Protocol = Protocol.Sftp,
HostName = (string) Dts.Variables["User::HostName"].Value,
UserName = (string) Dts.Variables["User::UserName"].Value,
//Password = (string) Dts.Variables["User::Password"].Value,
PortNumber = 22,
SshHostKeyFingerprint = "ssh-rsa 2048 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx"


using (Session session = new Session())
session.ExecutablePath = @"E:\Program Files (x86)\WinSCP\winscp.exe";

// Connect

// Upload files
TransferOptions transferOptions = new TransferOptions();
transferOptions.TransferMode = TransferMode.Binary;
MessageBox.Show("transfer mode set");

TransferOperationResult transferResult;
transferResult = session.PutFiles(@"" + localPath + "\\" + extractfilename + ".txt", "/home/Vulcan/", false, transferOptions);

// Throw on any error
MessageBox.Show("successful upload");

// Print results
bool fireAgain = false;
foreach (TransferEventArgs transfer in transferResult.Transfers)
Dts.Events.FireInformation(0, null,
string.Format("Upload of {0} succeeded", transfer.FileName),
null, 0, ref fireAgain);

I enable logging in WinSCP GUI and having information(below), may this helps.

Session name: (Modified stored session)
Host name: (Port: 22)
User name: user_xyz (Password: No, Key file: Yes)
Tunnel: No
Transfer Protocol: SFTP (SCP)
Ping type: -, Ping interval: 30 sec; Timeout: 15 sec
Proxy: none
SSH protocol version: 2; Compression: No
Bypass authentication: No
Try agent: Yes; Agent forwarding: No; TIS/CryptoCard: No; KI: Yes; GSSAPI: No
Ciphers: aes,blowfish,3des,WARN,arcfour,des; Ssh2DES: No
SSH Bugs: A,A,A,A,A,A,A,A,A,A
Return code variable: Autodetect; Lookup user groups: A
Shell: default
EOL: 0, UTF: 2
Clear aliases: Yes, Unset nat.vars: Yes, Resolve symlinks: Yes
LS: ls -la, Ign LS warn: Yes, Scp1 Comp: No
SFTP Bugs: A,A
SFTP Server: default
Local directory: default, Remote directory: home, Update: Yes, Cache: Yes
Cache directory changes: Yes, Permanent: Yes
DST mode: 1; Timezone offset: 0h 0m
Looking up host ""
Connecting to port 22
Server version: SSH-2.0-OpenSSH_5.3
Using SSH protocol version 2
We claim version: SSH-2.0-WinSCP_release_5.1.5
Doing Diffie-Hellman group exchange
Doing Diffie-Hellman key exchange with hash SHA-1
Verifying host key rsa2 0x23,0xc5f193314968ce17c4855eef3556694a0326b1c44f779773cf99b85a6732da8e805bde7e521d6dd850be0561185672a836d17439250392d53f7bc8601caa2a25e3d31f575c4c9f6c589a8dfdee0e5f7c4ec6956d5fc27d7a9bea1184168808bce2e700a96d217787779c99019502dbe5d9fff38c9b73390bfbae0ce54c31045a950db864ed147e728e483a635a3c90f4c4ab2e558e6e0cb6fd88b45a2eb0ed41e8d60de8a6759bc76dcf2b2fdb0ff3797377664434f1b75cf760e5c8db1770f38f762f78919f6d59e68eff12847b94afa904a79bcbebd41bc312270c2b516c6c0efc2bcd6af6f72a614826e81002907ca31a8131df6494bad8af8ad9affe20d3 with fingerprint ssh-rsa 2048 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx
Host key matches cached key
Host key fingerprint is:
ssh-rsa 2048 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx
Initialised AES-256 SDCTR client->server encryption
Initialised HMAC-SHA1 client->server MAC algorithm
Initialised AES-256 SDCTR server->client encryption
Initialised HMAC-SHA1 server->client MAC algorithm
Reading private key file "E:\Program Files (x86)\WinSCP\PuTTY\myPrivateKeyFile.ppk"
Using username "user_xyz".
Offered public key
Offer of public key accepted
Authenticating with public key "rsa-key-20140220"
Sent public key signature
Access granted
Opened channel for session
Started a shell/command
Using SFTP protocol.
Doing startup conversation with host.
Type: SSH_FXP_INIT, Size: 5, Number: -1
Type: SSH_FXP_VERSION, Size: 95, Number: -1
SFTP version 3 negotiated.
Unknown server extension"1"
Unknown server extension"2"
Unknown server extension"2"
We believe the server has signed timestamps bug
We will use UTF-8 strings for status messages only
Limiting packet size to OpenSSH sftp-server limit of 262148 bytes
Getting current directory name.
Getting real path for '.'
Type: SSH_FXP_REALPATH, Size: 10, Number: 1552
Type: SSH_FXP_NAME, Size: 45, Number: 1552
Real path is '/home/Vulcan'
Listing directory "/home/Vulcan".
Type: SSH_FXP_OPENDIR, Size: 21, Number: 1803
Type: SSH_FXP_HANDLE, Size: 13, Number: 1803
Type: SSH_FXP_READDIR, Size: 13, Number: 2060
Type: SSH_FXP_NAME, Size: 889, Number: 2060
Type: SSH_FXP_READDIR, Size: 13, Number: 2316
Type: SSH_FXP_STATUS, Size: 28, Number: 2316
Status code: 1
Type: SSH_FXP_CLOSE, Size: 13, Number: 2564
.ssh;d;4096;2014-02-20T09:26:53.000Z;"Vulcan" [7382];"Vulcangrp" [17234];rwx------;2
testfile;-;0;2014-02-20T07:31:43.000Z;"root" [0];"root" [0];rw-r--r--;0
.bashrc;-;124;2013-07-09T13:24:50.000Z;"Vulcan" [7382];"Vulcangrp" [17234];rw-r--r--;2
.bash_profile;-;176;2013-07-09T13:24:50.000Z;"Vulcan" [7382];"Vulcangrp" [17234];rw-r--r--;2
..;d;12288;2014-02-20T07:19:30.000Z;"root" [0];"root" [0];rwxr-xr-x;0
.kshrc;-;121;2013-05-16T13:24:27.000Z;"Vulcan" [7382];"Vulcangrp" [17234];rw-r--r--;2
.bash_logout;-;18;2013-07-09T13:24:50.000Z;"Vulcan" [7382];"Vulcangrp" [17234];rw-r--r--;2
Startup conversation with host finished.

NOTE: I have modified the confidential information here (e.g. servername, username, fingerprintkey, etc)

Please help me to know the possible reason of failure. Your help is highly appreciated. :)

Reply with quote

Site Admin
martin avatar
Prague, Czechia

Re: Connection has been unexpectedly closed. Server sent command

Please attach a full session log file showing the problem (using the latest version of WinSCP).

To generate log file, set Session.SessionLogPath. Submit the log with your post as an attachment. Note that passwords and passphrases not stored in the log. You may want to remove other data you consider sensitive though, such as host names, IP addresses, account names or file names (unless they are relevant to the problem). If you do not want to post the log publicly, you may email it to me. You will find my address (if you log in) in my forum profile. Please include link back to this topic in your email. Also note in this topic that you have emailed the log.

Reply with quote


You can post new topics in this forum