Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SFTP - secure ftp transfer using VBS 1

Status
Not open for further replies.

DPlank

IS-IT--Management
May 30, 2003
1,903
GB
Hi,

I am trying to set up an sftp transfer using VBS. I found the code for an ftp function by Nate Rice and have tried to modify it for sftp. But I can't get the code to accept the password within the batch file. I can get it to generate the login prompt, but it pauses for the password manually.

Anyone know how I can do this?

Cheers,
Dave

"Yes, I'll stop finding bugs in the software - as soon as you stop writing bugs into the software." <-- Me

For all your testing needs: Forum1393
 
That code is for regular ftp, not secure.

Having done quite a bit of research on the subject you will probably have to purchase a secure solution.

I know that is one of them.

There is one open source that I was not able to get working but I do not remember the name. A quick google ought to find it.

Simi
 
Thanks Simi - I noticed that - which is why I was trying to modify it.

After much searching, I got my mitts on a free utility called WinSCP, installed it and resolved the issue with the following code:

Code:
'###########################################################################
'# Function: MISC_FTPUpload
'#
'# Description:
'#  Uses the FSO object to FTP a file to a remote server
'#
'# Parameters:
'#	(in) sSite 			- The site to FTP to
'#	(in) sUsername		- The username to log in with
'#	(in) sPassword		- The password to log in with
'#	(in) sLocalFile		- The Locally stored file to FTP to the remote server
'#	(in) sRemotePath	- The path to store the file in, on the remote server
'#
'#	(out) - sError - The error output 
'#
'# Return:
'#  True  - File successfully sent
'#  False - File not successfully sent
'###########################################################################

