Using FTP in WSH - Putting the WSH script together
(Page 3 of 4 )
At this point you can probably see where I’m going with this. We’ll be creating a WSH script that creates an unattended ftp script on the fly and then executes the ftp command. While the example in this article takes a very direct approach, try to keep in mind that this can be a very dynamic process if need be.
Don’t feel like you’re stuck creating simple line-by-line script solutions. You can create an upload script to synchronize a dynamic directory structure, for example. All you need to do is analyze the structure, gather the folder and file names, and use those to create your unattended script file. You’ll see that this is much easier than it sounds.
Let’s take a look at the code. To begin, we’re going to establish our server details and instantiate both the WshShell object and the FileSystemObject. We’ll also create a temporary text file that will be used as an unattended ftp script.
Const HOSTNAME = "ftp.myserver.com"
Const USERNAME = "Login"
Const PASSWORD = "password"
Set WshShell = CreateObject("WScript.Shell")
Set objFso = CreateObject("Scripting.FileSystemObject")
Set objFile = objFso.CreateTextFile("session.txt")
Next, we’ll add the necessary ftp commands by writing them to the ftp script file.
With objFile
.WriteLine "USER username"
.WriteLine "password"
.WriteLine "cd /public_html/pub/examples"
.WriteLine "get word.vbs"
.Close
End With
Finally, we’ll execute the ftp command using the WshShell object’s Exec method.
strFTP = "%systemroot%System32ftp.exe -s:session.txt"
strFTP = WshShell.ExpandEnvironmentStrings(strFTP)
WshShell.Run strFTP
For the sake of compatibility, I’ve used an environmental variable in my path. Be aware that you cannot do this if you intend to run this script as a Scheduled Task. Scheduled Tasks do not support the use of environmental variables in WSH scripts and will require you to hard code the path.
objFso.DeleteFile "session.txt", vbTrue
All that’s left now is a little cleanup to remove the temporary text file that we’ve created. Now let’s take a look at some potential security vulnerabilities that we should take into consideration.
Next: Security considerations when using unattended ftp scripts >>
More Windows Scripting Articles
More By Nilpo