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

Advertisement

Jaya
Guest

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

Hi I am using the belowcode to connect to SFTP Through WinSCP. It gets connected only once.Next time when I run the code I get the error at this line
"mySession.Open mySessionOptions"

connection has been unexpectedly closed. Server sent command exit status 0.Authentication failed
Please Help.


Option Explicit

Sub Connect_To_SFTP()

Dim mySession As New session


' Enable custom error handling
On Error Resume Next

Upload mySession

' Query for errors
If Err.Number <> 0 Then
MsgBox "Error: " & Err.Description

' Clear the error
Err.Clear
End If

' Disconnect, clean up
mySession.Dispose

' Restore default error handling
On Error GoTo 0

End Sub

Sub Upload(ByRef mySession As session)
Dim works As String

' Setup session options
Dim mySessionOptions As New sessionOptions
'bool GiveUpSecurityAndAcceptAnySshHostKey
With mySessionOptions
.Protocol = Protocol_Sftp
.HostName = "lab.com"
.UserName = "*****"
.Password = "******"
.GiveUpSecurityAndAcceptAnySshHostKey = True
End With

' Connect
mySession.Open mySessionOptions

' Upload files
Dim transferOptions As New transferOptions
'Set transferOptions = WScript.CreateObject("WinSCP.TransferOptions")
transferOptions.TransferMode = TransferMode_Binary

Dim transferResult
Set transferResult = mySession.GetFiles("/Result/Output_TXT/*", "c:\toupload\", False, transferOptions)

' Throw on any error
transferResult.Check

' Print results
Dim transfer
For Each transfer In transferResult.Transfers
Debug.Print "Download of " & transfer.Filename & " succeeded"
Next
' Disconnect, clean up
mySession.Dispose

End Sub

Reply with quote

Advertisement

martouf
Guest

Connection unexpectedly closed

Try following the thread of execution but focus only on the mySession variable.

Connect_To_SFTP()
.
.
Dim mySession
.
.
Upload( mySession ) -> passed by reference
.
.
Dispose mySession
.
.
Return to Connect_To_SFTP()
.
.
Dispose mySession
.
.
Return to <caller>

Reply with quote

Jaya
Guest

Hello yesterday I did windows update. Now its giving me Automation error-2146234304
Please help me with this first.
so that i can check the above solution

Reply with quote

martouf
Guest

sorry, but "the above" is not a SOLUTION but a big HINT of what the problem is likely to be.

i could be wrong, but a double DISPOSE is not likely to give good results.

in general, it is my understanding that good coding practice is to perform a Dispose only in the method or subroutine that performed the variable allocation ("Dim") -especially- if the variable is passed around by reference (and not by value).

Reply with quote

Advertisement

Jaya
Guest

martin wrote:

Jaya wrote:

Hello yesterday I did windows update. Now its giving me Automation error-2146234304
Please help me with this first.
so that i can check the above solution
Try to unregister (!) and then re-register the .NET assembly.
https://winscp.net/eng/docs/library_install#registering


Hello I unregisterd using the command
SET WorkFolder= '<directory>'
regasm.exe /unregister %WorkFolder%\xxx.dll

And re registerd it.

Still getting the same error
Please help

Reply with quote

Advertisement

Jaya
Guest

martin wrote:

So you have some .exe file built from your code? Is it VB6?

No .exe files.Have .tlb and .dll files.
It's VB 7.1.

Reply with quote

Jaya
Guest

martin wrote:

Sorry, I have no idea how VB works. So again, do you build some .exe from your code? Or how do you run it?


Calling Winscp.exe and COM object accessing Microsoft .NET framework.

Reply with quote

Advertisement

Jaya
Guest

martin wrote:

Sorry, I have no idea how VB works. So again, do you build some .exe from your code? Or how do you run it?

I have restored the system.Now no automation error.
Please help me with this.

I am using the above code to connect to SFTP Through WinSCP. It gets connected only once.Next time when I run the code I get the error at this line
"mySession.Open mySessionOptions"

connection has been unexpectedly closed. Server sent command exit status 0.Authentication failed
Please Help.

Reply with quote

martin
Site Admin
martin avatar
Joined:
Posts:
40,476
Location:
Prague, Czechia

Jaya wrote:

I am using the above code to connect to SFTP Through WinSCP. It gets connected only once.Next time when I run the code I get the error at this line
"mySession.Open mySessionOptions"

connection has been unexpectedly closed. Server sent command exit status 0.Authentication failed
Please Help.
Please attach a full session log file for both sessions (using the latest version of WinSCP).

To generate the session 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 can mark the attachment as private.

Reply with quote

Jaya
Guest

martin wrote:

Jaya wrote:

I am using the above code to connect to SFTP Through WinSCP. It gets connected only once.Next time when I run the code I get the error at this line
"mySession.Open mySessionOptions"

connection has been unexpectedly closed. Server sent command exit status 0.Authentication failed
Please Help.
Please attach a full session log file for both sessions (using the latest version of WinSCP).

To generate the session 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 can mark the attachment as private.


i have attached the code.
Registered regasm.exe using commands
"%WINDIR%\Microsoft.NET\Framework\v4.0.30319\RegAsm.exe WinSCPnet.dll /codebase /tlb:WinSCPnet32.tlb"
"%WINDIR%\Microsoft.NET\Framework64\v4.0.30319\RegAsm.exe WinSCPnet.dll /codebase /tlb:WinSCPnet64.tlb"

Reply with quote

Advertisement

Jaya
Guest

martin wrote:

There's no log file attached to your post.

what are log files?. I'm new to VBA I don't know what are log files.

Reply with quote

martin
Site Admin
martin avatar

Jaya wrote:

what are log files?. I'm new to VBA I don't know what are log files.
Read my previous post carefully again!

Reply with quote

Advertisement

You can post new topics in this forum