Topic "Using FTP via COM returns Protocol.Sftp or Protocol.Scp"

Author Message
trh@marcello.no
[View user's profile]

Joined: 2013-02-19
Posts: 3
Location: Oslo
Hello!

I have a project where i shall transfer files to a FTP server. To this, I have tried to use WinSCP with the .NET assembly/ Com library.

Using VBscript Embarassed

I have tested with following downloaded installations:

winscp514automation.zip/winscp514setup.exe (calling returns that winscp.dll is incompatible, so tested older versions as well):
winscp513automation.zip/winscp513setup.exe
winscp510automation.zip/winscp510setup.exe

I have registered Winscp.dll using:

SET NETVersion=v4.0.30319
call "%WINDIR%\Microsoft.NET\Framework64\%NETVersion%\RegAsm.exe" "WinSCP.dll" /codebase /tlb

Where WinSCP is copied to %ProgramFiles(x86)%\WinSCP and above call is executed in this directory.

I use the example as provided here: http://winscp.net/eng/docs/library_com_wsh#vbscript, with the following modifications:


With sessionOptions
.FtpMode = Passive
.Protocol = Protocol_Ftp
.HostName = "example.com"
.UserName = "user"
.Password = "mypassword"
End With

(Hostname, Username and Password is changed for my environment in my code)

However, on all my computers this fails with:

WinSCP: SessionOptions.Protocol is Protocol.Sftp or Protocol.Scp, but SessionOptions.HostKey is not set.
...and if I provide SessionOptions.HostKey:

WinSCP: SSH host key fingerprint "ssh-rsa1024 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx" does not match pattern .. blah blah.

A SSH key should not be needed for FTP anyhow, so.....

Any tips on what I do wrong?
Advertisements
martin
[View user's profile]
Site Admin
Joined: 2002-12-10
Posts: 24512
Location: Prague, Czechia
For 5.1.4: I have released development version of the .NET assembly with 5.1.4 by mistake. Will fix it shortly.

I have tested the example VBS code again using 5.1.3 and it works correctly for me. When I set Protocol to Protocol_Ftp, I'm not asked for host key.

Are you using the WSF solution for the enumeration values? Or have you defined the enums yourself (possibly setting the Prototol_Ftp to wrong value, should be 2). The Passive token seems suspicious to me, as the correct syntax would be FtpMode_Protocol.

Can you post your complete code? Can you try to print the SessionOptions.Protocol before Session.Open to verify it has correct value?
trh@marcello.no
[View user's profile]

Joined: 2013-02-19
Posts: 3
Location: Oslo
I might be a noob here, anyhow. Here is the code with some corrections.


Host, username and password is maniuplated in the dump...

Funny thing, if I uncomment "CONST Protocol_ftp=2", nothing happends, no errors, no transfer - only the message from my debugf flagged console output:
Transferring C:\Users\tonny.r.holm\Dropbox\Jobb\Programmering\2013 Efaktura-Itella\Work\test\* to destination out/test/data Using:2

You might se I have created a case selct to handle other transfer methods, but they are disabled for the momen - only testing FTP.

-----------------------------

Debugf=true
' CONST Protocol_ftp=2
' http://www.classictriple.com/blog/2012/5/28/file-transfer-automation-with-winscp.html
Result=PutFileFTPWinSCP("ftp.mytestserver.net","Protocol_Ftp","USER","password","C:\Users\tonny.r.holm\Dropbox\Jobb\Programmering\2013 Efaktura-Itella\Work\test\*","out/test/data")

Function PutFileFTPWinSCP(strHostname,strProtocol,strUsername,strPassword,strSourcefiles,strDestDir)
' Module for Remote connect using WinSCP Assembly.

'strProtocol = Protocol_Sftp/Protocol_Scp/Protocol_Ftp
'strHostname = "example.com"
'strUsername = "user"
'strPassword = "mypassword"
'strSourcefiles = "d:\toupload\*"
'strDestDir = /home/user/
' .SshHostKeyFingerprint = "ssh-rsa 1024 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx"


' Setup session options
Dim sessionOptions
Set sessionOptions = WScript.CreateObject("WinSCP.SessionOptions")


With sessionOptions
.Protocol = Protocol_ftp
.HostName = strHostname
.UserName = strUsername
.Password = strPassword
'.SshHostKeyFingerprint = "ssh-rsa 1024 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx"
End With

'Select CASE strProtocol
'Case "Protocol_Sftp"
' With sessionOptions
' .Protocol = Protocol_Sftp
' .HostName = strHostname
' .UserName = strUsername
' .Password = strPassword
' .SshHostKeyFingerprint = "ssh-rsa 1024 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx"
' End With
'Case "Protocol_Scp"
' With sessionOptions
' .Protocol = Protocol_Scp
' .HostName = strHostname
' .UserName = strUsername
' .Password = strPassword
' .SshHostKeyFingerprint = "ssh-rsa 1024 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx"
' End With
'Case "Protocol_Ftp"
' if debugf then Wscript.echo "Using Protocol_Ftp"
' With sessionOptions
' .FtpMode = Passive
' .Protocol = Protocol_Ftp
' .HostName = strHostname
' .UserName = strUsername
' .Password = strPassword
' '.SshHostKeyFingerprint = "ssh-rsa 1024 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx"
' End With
'End Select


Dim session
Set session = WScript.CreateObject("WinSCP.Session")
' Ignore wrong Assembly version
Session.DisableVersionCheck=True

' Connect
session.Open sessionOptions

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

Dim transferResult
Set transferResult = session.PutFiles(strSourcefiles, strDestDir, False, transferOptions)

if debugf then WScript.Echo "Transferring " & strSourcefiles & " to destination " & strDestDir & " Using:" & SessionOptions.Protocol

' Throw on any error
transferResult.Check

' Print results
Dim transfer
For Each transfer In transferResult.Transfers
if debugf then WScript.Echo "Upload of " & transfer.FileName & " succeeded"
Next

' Disconnect, clean up
session.Dispose

End function
martin
[View user's profile]
Site Admin
Joined: 2002-12-10
Posts: 24512
Location: Prague, Czechia
Can you please try using the WSF solution?
http://winscp.net/eng/docs/library_com_wsh#enums
trh@marcello.no
[View user's profile]

Joined: 2013-02-19
Posts: 3
Location: Oslo
prikryl wrote:
Can you please try using the WSF solution?
http://winscp.net/eng/docs/library_com_wsh#enums


Yep, that worked. I tried that actually earlier; however seems like that test had other problems (using 5.14 or the fact that it was a 64 bit OS). However, now that I have one working test code I am a bit more optimistic in figuring out other issues Wink

Thanks.
martin
[View user's profile]
Site Admin
Joined: 2002-12-10
Posts: 24512
Location: Prague, Czechia
Also, I have tried your code. With "CONST Protocol_ftp=2" line enabled, it works ok for me.
Advertisements

You can post new topics in this forum






Search Site

What is WinSCP?

It is award-winning SFTP client, SCP client, FTPS client and FTP client integrated into one software program for file transfer to FTP server or secure SFTP server. [More]

And it's free!

Donate

About donations

$9   $19   $49   $99

About donations

Recommend

WinSCP Privacy Policy

WinSCP License