Function MISC_FTPUpload(byVal sSite, byVal sUsername, byVal sPassword, byVal sLocalFile, byVal sRemotePath, byRef sError)
'This script is provided under the Creative Commons license located
'at [URL unfurl="true"]http://creativecommons.org/licenses/by-nc/2.5/[/URL] . It may not
'be used for commercial purposes with out the expressed written consent
'of NateRice.com

	Const OpenAsDefault = -2
	Const FailIfNotExist = 0
	Const ForReading = 1
	Const ForWriting = 2
	Dim oFTPScriptFSO
	Dim oFTPScriptShell
	Dim sOriginalWorkingDirectory
	Dim sFTPScript
	Dim sFTPTemp
	Dim bRetCode
	Dim sFTPTempFile
	Dim oFTPScript
	Dim sResults
	Dim sOut
	Dim sCmd
	
	LOG_Write "MISC_FTPUpload called at: " & Now
	
	Set oFTPScriptFSO = CreateObject("Scripting.FileSystemObject")
	Set oFTPScriptShell = CreateObject("WScript.Shell")
	
	sRemotePath = Trim(sRemotePath)
	sLocalFile = Trim(sLocalFile)
	
	'----------Path Checks---------
	'Here we will check the path, if it contains
	'spaces then we need to add quotes to ensure
	'it parses correctly.
	If InStr(sRemotePath, " ") > 0 Then
		If Left(sRemotePath, 1) <> """" And Right(sRemotePath, 1) <> """" Then
			  sRemotePath = """" & sRemotePath & """"
		End If
	End If
	
	If InStr(sLocalFile, " ") > 0 Then
		If Left(sLocalFile, 1) <> """" And Right(sLocalFile, 1) <> """" Then
	  		sLocalFile = """" & sLocalFile & """"
		End If
	End If
	
	'Check to ensure that a remote path was
	'passed. If it's blank then pass a "\"
	If Len(sRemotePath) = 0 Then
		'Please note that no premptive checking of the
		'remote path is done. If it does not exist for some
		'reason, Unexpected results may occur.
		sRemotePath = "\"
	End If
	
	'Check the local path and file to ensure
	'that either the a file that exists was
	'passed or a wildcard was passed.
	If InStr(sLocalFile, "*") Then
		If InStr(sLocalFile, " ") Then
			sError = "Error: Wildcard uploads do not work if the path contains a space." & vbNewLine & "This is a limitation of the Microsoft FTP client."
	  		LOG_Write sError
	  		MISC_FTPUpload = False
	  		Exit Function
		End If
	ElseIf Len(sLocalFile) = 0 Or Not oFTPScriptFSO.FileExists(sLocalFile) Then
		'nothing to upload
		sError = "Error: File Not Found."
		LOG_Write sError
		MISC_FTPUpload = False
		Exit Function
	End If
	'--------END Path Checks---------
	
	'build input file for ftp command
	sFTPScript = sFTPScript & "option batch on" & vbCRLF
	sFTPScript = sFTPScript & "option confirm off"& vbCrLf  
	sFTPScript = sFTPScript & "option transfer binary" & vbCrLf
	sFTPScript = sFTPScript & "open sftp://" & sUsername & ":" & sPassword & "@" & sSite & vbCrLf
	sFTPScript = sFTPScript & "cd " & sRemotePath & vbCrLf
	sFTPScript = sFTPScript & "put " & sLocalFile & vbCRLF
	sFTPScript = sFTPScript & "close" & vbCrLf
	sFTPScript = sFTPScript & "exit" & vbCrLf
	
	LOG_Write "Script for FTP File: " & vbNewLine & sFTPScript
	
	sFTPTemp = oFTPScriptShell.ExpandEnvironmentStrings("%TEMP%")
	sFTPTempFile = sFTPTemp & "\" & oFTPScriptFSO.GetTempName
	LOG_Write "FTP Input file stored at: " & sFTPTempFile
	
	'Write the input file for the ftp command
	'to a temporary file.
	Set oFTPScript = oFTPScriptFSO.CreateTextFile(sFTPTempFile, True)
	oFTPScript.WriteLine(sFTPScript)
	oFTPScript.Close
	Set oFTPScript = Nothing  
	
	sCmd = """C:\Program Files\WinSCP\WinSCP.com"" -script=" & sFTPTempFile 
	MISC_RunCmd sCmd, sOut, sError
	LOG_Write sOut
	
	Wscript.Sleep 1000
	
	' Get rid of temp file used for input to sftp
	oFTPScriptFSO.DeleteFile(sFTPTempFile)
	
	'Check results of transfer.	
	If sError = ""  And InStr(sOut, "binary") >0  And InStr(sOut, "100%") >0 Then
		MISC_FTPUpload = True
	Else
		sError = "Error: " & sError
		LOG_Write sError
		MISC_FTPUpload = False 
	End If
	
	Set oFTPScriptFSO = Nothing
	Set oFTPScriptShell = Nothing
End Function

'###########################################################################
'# Function: MISC_FTPDownload
'#
'# Description:
'#  Uses the FSO object to FTP a file from a remote server
'#
'# Parameters:
'#	(in) sSite 			- The site to FTP from
'#	(in) sUsername		- The username to log in with
'#	(in) sPassword		- The password to log in with
'#	(in) sLocalPath		- The path to store the file in, on the local drive
'#	(in) sLocalPath		- The path to get the file from, on the remote drive
'#	(in) sRemoteFile	- The remotely stored file to FTP to the local drive
'#
'#	(out) - sError - The error output 
'#
'# Return:
'#  True  - File successfully retrieved
'#  False - File not successfully retrieved
'###########################################################################

Function MISC_FTPDownload(byVal sSite, byVal sUsername, byVal sPassword, byVal sLocalPath, byVal sRemotePath, byVal sRemoteFile, byRef sError)
'This script is provided under the Creative Commons license located
'at [URL unfurl="true"]http://creativecommons.org/licenses/by-nc/2.5/[/URL] . It may not
'be used for commercial purposes with out the expressed written consent
'of NateRice.com

	Const OpenAsDefault = -2
	Const FailIfNotExist = 0
	Const ForReading = 1
	Const ForWriting = 2
	Dim oFTPScriptFSO
	Dim oFTPScriptShell
	Dim sOriginalWorkingDirectory
	Dim sFTPScript
	Dim sFTPTemp
	Dim sFTPTempFile
	Dim bRetCode
	Dim oFTPScript
	Dim sResults
	Dim sCmd
	Dim sOut
	
	LOG_Write "MISC_FTPDownload called at: " & Now
	
	Set oFTPScriptFSO = CreateObject("Scripting.FileSystemObject")
	Set oFTPScriptShell = CreateObject("WScript.Shell")
	
	sRemotePath = Trim(sRemotePath)
	sLocalPath = Trim(sLocalPath)
	
	'----------Path Checks---------
	'Here we will check the remote path, if it contains
	'spaces then we need to add quotes to ensure
	'it parses correctly.
	If InStr(sRemotePath, " ") > 0 Then
		If Left(sRemotePath, 1) <> """" And Right(sRemotePath, 1) <> """" Then
			sRemotePath = """" & sRemotePath & """"
		End If
	End If
	
	'Check to ensure that a remote path was
	'passed. If it's blank then pass a "\"
	If Len(sRemotePath) = 0 Then
		'Please note that no premptive checking of the
		'remote path is done. If it does not exist for some
		'reason. Unexpected results may occur.
		sRemotePath = "\"
	End If
	
	'If the local path was blank. Pass the current
	'working direcory.
	If Len(sLocalPath) = 0 Then
		sLocalPath = oFTPScriptShell.CurrentDirectory
	End If
	
	If Not oFTPScriptFSO.FolderExists(sLocalPath) Then
		'destination not found
		sError = "Error: Local Folder Not Found."
		LOG_Write sError
		MISC_FTPDownload = False
		Exit Function
	End If
	
	sOriginalWorkingDirectory = oFTPScriptShell.CurrentDirectory
	oFTPScriptShell.CurrentDirectory = sLocalPath
	'--------END Path Checks---------
	
	'build input file for ftp command
	sFTPScript = sFTPScript & "option batch on" & vbCRLF
	sFTPScript = sFTPScript & "option confirm off"& vbCrLf
	sFTPScript = sFTPScript & "option transfer binary" & vbCrLf
	sFTPScript = sFTPScript & "open sftp://" & sUsername & ":" & sPassword & "@" & sSite & vbCrLf
	sFTPScript = sFTPScript & "cd " & sRemotePath & vbCrLf
	sFTPScript = sFTPScript & "get " & sRemoteFile & vbCRLF
	sFTPScript = sFTPScript & "close" & vbCrLf
	sFTPScript = sFTPScript & "exit" & vbCrLf
	
	LOG_Write "Script for FTP File: " & vbNewLine & sFTPScript
	
	sFTPTemp = oFTPScriptShell.ExpandEnvironmentStrings("%TEMP%")
	sFTPTempFile = sFTPTemp & "\" & oFTPScriptFSO.GetTempName
	LOG_Write "FTP Input file stored at: " & sFTPTempFile
	
	'Write the input file for the ftp command
	'to a temporary file.
	Set oFTPScript = oFTPScriptFSO.CreateTextFile(sFTPTempFile, True)
	oFTPScript.WriteLine(sFTPScript)
	oFTPScript.Close
	Set oFTPScript = Nothing  
	
	sCmd = """C:\Program Files\WinSCP\WinSCP.com"" -script=" & sFTPTempFile 
	MISC_RunCmd sCmd, sOut, sError
	LOG_Write sOut
	
	Wscript.Sleep 1000
	
	' Get rid of temp file used for input to sftp
	oFTPScriptFSO.DeleteFile(sFTPTempFile)
	
	'Check results of transfer.	
	If sError = ""  And InStr(sOut, "binary") >0  And InStr(sOut, "100%") >0 Then
		MISC_FTPDownload = True
	Else
		sError = "Error: " & sError
		LOG_Write sError
		MISC_FTPDownload = False 
	End If
	
	Set oFTPScriptFSO = Nothing
	Set oFTPScriptShell = Nothing
End Function
LOG_Write and MISC_RunCmd functions are separately defined but it's relatively easy to see what they do...

Hope this may be of use to someone else!

Cheers,
Dave

"Yes, I'll stop finding bugs in the software - as soon as you stop writing bugs into the software." <-- Me

For all your testing needs: Forum1393
 
I am trying to use your code but I can't log in.
My IIS is working fine, I can log n to remote FTP server runing a batchfile from IIS machine, but I can not use this ASP code to log in from my IIS machine to the remote FTP server.
Please let me know what I am missing. I copied your code exactly and added the following for the run part:

sCmd2 = """C:\Program Files\WinSCP\WinSCP.com"" -script=" & sFTPTempFile

sout2 = oFTPScriptShell.run (sCmd2, 3, true)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top