Hi there,
I am having trouble getting a PUT command to work using a public/private key combination in script (VB.NET). I have successfully managed to get it to work to the same connection using :-
a) the username / password via script
b) the public / private key via the WinSCP GUI
I assume therefore that it is a code problem. Here is the VB.NET code :-
Try
' Setup session options
Dim sessionOptions As New SessionOptions
With sessionOptions
.Protocol = Protocol.Sftp
.HostName = "xxxxxxxx"
.UserName = "XXXXX"
.SshHostKeyFingerprint = "xxxxxxxxx"
.SshPrivateKeyPath = "xxxxxx"
End With
Using session As Session = New Session
session.ExecutablePath = "xxxxxxx"
' Connect
session.Open(sessionOptions)
' Upload files
Dim transferOptions As New TransferOptions
transferOptions.TransferMode = TransferMode.Binary
Dim transferResult As TransferOperationResult
transferResult = session.PutFiles("xxxxxxx", "/xxxxx/", False, transferOptions)
' Throw on any error
transferResult.Check()
' Print results
Dim transfer As TransferEventArgs
For Each transfer In transferResult.Transfers
lblResult.Text += String.Format("Upload of {0} succeeded", transfer.FileName)
Next
End Using
lblResult.Text += "OK"
lblResult.Text += Now().ToLongTimeString()
Catch exx As Exception
lblResult.Text += Now().ToLongTimeString()
End Try
The error is coming from this line :-
session.Open(sessionOptions)
And the error message is :-
Connection has been unexpectedly closed. Server sent command exit status 0.
I haven't managed to find any code examples (in VB.NET or otherwise), so please could you either point me in the direction of some working code, or let me know why the script isn't working directly.
I'm not sure if this is a factor or not, but the connection does take around 80-90s to complete successfully.
Many thanks,
stevotodd