FTP Management Pack VBScript

One of the problems when authoring a FTP Management Pack is having to write a FTP command file to parse the commands to the FTP server. I had a look around and came up with the following VBScript which doesn’t make use of a FTP command file. The script will parse FTP commands directly to the FTP server and uses RexEx to query the output. Feel free to copy and modify the script to fit your use.

Set oAPI   = CreateObject("MOM.ScriptAPI")
Set oBag   = oAPI.CreatePropertyBag()
Set oArgs   = WScript.Arguments
Set oShell   = CreateObject("WScript.Shell")
Set oFso   = CreateObject("Scripting.FileSystemObject")

strFTPIPAddress   = oArgs(0)
strFTPUserName    = oArgs(1)
strFTPPassword    = oArgs(2)
strFTPDirectory    = oArgs(3)
strFTPTestDirectory = oArgs(4)
Call oAPI.LogScriptEvent("FTPMonitor.vbs",101,0,vbcrlf & strFTPIPAddress & vbcrlf & strFTPUserName & vbcrlf & strFTPPassword  & vbcrlf & strFTPDirectory  & vbcrlf & strFTPTestDirectory)
'Set 1st regex to detect errors in output
Set regExError = New RegExp
regExError.IgnoreCase = True
regExError.Pattern = "Not connected|t change directory|Permission denied"

'set 2nd regex to detect no output (FTP server is not responding)
Set regExFTPConnection = New RegExp
regExFTPConnection.IgnoreCase = True
regExFTPConnection.Pattern = "220"

Healthy = "true"

'Open a FTP connection
Set oExec = oShell.exec("<a href="ftp://ftp.exe">ftp.exe</a> -n -i -v")
set FTPCommand = oExec.stdin
Set FTPResponce = oExec.stdout

'Issue the FTP commands
FTPCommand.writeline "open " & strFTPIPAddress
FTPCommand.writeline "user " & " " & strFTPUserName & " " & strFTPPassword
FTPCommand.writeline "cd /" & strFTPDirectory
FTPCommand.writeline "mkdir " & strFTPTestDirectory
FTPCommand.writeline "rmdir " & strFTPTestDirectory
FTPCommand.writeline "bye"

'Check the FTP output with regex
While oExec.Status <> 1
strResponce = FTPResponce.readline
Set ErrorMatches         = regExError.Execute(strResponce)
Set FTPConnectionMatches = regExFTPConnection.Execute(strResponce)
If ErrorMatches.Count = 1 Then
Healthy = "false"
End If

If FTPConnectionMatches.Count = 1 Then
strFTPConnection = strFTPConnection + 1
End If

If strFTPConnection = 0 Then
Healthy = "false"
End If

'Set the status for the PropertyBag
if Healthy = "false" then
Call oBag.AddValue("Status","CRITICAL")
Call oBag.AddValue("Status","HEALTHY")
End If

Call oAPI.Return(oBag)
This entry was posted in Management Pack, MP, VBScripts, Walkthrough. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s