How to connect to a SFTP server (like freeSSHd) us - WeOnlyDo Discussion board

How to connect to a SFTP server (like freeSSHd) us (General questions)

by Duncan Newman, Tuesday, November 20, 2007, 11:55 (6213 days ago)

Never really answered anywhere ... and after a while of trying to figure it out I've got the following code. Hopefully it'll make it onto google at some point as there is nothing on SFTP via VBS to be found!

Feel free to use the code, or maybe add something similar to the documentation WOD :)

[code]
' This example connects to a password-less SFTP server
' authenticating using keys generated by Putty or similar.
' The user should be set ON THE SFTP server to Password: Disabled
' and Public Key authentication: Required
' =================================================================
' Set Variables
Option Explicit
Dim objFTP, lst, key
Set objFTP = WScript.CreateObject( WeOnlyDo.wodSFTPCom.1 , wod_ )
Set Key = WScript.CreateObject( WeOnlyDo.Keys.1 )
' =================================================================
' User configurable variables
' =================================================================

' Load the Private Key ready for use
key.Load c:privatekey.ppk
' If you have a password set on your key (you should really) use the following line instead:
' key.Load c:privatekey.ppk , password_on_key

' Set the hostname/IP to connect to
objFTP.Hostname = server.ip
' User name for the SFTP server
objFTP.Login = username
' Set authentication type to Public Key only
objFTP.Authentication = 2
' Set the Key ready for authentication
objFTP.PrivateKey = key
' =================================================================
' END OF User configurable variables
' =================================================================

objFTP.Blocking = 1
' Connect to server with settings....
objFTP.Connect


' Bit of random code to get something out (shows it is working)
' List directory....
objFTP.ListDir ( / )
lst = objFTP.ListItem
' Echo the results...
WScript.echo lst
' Disconnect...
objFTP.Disconnect

' Triggered events - this automatically fires upon connect....
Sub wod_Connected(ErrorCode, ErrorText)
WScript.Echo Connected & ErrorText
End Sub

' Triggered events - this automatically fires upon disconnect....
Sub wod_Disconnected()
WScript.Echo Disconnected
End Sub
[/code]

Re: How to connect to a SFTP server (like freeSSHd

by Duncan Newman, Tuesday, November 20, 2007, 11:55 (6213 days ago) @ Duncan Newman

Title should read....

How to connect to a SFTP server (like freeSSHd) using Public/Private Keys in VBS

:)

Re: How to connect to a SFTP server (like freeSSHd

by Duncan Newman, Tuesday, November 20, 2007, 12:01 (6213 days ago) @ Duncan Newman

Also the slashes have been ripped out of the code block. You will need to use a proper path (obviously!) to get the key from the file else you will recieve the error:

Failed to import key from file.

Bit annoying that. Should it not leave code alone as otherwise you cannot copy paste it?

Re: How to connect to a SFTP server (like freeSSHd

by woddrazen, Tuesday, November 20, 2007, 12:07 (6213 days ago) @ Duncan Newman

Hi Duncan,


Thanks.

Actually we do have example how to connect to server using PrivateKey in wodSFTP in our Knowledge base here:
http://www.weonlydo.com/index.asp?kb=1&View=entry&EntryID=55

Also I have found in forum example how to done it in VBS here:
http://www.weonlydo.com/index.asp?forum=1&action=view&topic=1169631137#1169631137


Regards,
Drazen

Re: How to connect to a SFTP server (like freeSSHd

by Duncan Newman, Tuesday, November 20, 2007, 12:13 (6213 days ago) @ woddrazen

Arrrrggggghhhhh! I spent ages looking for that yesterday no idea how I didn't spot it! Twice! Must have been blind as a bat!

Well I hope it might be useful to someone.
Is there any reason why it is not in the CHM? Would have thought that most people want to use keys rather than a password to connect to SFTP?

Anyway thanks for the reply - will have to look three times as hard next time!

Hi Duncan,


Thanks.

Actually we do have example how to connect to server using PrivateKey in wodSFTP in our Knowledge base here:
http://www.weonlydo.com/index.asp?kb=1&View=entry&EntryID=55

Also I have found in forum example how to done it in VBS here:
http://www.weonlydo.com/index.asp?forum=1&action=view&topic=1169631137#1169631137


Regards,
Drazen